Skip to content

Commit

Permalink
docs: use dockhand for static generation
Browse files Browse the repository at this point in the history
Drop Gatsby, use a simplified custom static site generator that uses GFM
and a template for extremely lightweight docs.
  • Loading branch information
ethomson committed Oct 15, 2020
1 parent dc288b6 commit 4f6fb7a
Show file tree
Hide file tree
Showing 16 changed files with 2,187 additions and 46,313 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ npm-debug.log
/test/packages/test-package/random-data.txt
/test/root
/node_modules/.bin
/docs/public/
/docs/.cache/
/docs/output/
/docs/node_modules/
/man/
/npmrc
Expand Down
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ mandocs: $(mandocs)

htmldocs:
cd docs && node ../bin/npm-cli.js install --legacy-peer-deps --no-audit && \
node ../bin/npm-cli.js run build:static >&2 && \
rm -rf .cache public/*js public/*json public/404* public/page-data public/manifest* public/*.map public/*.LICENSE.txt public/static
node ../bin/npm-cli.js run build >&2

docs: mandocs htmldocs

Expand All @@ -67,7 +66,7 @@ docs-clean:
.building_marked-man \
man \
docs/node_modules \
docs/public \
docs/output \
docs/.cache

## build-time tools for the documentation
Expand Down
5 changes: 5 additions & 0 deletions docs/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"github_repo": "npm/cli",
"github_branch": "latest",
"github_path": "docs/content"
}
5 changes: 0 additions & 5 deletions docs/content/commands/index.mdx

This file was deleted.

5 changes: 0 additions & 5 deletions docs/content/configuring-npm/index.mdx

This file was deleted.

7 changes: 0 additions & 7 deletions docs/content/index.mdx

This file was deleted.

5 changes: 0 additions & 5 deletions docs/content/using-npm/index.mdx

This file was deleted.

140 changes: 140 additions & 0 deletions docs/dockhand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#!/usr/bin/env node

const path = require('path');
const fs = require('fs');
const yaml = require('yaml');
const cmark = require('cmark-gfm');
const mkdirp = require('mkdirp');
const jsdom = require('jsdom');
const npm = require('../lib/npm.js')

const config = require('./config.json');

const docsRoot = __dirname;
const inputRoot = path.join(docsRoot, 'content');
const outputRoot = path.join(docsRoot, 'output');

const template = fs.readFileSync('template.html').toString();

walk(inputRoot);

function walk(root, dirRelative) {
const dirPath = dirRelative ? path.join(root, dirRelative) : root;

fs.readdirSync(dirPath).forEach((childFilename) => {
const childRelative = dirRelative ? path.join(dirRelative, childFilename) : childFilename;
const childPath = path.join(root, childRelative);

if (fs.lstatSync(childPath).isDirectory()) {
walk(root, childRelative);
}
else {
translate(childRelative);
}
});
}

function translate(childPath) {
const inputPath = path.join(inputRoot, childPath);

if (!inputPath.match(/\.md$/)) {
console.log(`warning: unknown file type ${inputPath}, ignored`);
return;
}

const outputPath = path.join(outputRoot, childPath.replace(/\.md$/, '.html'));

let md = fs.readFileSync(inputPath).toString();
let frontmatter = { };

// Take the leading frontmatter out of the markdown
md = md.replace(/^---\n([\s\S]+)\n---\n/, (header, fm) => {
frontmatter = yaml.parse(fm, 'utf8');
return '';
});

// Replace any tokens in the source
md = md.replace(/@VERSION@/, npm.version);

// Render the markdown into an HTML snippet using a GFM renderer.
const content = cmark.renderHtmlSync(md, {
'smart': true,
'githubPreLang': true,
'strikethroughDoubleTilde': true,
'unsafe': false,
extensions: {
'table': true,
'strikethrough': true,
'tagfilter': true,
'autolink': true
}
});

// Inject this data into the template, using a mustache-like
// replacement scheme.
const html = template.replace(/\{\{\s*([\w\.]+)\s*\}\}/g, (token, key) => {
switch (key) {
case 'content':
return content;
case 'path':
return childPath;
case 'url_path':
return encodeURI(childPath);

case 'title':
case 'section':
case 'description':
return frontmatter[key];

case 'config.github_repo':
case 'config.github_branch':
case 'config.github_path':
return config[key.replace(/^config\./, '')];

default:
console.log(`warning: unknown token '${token}' in ${inputPath}`);
return '';
}
console.log(key);
return key;
});

const dom = new jsdom.JSDOM(html);
const document = dom.window.document;

// Rewrite relative URLs in links and image sources to be relative to
// this file; this is for supporting `file://` links. HTML pages need
// suffix appended.
const links = [
{ tag: 'a', attr: 'href', suffix: '.html' },
{ tag: 'img', attr: 'src' }
];

for (let linktype of links) {
for (let tag of document.querySelectorAll(linktype.tag)) {
let url = tag.getAttribute(linktype.attr);

if (url.startsWith('/')) {
const childDepth = childPath.split('/').length - 1;
const prefix = childDepth > 0 ? '../'.repeat(childDepth) : './';

url = url.replace(/^\//, prefix);

if (linktype.suffix) {
url += linktype.suffix;
}

tag.setAttribute(linktype.attr, url);
}
}
}

const output = dom.serialize();

mkdirp.sync(path.dirname(outputPath));
fs.writeFileSync(outputPath, output);
}

function debug(str) {
console.log(str);
}
28 changes: 0 additions & 28 deletions docs/gatsby-config.js

This file was deleted.

Loading

0 comments on commit 4f6fb7a

Please sign in to comment.