-
Notifications
You must be signed in to change notification settings - Fork 72
/
dev-chars.js
60 lines (41 loc) · 1.88 KB
/
dev-chars.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
const fs = require("fs");
const path = require("path");
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Path to the _posts directory
const postsDir = path.join(__dirname, "..", "_source", "_posts");
// Get a list of all files in the _posts directory
fs.readdir( postsDir, (err, files) => {
if ( err ) {
console.log( err );
return;
}
const fn = files[files.length-1];
var contents = fs.readFileSync(postsDir+'/'+fn, 'utf8');
// Count matches to report
var count_quotes = (contents.match(/[‘’“”…]/g) || []).length;
var count_urls = (contents.match(/\(https:\/\/developer\.okta\.com\/blog\/(.+)\)/g) || []).length;
var count_github_case = (contents.match(/Github/g) || []).length;
console.log("Reading latest post "+fn);
console.log("Found "+count_quotes+" curly quotes, "+count_urls+" absolute blog URLs, and "+count_github_case+" 'GitHub' case issues.");
rl.question("Replace characters? [Y/n] ", (answer) => {
if(answer.toLowerCase() == 'yes' || answer.toLowerCase() == 'y' || answer == '') {
contents = contents.replace(/‘/g, "'")
.replace(/’/g, "'")
.replace(/“/g, '"')
.replace(/”/g, '"')
.replace(/…/g, '...');
console.log("");
console.log("Replaced "+count_quotes+" characters");
contents = contents.replace(/\(https:\/\/developer\.okta\.com\/blog\/(.+)\)/g, '(/blog/$1)');
console.log("Replaced "+count_urls+" instances of absolute blog URLs");
contents = contents.replace(/Github/g, 'GitHub');
console.log("Replaced "+count_github_case+" instances of 'Github' to 'GitHub'");
fs.writeFileSync(postsDir+'/'+fn, contents);
}
rl.close();
});
});