forked from freeCodeCamp/guide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_normaliseArticles.js
155 lines (133 loc) · 3.84 KB
/
_normaliseArticles.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const Rx = require('rx');
const fse = require('fs-extra');
const chalk = require('chalk');
const { commonREs, excludedDirs, titleify } = require('./seed/utils');
const {
httpsRE,
isAFileRE,
isAStubRE,
markdownLinkRE,
shouldBeIgnoredRE
} = commonREs;
const { Observable } = Rx;
function info(str, colour = 'red') {
console.log(chalk[colour](str));
}
const pagesDir = `${process.cwd()}/src/pages`;
function readDir(dir) {
return fse.readdirSync(dir)
.filter(item => !isAFileRE.test(item))
.filter(file => !shouldBeIgnoredRE.test(file));
}
function appendStub(path) {
const pathArr = path.split('/');
const filePath = pathArr
.slice(pathArr.indexOf('pages') + 1)
.join('/')
.toLowerCase();
const title = path
.split('/')
.slice(-1)
.join('');
const pageTitle = titleify(title);
const newMeta = (
`---
title: ${pageTitle}
---`);
/* eslint-disable max-len */
return `${newMeta}
## ${pageTitle}
This is a stub. [Help our community expand it](https://github.com/freecodecamp/guides/tree/master/src/pages/${filePath}/index.md).
[This quick style guide will help ensure your pull request gets accepted](https://github.com/freecodecamp/guides/blob/master/README.md).
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
#### More Information:
<!-- Please add any articles you think might be helpful to read before writing the article -->
`;
}
/* eslint-enable max-len */
function normaliseLinks(content) {
let anchored = content.slice(0);
const links = content.match(markdownLinkRE);
if (links) {
links
.filter(x => !x.startsWith('!'))
.filter(x => x.match(httpsRE))
.map(str => {
// raw will look like:
// [ '[guides website', 'https://guide.freecodecamp.org)' ]
const raw = str.slice(0).split('](');
const formatted = [ raw[0].replace('[', ''), raw[1].replace(')', '') ];
const [ childText, url ] = formatted;
const anchor = (
`<a href='${url}' target='_blank' rel='nofollow'>${childText}</a>`
);
anchored = anchored.replace(str, anchor);
});
}
return anchored;
}
function normalise(dirLevel) {
const filePath = `${dirLevel}/index.md`;
fse.open(filePath, 'r', (err) => {
if (err) {
if (err.code === 'ENOENT') {
console.error(
'index.md does not exist in %s',
filePath.replace(/index\.md$/, '')
);
return fse.ensureFile(filePath)
.then(() => {
console.log('%s created', filePath);
return normalise(dirLevel);
})
.catch(err => {
console.error(err);
});
}
throw err;
}
fse.readFile(filePath, 'utf-8')
.then(content => {
let normalised = content;
if (
normalised.length < 30 ||
isAStubRE.test(content)
) {
normalised = appendStub(dirLevel);
}
const finalNormalised = normaliseLinks(normalised);
fse.writeFile(filePath, finalNormalised);
})
.catch(err => {
console.error('something went wrong', err);
});
return null;
});
}
function applyNormaliser(dirLevel) {
return Observable.from(readDir(dirLevel))
.filter(dir => !excludedDirs.includes(dir))
.flatMap(dir => {
const dirPath = `${dirLevel}/${dir}`;
const subDirs = readDir(dirPath);
if (!subDirs) {
normalise(dirPath);
return Observable.of(null);
}
normalise(dirPath);
return applyNormaliser(dirPath);
});
}
applyNormaliser(pagesDir)
.subscribe((dir)=> {
if (dir) {
applyNormaliser(dir);
}
},
err => {
throw err;
},
() => {
info('\n\nNormalisation Completed\n\n', 'greenBright');
info('Please check for uncommited changes before pushing\n', 'yellow');
});