-
Notifications
You must be signed in to change notification settings - Fork 7
/
generate-readme-toc.js
executable file
·49 lines (37 loc) · 1.28 KB
/
generate-readme-toc.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
'use strict';
// Load modules
const Toc = require('markdown-toc');
const Fs = require('fs');
const Package = require('./package.json');
// Declare internals
const internals = {
api: {
filename: './API.md',
contents: Fs.readFileSync('./API.md', 'utf8')
},
readme: {
filename: './README.md',
contents: Fs.readFileSync('./README.md', 'utf8')
}
};
internals.generateToc = function () {
const tocOptions = {
bullets: '-',
slugify: function (text) {
return text.toLowerCase()
.replace(/\s/g, '-')
.replace(/[^\w-]/g, '');
}
};
const api = Toc.insert(internals.api.contents, tocOptions)
.replace(/<!-- version -->(.|\n)*<!-- versionstop -->/, `<!-- version -->\n# ${Package.version} API Reference\n<!-- versionstop -->`);
Fs.writeFileSync(internals.api.filename, api);
};
internals.generateLink = function () {
// create absolute URL for versioned docs
const readme = internals.readme.contents
.replace(/\[API Reference\]\(.*\)/gi, `[API Reference](${Package.homepage || ''}/blob/v${Package.version}/${internals.api.filename.substr(2)})`);
Fs.writeFileSync(internals.readme.filename, readme);
};
internals.generateToc();
internals.generateLink();