This repository has been archived by the owner on Feb 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
146 lines (122 loc) · 4.92 KB
/
index.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
const log = require("debug")("pastel");
const fs = require('fs');
const path = require('path');
const showdown = require('showdown');
const converter = new showdown.Converter();
const defaultMetadata = {
'title': 'API Documentation',
'language_tabs': [],
'toc_footers': [
"<a href='https://github.com/knuckleswtf/pastel-js'>Documentation powered by Pastel 🎨</a>",
],
'logo': false,
'includes': [],
'last_updated': '',
};
/**
* Generate the API documentation using the markdown and include files
*/
async function generate(sourceFolder, destinationFolder = '') {
let assetsFolder = '';
let sourceMarkdownFilePath = '';
if (sourceFolder.endsWith('.md')) {
// We're given just the path to a file, we'll use default assets
sourceMarkdownFilePath = sourceFolder;
sourceFolder = path.dirname(sourceMarkdownFilePath);
assetsFolder = __dirname + '/resources';
} else {
if (!fs.existsSync(sourceFolder)) {
throw new Error(`Source folder ${sourceFolder} does not exist.`);
}
// Valid source directory
sourceMarkdownFilePath = sourceFolder + '/index.md';
assetsFolder = sourceFolder;
}
if (!destinationFolder) {
// If no destination is supplied, place it in the source folder
destinationFolder = sourceFolder;
}
let {frontmatter, converter, html} = getFrontMatterAndMainHtml(sourceMarkdownFilePath);
let filePathsToInclude = (frontmatter.includes || []).map(
include => path.resolve(sourceFolder.replace(/\/$/g, '') + '/' + include.replace(/^\//g, ''))
);
html += includeSpecifiedMarkdownFiles(filePathsToInclude, html, converter);
if (!frontmatter.last_updated) {
// Set last_updated to most recent time main or include files was modified
const timesLastUpdatedFiles = filePathsToInclude.map(function (filePath) {
const realPath = path.resolve(filePath);
try {
return fs.statSync(realPath).mtime;
} catch (e) {
// If we encounter a nonexistent file
return 0;
}
});
timesLastUpdatedFiles.push(fs.statSync(sourceMarkdownFilePath).mtime);
const lastUpdated = new Date(Math.max(...timesLastUpdatedFiles));
frontmatter.last_updated = new Intl.DateTimeFormat('en-US', {month: 'long', 'day': 'numeric', year: 'numeric'})
.format(lastUpdated);
}
const metadata = getPageMetadata(frontmatter);
const ejs = require('ejs');
const output = ejs.render(fs.readFileSync(path.join(__dirname, 'resources/views/index.ejs'), 'utf8'), {
page: metadata,
content: html,
tools: require('./utils'),
});
if (!fs.existsSync(destinationFolder)) {
fs.mkdirSync(destinationFolder, {recursive: true});
}
fs.writeFileSync(destinationFolder + '/index.html', output);
await copyAssets(assetsFolder, destinationFolder);
console.log(`Generated documentation from ${sourceMarkdownFilePath} to ${destinationFolder}.`);
}
function getFrontMatterAndMainHtml(sourceMarkdownFilePath) {
const yamlFront = require('yaml-front-matter');
const yaml = yamlFront.loadFront(fs.readFileSync(sourceMarkdownFilePath, 'utf8'));
let content = yaml.__content;
let frontmatter = yaml;
delete frontmatter.__content;
let html = converter.makeHtml(content);
return {frontmatter, converter, html};
}
function getPageMetadata(frontmatter) {
let metadata = defaultMetadata;
// Override default with values from front matter
metadata = Object.assign({}, metadata, frontmatter);
return metadata;
}
function includeSpecifiedMarkdownFiles(filePathsToInclude) {
let extraContent = '';
const glob = require("glob");
filePathsToInclude.forEach((filePath) => {
if (filePath.includes('*')) {
for (let file of glob.sync(filePath)) {
log(`Including file ${file}`);
extraContent += converter.makeHtml(fs.readFileSync(file, 'utf8'));
}
} else {
if (!fs.existsSync(filePath)) {
console.log(`Include file ${filePath} not found.`);
return;
}
log(`Including file ${filePath}`);
extraContent += converter.makeHtml(fs.readFileSync(filePath, 'utf8'));
}
});
return extraContent;
}
async function copyAssets(assetsFolder, destinationFolder) {
log(`Copying assets from ${assetsFolder} to ${destinationFolder}`);
const ncp = require('ncp').ncp;
const ncp2 = require('util').promisify(ncp);
try {
await ncp2(assetsFolder + '/images/', destinationFolder + '/images');
await ncp2(assetsFolder + '/css/', destinationFolder + '/css');
await ncp2(assetsFolder + '/js/', destinationFolder + '/js');
} catch (e) {
console.error(e);
process.exit(1);
}
}
module.exports = { generate };