Skip to content

Commit

Permalink
#2334 Dropdown to select docs for previous versions
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-ward authored and StephanHoyer committed May 16, 2022
1 parent 1b89cbd commit 6c8a01a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
4 changes: 2 additions & 2 deletions docs/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
29 changes: 26 additions & 3 deletions scripts/generate-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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)}\\))`
Expand Down Expand Up @@ -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"),
Expand Down

0 comments on commit 6c8a01a

Please sign in to comment.