-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-readme.js
69 lines (58 loc) · 1.95 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
/* script for generating docs example table */
const fs = require('fs')
const path = require('path')
const url = require('url')
const markdownMagic = require('markdown-magic')
const config = {
transforms: {
/* In README.md the below comment block adds the list to the readme
<!-- AUTO-GENERATED-CONTENT:START (EXAMPLES_TABLE)-->
community examples list will be generated here
<!-- AUTO-GENERATED-CONTENT:END -->
*/
EXAMPLES_TABLE() {
const exampleFile = path.join(__dirname, '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) => {
return a.name < b.name ? -1 : 1
}).forEach((data) => { // eslint-disable-line
// add table rows
const repoUrl = (Array.isArray(data.code)) ? data.code[0] : data.code
const link = (data.url) ? ` [view the project](${data.url})` : ''
const userName = username(repoUrl)
const profileURL = `http://github.com/${userName}`
md += `| **[${formatPluginName(data.name)}](${repoUrl})** <br/>`
md += ` ${data.description}${link} | [${userName}](${profileURL}) |\n`
})
return md.replace(/^\s+|\s+$/g, '')
},
},
}
const markdownPath = path.join(__dirname, 'README.md')
// run md-magic
markdownMagic(markdownPath, config, () => {
console.log('Docs updated!')
})
// util functions
function toTitleCase(str) {
return str.replace(/\w\S*/g, txt => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase())
}
function formatPluginName(string) {
return toTitleCase(string.replace(/-/g, ' '))
}
function username(repo) {
if (!repo) {
return null
}
const o = url.parse(repo)
var urlPath = o.path
if (urlPath.length && urlPath.charAt(0) === '/') {
urlPath = urlPath.slice(1)
}
urlPath = urlPath.split('/')[0]
return urlPath
}