-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown.js
94 lines (70 loc) · 1.68 KB
/
markdown.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
/**
* hmdoc.Markdown
* written by Joel Dentici
* on 7/19/2017
*
* Markdown documentation generator
*/
function prettyHmType(hmType) {
return hmType.replace(/->/g, '→').replace(/=>/g, '⇒');
}
function modLink(name) {
return name
.replace(/\s/g, '-')
.replace(/\./g, '-')
.toLowerCase();
}
function generateModuleSynopsis(module) {
const link = modLink(module.name);
return `* [${module.name}](#${link})`;
}
function generateFunctionDescription(fn) {
const typeSig = prettyHmType(fn.hmType);
return `
#### ${fn.name} :: ${typeSig}
${fn.description}
`.trim();
}
function getAuthors(authors) {
if (authors.length > 1) {
return authors.slice(0, -1).join(', ')
+ ', and '
+ authors.slice(-1)[0];
}
else {
return authors[0];
}
}
function generateModuleDescription(module) {
const anchor = `<a name="${modLink(module.name)}"></a>`
const authors = getAuthors(module.author);
const functions = module.functions
.map(generateFunctionDescription).join('\n');
return `
## ${module.name}
${anchor}
**Written By:** ${authors}
**Written On:** ${module.date}
${module.description}
${functions}
`.trim();
}
function generateSynopsis(modules) {
const list = modules.map(generateModuleSynopsis).join('\n');
return `
## Modules:
Click a module name below to see its documentation
${list}`.trim();
}
function generateDescription(modules) {
return modules.map(generateModuleDescription).join('\n');
}
function generateMarkdown(projectName, modules) {
const synopsis = generateSynopsis(modules);
const description = generateDescription(modules);
return `
# ${projectName} Documentation
${synopsis}
${description}`.trim();
}
module.exports = generateMarkdown;