-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathbuild.js
More file actions
230 lines (171 loc) · 6.45 KB
/
build.js
File metadata and controls
230 lines (171 loc) · 6.45 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*!
* This source file is part of the open source project
* ExpressionEngine User Guide (https://github.com/ExpressionEngine/ExpressionEngine-User-Guide)
*
* @link https://expressionengine.com/
* @copyright Copyright (c) 2003-2019, EllisLab, Inc. (https://ellislab.com)
* @license https://expressionengine.com/license Licensed under Apache License, Version 2.0
*/
const Fs = require('fs')
const Path = require('path')
const gulp = require('gulp')
const glob = require('glob')
const Through2 = require('through2')
const FrontMatter = require('front-matter')
const replaceExt = require('replace-ext')
const Yaml = require('js-yaml')
const RenderMd = require('./md-render.js')
const Logger = require('./logger.js')
const CONFIG = require('./config.js')
const { getSlugger, renderTemplate, getRelativeRootFromPage, relFromSource, bsToFs } = require('./utility.js')
// Used for saving build information from other files
global.BuildInfo = { }
// -------------------------------------------------------------------
// Build
module.exports = () => {
Logger.reset()
BuildInfo = { foundFiles: [], pages: {} }
var masterToc = getMasterToc()
// Get the page template
let themeTemplate = GLOBAL_buildThemeFile || CONFIG.pageTemplatePath
const pageTemplate = Fs.readFileSync(themeTemplate, { encoding: 'utf8' })
// Get all the markdown files
return gulp.src(CONFIG.sourceDir + '/**/*.md')
// Build Each File
.pipe(Through2.obj((file, _, cb) => {
let pageId = relFromSource(file.path)
console.time(pageId.green)
let pageFM = FrontMatter(file.contents.toString())
let relPath = getRelativeRootFromPage(file.path)
BuildInfo.pages[pageId] = { inPageLinks: [], headingSlugs: [] }
let currentPageInfo = {
pageId: pageId,
path: file.path,
relPath: relPath,
codeLang: pageFM.attributes.lang || null,
slugify: getSlugger()
}
let pageContent = pageFM.body
// Use the first h1 in the page for the title
let pageTitle = /^# *(?!#)(.*)/gm.exec(pageContent)
pageTitle = pageTitle ? pageTitle[1] : 'Doc Page'
// Render the markdown
let pageHtml = RenderMd(pageContent, currentPageInfo)
// Render the template
let templateVariables = {
...CONFIG.customVariables,
page_title: pageTitle,
page_content: pageHtml,
page_path: pageId,
root_dir: relPath,
getting_started_toc: masterToc.make(file.path, relPath, "getting_started_toc"),
the_fundamentals_toc: masterToc.make(file.path, relPath, "the_fundamentals_toc"),
advanced_usage_toc: masterToc.make(file.path, relPath, "advanced_usage_toc"),
best_practices_toc: masterToc.make(file.path, relPath, "best_practices_toc"),
community_toc: masterToc.make(file.path, relPath, "community_toc"),
}
let page = renderTemplate(pageTemplate, templateVariables, currentPageInfo)
file.contents = Buffer.from(page)
file.path = replaceExt(file.path, '.html')
console.timeEnd(pageId.green)
cb(null, file)
}))
// Output the the new pages
.pipe(gulp.dest(CONFIG.outputDir))
// Log any messages or warnings when done
.on('end', () => {
let allFiles = glob.sync(CONFIG.sourceDir + '/**/*.*', { ignore: [CONFIG.tocPath] }).map(e => Path.resolve(e))
let foundFiles = BuildInfo.foundFiles.map(e => Path.resolve(e))
// Warn for any unused files
allFiles.filter(i => foundFiles.indexOf(i) < 0).forEach(e => Logger.warn('Unused file:', relFromSource(e)))
// Check for broken in-page links
for (let [pagePath, page] of Object.entries(BuildInfo.pages)) {
for (let link of page.inPageLinks) {
let outPagePath = relFromSource(link.page)
let pageToCheck = BuildInfo.pages[outPagePath]
if (pageToCheck) {
if (!pageToCheck.headingSlugs.includes(link.anchor.trim().replace(/^#/, ''))) {
Logger.warn('Link does not point to valid anchor', `[${link.text}](${outPagePath == pagePath ? '' : outPagePath}${link.anchor})`, pagePath)
}
}
}
}
// Display any warnings
Logger.showMessages()
console.log('\nFinished Build! Did you remember to update Authors and Contributors?'.green)
})
}
// -------------------------------------------------------------------
// Get master toc
// -------------------------------------------------------------------
function getMasterToc() {
let toc
const recurse = (item) => {
if (item.href) {
let itemPath = Path.resolve(Path.join(CONFIG.sourceDir, item.href))
// Warn if the toc is linking to a file that does not exist
if (!Fs.existsSync(itemPath)) {
Logger.warn('Unknown file in toc:', relFromSource(itemPath), '_toc.yml')
}
BuildInfo.foundFiles.push(itemPath)
item.href="/?originalUrl=https%3A%2F%2Fgithub.com%2FrelFromSource(replaceExt(itemPath%2C%2520%26%23039%3B.html%26%23039%3B))%253C%2Fdiv">
}
if (item.items) {
for (let child of item.items)
recurse(child)
}
}
return {
make: (page, relPath, tocSection) => {
page = relFromSource(replaceExt(page, '.html'))
const makeItem = (item) => {
let isSelected = (item.href && item.href="/?originalUrl=https%3A%2F%2Fgithub.com%2F%3D%2520page)%253C%2Fdiv">
let isCurrentPage = isSelected
// Create any sub item children
let subItems = ''
if (item.items) {
subItems += '<ul>'
for (let child of item.items) {
let newChild = makeItem(child)
isSelected = isSelected != true ? newChild.isSelected : isSelected
subItems += newChild.html
}
subItems += '</ul>'
}
let itemHTml = `<li ${isCurrentPage ? 'data-active-page="true"' : ''} class="${isSelected ? 'active' : ''}">`
itemHTml += `<a ${item.href ? `href="/?originalUrl=https%3A%2F%2Fgithub.com%2F%26quot%3B%24%257Bitem.href%2520%3F%2520bsToFs(Path.join(relPath%2520%2B%2520item.href))%2520%3A%2520%26%23039%3B%26%23039%3B%7D%26quot%3B%2560%2520%3A%2520%26%23039%3B%26%23039%3B%7D%26gt%3B%24%7Bitem.name%7D%26lt%3B%2Fa%26gt%3B%2560%253C%2Fdiv">
itemHTml += subItems
return { html: itemHTml + '</li>', isSelected: isSelected }
}
let html = ''
let tocSectionPath;
switch(tocSection) {
case 'getting_started_toc':
tocSectionPath = 'docs/toc_sections/_getting_started_toc.yml';
break;
case 'the_fundamentals_toc':
tocSectionPath = 'docs/toc_sections/_the_fundamentals_toc.yml';
break;
case 'advanced_usage_toc':
tocSectionPath = 'docs/toc_sections/_advanced_usage_toc.yml';
break;
case 'best_practices_toc':
tocSectionPath = 'docs/toc_sections/_best_practices_toc.yml';
break;
case 'community_toc':
tocSectionPath = 'docs/toc_sections/_community_toc.yml';
break;
}
try {
toc = Yaml.safeLoad(Fs.readFileSync(tocSectionPath, 'utf8'))
} catch (e) {
throw 'Error reading _toc.yml:\n' + e
}
for (let item of toc)
recurse(item)
for (let item of toc)
html += makeItem(item).html
return '<ul>' + html + '</ul>'
}
}
}
You can’t perform that action at this time.