-
Notifications
You must be signed in to change notification settings - Fork 7
/
generate-readme.js
84 lines (71 loc) · 2.87 KB
/
generate-readme.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
const fs = require('fs');
const path = require('path');
const url = require('url');
const markdownMagic = require('markdown-magic'); // eslint-disable-line
const globby = require('markdown-magic').globby; // eslint-disable-line
const toTitleCase = (str) => { // eslint-disable-line
return str.replace(/\w\S*/g, txt => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase());
};
const formatPluginName = (string) => { // eslint-disable-line
return toTitleCase(string.replace(/-/g, ' '));
};
const username = (repo) => {
if (!repo) {
return null;
}
const o = url.parse(repo);
var urlPath = o.path; // eslint-disable-line
if (urlPath.length && urlPath.charAt(0) === '/') {
urlPath = urlPath.slice(1);
}
urlPath = urlPath.split('/')[0];
return urlPath;
};
const config = {
transforms: {
/*
In README.md the below function will add a list from Readme files in the folders
*/
EXAMPLE_TABLE() {
const examples = globby.sync(['**/README.md', '!README.md', '!node_modules/**/package.json', '!**/node_modules/**/README.md', '!README.md', '!**/bin/**/netcoreapp2.1/**/README.md']);
// Make table header
let md = '| Example | Author |\n';
md += '|:-------|:------:|\n';
examples.forEach((example) => {
const data = fs.readFileSync(example, 'utf8');
const readme = data.toString().split(/\r?\n/);
const tableLine = readme.find(l => l.startsWith('Name'));
const tableIndex = readme.indexOf(tableLine);
const [name, description, author] = readme[tableIndex + 2].split('|');
const dirname = path.dirname(example);
const exampleUrl = `https://github.com/storyblok/field-type-examples/tree/main/${dirname}`;
// add table rows
md += `| **[${formatPluginName(name)}](${exampleUrl})** <br/> ${description} | ${author} |\n`;
});
return md;
},
/*
In README.md the below function will add a list from the community-examples.json file
*/
COMMUNITY_EXAMPLES_TABLE() {
const exampleFile = path.join(__dirname, 'community-examples.json');
const examples = JSON.parse(fs.readFileSync(exampleFile, 'utf8'));
// Make table header
let md = '| Example | Author |\n';
md += '|:-------|:------:|\n';
// Sort alphabetically
examples.sort((a, b) => a.name < b.name ? -1 : 1).forEach((data) => { // eslint-disable-line
// add table rows
const userName = username(data.githubUrl);
const profileURL = `http://github.com/${userName}`;
md += `| **[${formatPluginName(data.name)}](${data.githubUrl})** <br/>`;
md += ` ${data.description} | [${userName}](${profileURL}) |\n`;
});
return md.replace(/^\s+|\s+$/g, '');
},
},
};
const markdownPath = path.join(__dirname, 'README.md');
markdownMagic(markdownPath, config, () => {
console.log('Docs updated!'); // eslint-disable-line
});