2012-08-08 18:56:56 +00:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
const marked = require('marked');
|
|
|
|
const path = require('path');
|
2012-08-08 18:56:56 +00:00
|
|
|
|
|
|
|
module.exports = toHTML;
|
|
|
|
|
|
|
|
function toHTML(input, filename, template, cb) {
|
2020-11-23 18:21:51 +00:00
|
|
|
const lexed = marked.lexer(input);
|
|
|
|
fs.readFile(template, 'utf8', (er, template) => {
|
2012-08-08 18:56:56 +00:00
|
|
|
if (er) return cb(er);
|
|
|
|
render(lexed, filename, template, cb);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function render(lexed, filename, template, cb) {
|
|
|
|
// get the section
|
2020-11-23 18:21:51 +00:00
|
|
|
const section = getSection(lexed);
|
2012-08-08 18:56:56 +00:00
|
|
|
|
|
|
|
filename = path.basename(filename, '.md');
|
|
|
|
|
|
|
|
lexed = parseLists(lexed);
|
|
|
|
|
|
|
|
// generate the table of contents.
|
|
|
|
// this mutates the lexed contents in-place.
|
2020-11-23 18:21:51 +00:00
|
|
|
buildToc(lexed, filename, (er, toc) => {
|
2012-08-08 18:56:56 +00:00
|
|
|
if (er) return cb(er);
|
|
|
|
|
|
|
|
template = template.replace(/__FILENAME__/g, filename);
|
|
|
|
template = template.replace(/__SECTION__/g, section);
|
|
|
|
template = template.replace(/__TOC__/g, toc);
|
|
|
|
|
|
|
|
// content has to be the last thing we do with
|
|
|
|
// the lexed tokens, because it's destructive.
|
|
|
|
content = marked.parser(lexed);
|
|
|
|
template = template.replace(/__CONTENT__/g, content);
|
|
|
|
|
|
|
|
cb(null, template);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// just update the list item text in-place.
|
|
|
|
// lists that come right after a heading are what we're after.
|
|
|
|
function parseLists(input) {
|
2020-11-23 18:21:51 +00:00
|
|
|
let state = null;
|
|
|
|
let depth = 0;
|
|
|
|
const output = [];
|
2012-08-08 18:56:56 +00:00
|
|
|
output.links = input.links;
|
2020-11-23 18:21:51 +00:00
|
|
|
input.forEach((tok) => {
|
2012-08-08 18:56:56 +00:00
|
|
|
if (state === null) {
|
|
|
|
if (tok.type === 'heading') {
|
|
|
|
state = 'AFTERHEADING';
|
|
|
|
}
|
|
|
|
output.push(tok);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (state === 'AFTERHEADING') {
|
|
|
|
if (tok.type === 'list_start') {
|
|
|
|
state = 'LIST';
|
|
|
|
if (depth === 0) {
|
2020-11-23 18:21:51 +00:00
|
|
|
output.push({type: 'html', text: '<div class="signature">'});
|
2012-08-08 18:56:56 +00:00
|
|
|
}
|
|
|
|
depth++;
|
|
|
|
output.push(tok);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
state = null;
|
|
|
|
output.push(tok);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (state === 'LIST') {
|
|
|
|
if (tok.type === 'list_start') {
|
|
|
|
depth++;
|
|
|
|
output.push(tok);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (tok.type === 'list_end') {
|
|
|
|
depth--;
|
|
|
|
if (depth === 0) {
|
|
|
|
state = null;
|
2020-11-23 18:21:51 +00:00
|
|
|
output.push({type: 'html', text: '</div>'});
|
2012-08-08 18:56:56 +00:00
|
|
|
}
|
|
|
|
output.push(tok);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (tok.text) {
|
|
|
|
tok.text = parseListItem(tok.text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output.push(tok);
|
|
|
|
});
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function parseListItem(text) {
|
|
|
|
text = text.replace(/\{([^\}]+)\}/, '<span class="type">$1</span>');
|
2020-11-23 18:21:51 +00:00
|
|
|
// XXX maybe put more stuff here?
|
2012-08-08 18:56:56 +00:00
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// section is just the first heading
|
|
|
|
function getSection(lexed) {
|
2020-11-23 18:21:51 +00:00
|
|
|
const section = '';
|
|
|
|
for (let i = 0, l = lexed.length; i < l; i++) {
|
|
|
|
const tok = lexed[i];
|
2012-08-08 18:56:56 +00:00
|
|
|
if (tok.type === 'heading') return tok.text;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function buildToc(lexed, filename, cb) {
|
2020-11-23 18:21:51 +00:00
|
|
|
const indent = 0;
|
|
|
|
let toc = [];
|
|
|
|
let depth = 0;
|
|
|
|
lexed.forEach((tok) => {
|
2012-08-08 18:56:56 +00:00
|
|
|
if (tok.type !== 'heading') return;
|
|
|
|
if (tok.depth - depth > 1) {
|
2020-11-23 18:21:51 +00:00
|
|
|
return cb(new Error(`Inappropriate heading level\n${
|
|
|
|
JSON.stringify(tok)}`));
|
2012-08-08 18:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
depth = tok.depth;
|
2020-11-23 18:21:51 +00:00
|
|
|
const id = getId(`${filename}_${tok.text.trim()}`);
|
|
|
|
toc.push(`${new Array((depth - 1) * 2 + 1).join(' ')
|
|
|
|
}* <a href="#${id}">${
|
|
|
|
tok.text}</a>`);
|
|
|
|
tok.text += `<span><a class="mark" href="#${id}" ` +
|
|
|
|
`id="${id}">#</a></span>`;
|
2012-08-08 18:56:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
toc = marked.parse(toc.join('\n'));
|
|
|
|
cb(null, toc);
|
|
|
|
}
|
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
const idCounters = {};
|
2012-08-08 18:56:56 +00:00
|
|
|
function getId(text) {
|
|
|
|
text = text.toLowerCase();
|
|
|
|
text = text.replace(/[^a-z0-9]+/g, '_');
|
|
|
|
text = text.replace(/^_+|_+$/, '');
|
|
|
|
text = text.replace(/^([^a-z])/, '_$1');
|
|
|
|
if (idCounters.hasOwnProperty(text)) {
|
2020-11-23 18:21:51 +00:00
|
|
|
text += `_${++idCounters[text]}`;
|
2012-08-08 18:56:56 +00:00
|
|
|
} else {
|
|
|
|
idCounters[text] = 0;
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}
|