"use strict"
require("./_improve-rejection-crashing.js")
const {promises: fs} = require("fs")
const path = require("path")
const {promisify} = require("util")
const {marked} = require("marked")
const rimraf = promisify(require("rimraf"))
const {execFileSync} = require("child_process")
const escapeRegExp = require("escape-string-regexp")
const HTMLMinifier = require("html-minifier")
const upstream = require("./_upstream")
const version = require("../package.json").version
const r = (file) => path.resolve(__dirname, "..", file)
const metaDescriptionRegExp = //m
// Minify our docs.
const htmlMinifierConfig = {
collapseBooleanAttributes: true,
collapseWhitespace: true,
conservativeCollapse: true,
continueOnParseError: true,
minifyCss: {
compatibility: "ie9",
},
minifyJs: true,
minifyUrls: true,
preserveLineBreaks: true,
removeAttributeQuotes: true,
removeCdatasectionsFromCdata: true,
removeComments: true,
removeCommentsFromCdata: true,
removeEmptyAttributes: true,
removeOptionalTags: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true,
}
async function generate() {
return (await makeGenerator()).generate()
}
async function makeGenerator() {
await rimraf(r("dist"))
const [guides, methods, layout, pkg] = await Promise.all([
fs.readFile(r("docs/nav-guides.md"), "utf-8"),
fs.readFile(r("docs/nav-methods.md"), "utf-8"),
fs.readFile(r("docs/layout.html"), "utf-8"),
fs.readFile(r("package.json"), "utf-8"),
fs.mkdir(r("dist"), {recursive: true}),
])
const version = JSON.parse(pkg).version
// Make sure we have the latest archive.
execFileSync("git", [
"fetch", "--depth=1",
upstream.fetch.remote, "gh-pages",
])
// Set up archive directories
execFileSync("git", [
"checkout", `${upstream.fetch.remote}/gh-pages`,
"--", "archive",
])
await fs.rename(r("archive"), r("dist/archive"))
await fs.mkdir(r(`dist/archive/v${version}`), {recursive: true})
// Tell Git to ignore our changes - it's no longer there.
execFileSync("git", ["add", "archive"])
// add version selector
const docsSelect = await archiveDocsSelect()
return new Generator({
version,
guides,
methods,
layout: layout.replaceAll("[archive-docs]", docsSelect)
})
}
async function getArchiveDirs() {
const dirs = await fs.readdir(r("dist/archive"))
const ver = "v" + version;
if (dirs.every((dir) => ver !== dir)) dirs.push(ver);
return dirs.reverse();
}
async function archiveDocsSelect() {
const archiveDirs = await getArchiveDirs()
var options = archiveDirs
.map((ad) => ``)
.join("")
return ``
}
function encodeHTML (str) {
const charsToEncode = /[&"'<>]/g
const encodeTo = {
"&": "&",
"\"": """,
"'": "'",
"<": "<",
">": ">",
}
return str.replace(charsToEncode, function(char) { return encodeTo[char] })
}
function extractMetaDescription(markdown) {
var match = markdown.match(metaDescriptionRegExp)
if (match) {
return encodeHTML(match[1])
}
return "Mithril.js Documentation"
}
class Generator {
constructor(opts) {
this._version = opts.version
this._guides = opts.guides
this._methods = opts.methods
this._layout = opts.layout
}
async compilePage(file, markdown) {
file = path.basename(file)
const link = new RegExp(
`([ \t]*)(- )(\\[.+?\\]\\(${escapeRegExp(file)}\\))`
)
const src = link.test(this._guides) ? this._guides : this._methods
const metaDescription = extractMetaDescription(markdown)
let body = markdown.replace(metaDescriptionRegExp, "")
// fix pipes in code tags
body = body.replace(/`((?:\S| -> |, )+)(\|)(\S+)`/gim,
(match, a, b, c) =>
`${(a + b + c).replace(/\|/g, "|")}`
)
// inject menu
body = body.replace(
/(^# .+?(?:\r?\n){2,}?)(?:(-(?:.|\r|\n)+?)((?:\r?\n){2,})|)/m,
(match, title, nav) => {
if (!nav) {
return title + src.replace(link, "$1$2**$3**") + "\n\n"
}
return title + src.replace(link, (match, space, li, link) =>
`${space}${li}**${link}**\n${
nav.replace(/(^|\n)/g, `$1\t${space}`)
}`
) + "\n\n"
}
)
// fix links
body = body.replace(/(\]\([^\)]+)(\.md)/gim, (match, path, extension) =>
path + ((/http/).test(path) ? extension : ".html")
)
const markedHtml = marked(body)
const title = body.match(/^#\s+([^\n\r]+)/m) || []
let result = this._layout
if (title[1]) {
result = result.replace(
/