-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathconvert-https.js
85 lines (71 loc) · 2.19 KB
/
convert-https.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const matter = require('gray-matter');
const contentDir = 'content';
const contentAbsDir = path.join(__dirname, contentDir);
glob(`${contentDir}/**/*.md`, { ignore: '**/README.md' }, (err, files) => {
if (err) throw err;
console.log(`found ${files.length} files`);
files.forEach(processFile);
});
/**
* Process a file
*
* @param string file Relative path to file
*/
function processFile(file, idx) {
const rawFile = fs.readFileSync(file, 'utf8');
let content;
try {
// Read the file but ignore the links at the bottom
content = matter.read(file).content;
content = content.split('[1]')[0].trim();
} catch (e) {
console.log(`Error parsing ${file}`);
return;
}
const converted = convertShortcodeEmbeds(content);
// Check for unconverted embeds
const remainingEmbeds = getEmbedUrls(converted);
if (remainingEmbeds && remainingEmbeds.length) {
console.log([`Check embeds in ${file} (${idx}):`, ...remainingEmbeds, ''].join("\n"));
}
// Skip if no changes
if (converted === content) {
return;
}
fs.writeFile(file, rawFile.replace(content, converted), (err) => {
if (err) {
console.log(`Error writing to ${file}`);
}
console.log(`Wrote to ${file}`);
});
}
/**
* Get URLs of embedded content
*
* @param string Markdown content
* @return array URLs of embedded content
*/
function getEmbedUrls(content) {
// strip URLs that are links, either (foo)[http://bar] or {{< link="http://bar" ... >}}
const strippedLinks = content.replace(/(\[|link=")http:/, '');
// ok to use inexact regex, these will need to be checked manually anyway
return strippedLinks.match(/http:[\w\/-_\.]+/g);
}
/**
* Convert http -> https embeds Hugo shortcodes
*
* @param string content Original content
* @return string Converted content
*/
function convertShortcodeEmbeds(content) {
let converted = content;
// replace Meetup static domains
converted = converted.replace(/http:\/\/photos\d.meetupstatic/, 'https://secure.meetupstatic');
// Replace Hugo shortcodes
converted = converted.replace('src="http:', 'src="https:');
return converted;
}