Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.

Commit 4ebe8d1

Browse files
committed
add build-docs script
1 parent 3909066 commit 4ebe8d1

File tree

16 files changed

+408
-832
lines changed

16 files changed

+408
-832
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,6 @@ xcuserdata
7575
*.pem
7676
*.env.json
7777
*.env
78+
79+
# built docs
80+
docs/dist

build-docs.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
const fs = require('fs/promises')
2+
const path = require('path')
3+
const marked = require('marked')
4+
const fm = require('front-matter')
5+
6+
function tocHTML (toc) {
7+
let html = '<ul>\n'
8+
for (const li of toc) {
9+
html += '<li>\n'
10+
html += `<div>\n<a href="#${li.slug}">${li.text}</a>\n</div>\n`
11+
if (li.children && li.children.length > 0) {
12+
html += tocHTML(li.children)
13+
}
14+
html += '</li>\n'
15+
}
16+
html += '</ul>\n'
17+
18+
return html
19+
}
20+
21+
function markdownTOC (markdown) {
22+
const tokens = marked.lexer(markdown)
23+
const slugger = new marked.Slugger()
24+
const toc = []
25+
let currentHeading
26+
let ignoreFirst = true
27+
for (const token of tokens) {
28+
if (token.type === 'heading') {
29+
if (token.depth === 1) {
30+
if (ignoreFirst) {
31+
ignoreFirst = false
32+
continue
33+
}
34+
currentHeading = {
35+
text: token.text,
36+
slug: slugger.slug(token.text),
37+
children: []
38+
}
39+
toc.push(currentHeading)
40+
} else if (token.depth === 2) {
41+
if (!currentHeading) {
42+
continue
43+
}
44+
currentHeading.children.push({
45+
text: token.text,
46+
slug: slugger.slug(token.text)
47+
})
48+
}
49+
}
50+
}
51+
52+
return {
53+
toc: tocHTML(toc),
54+
html: marked.parser(tokens)
55+
}
56+
}
57+
58+
function createHTML (header, footer, text) {
59+
const { attributes, body } = fm(text)
60+
for (const prop in attributes) {
61+
header = header.replace(new RegExp(`%\\(${prop}\\)s`, 'ig'), attributes[prop])
62+
footer = footer.replace(new RegExp(`%\\(${prop}\\)s`, 'ig'), attributes[prop])
63+
}
64+
65+
const { toc, html } = markdownTOC(body)
66+
67+
header = header.replace(/%\(toc_html\)s/ig, toc)
68+
69+
return header + html + footer
70+
}
71+
72+
async function createDocs () {
73+
const docs = path.join(__dirname, 'docs')
74+
const dist = path.join(docs, 'dist')
75+
76+
await fs.rmdir(dist, { recursive: true })
77+
await fs.mkdir(dist)
78+
79+
const header = await fs.readFile(path.join(docs, 'branding', 'header.html.in'), { encoding: 'utf8' })
80+
const footer = await fs.readFile(path.join(docs, 'branding', 'footer.html.in'), { encoding: 'utf8' })
81+
const files = await fs.readdir(docs)
82+
for (const file of files) {
83+
if (!file.endsWith('.md')) {
84+
continue
85+
}
86+
const text = await fs.readFile(path.join(docs, file), { encoding: 'utf8' })
87+
const html = createHTML(header, footer, text)
88+
89+
await fs.writeFile(path.join(dist, file.replace(/md$/, 'html')), html)
90+
}
91+
92+
const dest = path.join(dist, 'media')
93+
const src = path.join(docs, 'branding', 'media')
94+
await fs.mkdir(dest)
95+
await fs.mkdir(path.join(dest, 'css'))
96+
await fs.mkdir(path.join(dest, 'img'))
97+
await fs.copyFile(path.join(src, 'css', 'style.css'), path.join(dest, 'css', 'style.css'))
98+
await fs.copyFile(path.join(src, 'img', 'logo.svg'), path.join(dest, 'img', 'logo.svg'))
99+
}
100+
101+
createDocs()

docs/branding/footer.html.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
</div><!-- end #content -->
2+
13
<script type="text/javascript" charset="utf-8">
24
$(function() {
35
var headerHeight = $("#header").height();

docs/branding/header.html.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<title>%(title)s</title>
55
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6-
<link rel="stylesheet" type="text/css" href="%(mediaroot)s/css/restdown.css">
6+
<link rel="stylesheet" type="text/css" href="media/css/style.css">
77
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
88
</head>
99
<body>
@@ -30,3 +30,5 @@
3030
</span>
3131
%(toc_html)s
3232
</div>
33+
34+
<div id="content">

0 commit comments

Comments
 (0)