#2334 Dropdown to select docs for previous versions

This commit is contained in:
mike-ward 2018-12-29 22:14:11 -05:00 committed by Stephan Hoyer
parent 99e038046b
commit 8ef7844f5a
2 changed files with 28 additions and 5 deletions

View file

@ -9,11 +9,11 @@
<link rel="icon" type="image/png" sizes="32x32" href="favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<body onload="window.requestAnimationFrame(function(){document.getElementById('archive-docs').selectedIndex = 0})" /* handle back navigation */>
<header>
<section>
<a class="hamburger" href="javascript:;"></a>
<h1><img src="logo.svg"> Mithril <small>[version]</small></h1>
<h1><img src="logo.svg"> Mithril [archive-docs]</h1>
<nav>
<a href="index.html">Guide</a>
<a href="api.html">API</a>

View file

@ -9,6 +9,7 @@ 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)
@ -70,9 +71,31 @@ async function makeGenerator() {
// Tell Git to ignore our changes - it's no longer there.
execFileSync("git", ["add", "archive"])
return new Generator({version, guides, methods, layout})
// 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) => `<option>${ad}</option>`)
.join("")
return `<select id="archive-docs" onchange="location.href='archive/' + this.value + '/index.html'">${options}</select>`
}
class Generator {
constructor(opts) {
this._version = opts.version
@ -81,7 +104,7 @@ class Generator {
this._layout = opts.layout
}
compilePage(file, markdown) {
async compilePage(file, markdown) {
file = path.basename(file)
const link = new RegExp(
`([ \t]*)(- )(\\[.+?\\]\\(${escapeRegExp(file)}\\))`
@ -179,7 +202,7 @@ class Generator {
}
else {
let html = await fs.readFile(file, "utf-8")
if (file.endsWith(".md")) html = this.compilePage(file, html)
if (file.endsWith(".md")) html = await this.compilePage(file, html)
const minified = HTMLMinifier.minify(html, htmlMinifierConfig)
await archived(
relative.replace(/\.md$/, ".html"),