Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"checkJs": true,
"strict": false
},
"include": ["lib/**/*.js"]
}
75 changes: 45 additions & 30 deletions lib/builder.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,50 @@
const fs = require('fs/promises');
const {marked,set_active_link} = require('./util');
import fs from "node:fs/promises";

const template_replace = (template,replacements) => template.replace(/@([a-z]+)/g,(match,word) => replacements[word] || match);
import { marked, set_active_link } from "./util.js";

function page_name(str)
{
return str
.replace(/^ch[0-9]+-[0-9]+-/,'')
.replace(/-/g,' ')
.split(' ').map(word => word[0].toUpperCase() + word.slice(1))
.join(' ').split('.').slice(0,-1).join('.');
}
const template_replace = (template, replacements) =>
template.replace(/@([a-z]+)/g, (match, word) => replacements[word] || match);

async function build_page(file,options)
{
const template = options && options.template || '';
const content = await fs.readFile(file,'utf8');
const title = page_name(file.split('/').pop());
set_active_link(options.active_link || file.split('/').pop());
return template_replace(template,{title,body: marked(content),summary: marked(options.summary || '')});
}
/**
* @param {string} str
* @returns
*/
function page_name(str) {
return str
.replace(/^ch[0-9]+-[0-9]+-/, "")
.replace(/-/g, " ")
.split(" ")
.map((word) => word[0].toUpperCase() + word.slice(1))
.join(" ")
.split(".")
.slice(0, -1)
.join(".");
}

async function link_page(file,previous,next)
{
const content = await fs.readFile(file,'utf8');
let index = content.indexOf('<div class="footnote">');
if (index === -1)
index = content.lastIndexOf('</article>');
if (index === -1)
console.warning(`Unable to link ${file}`);
return index === -1 ? content : content.slice(0,index) + '<div class="prevnext">' + (previous?`<a href="${previous}" class="prev"></a>`:'<div></div>') + (next?`<a href="${next}" class="next"></a></div>`:'<div></div>') + content.slice(index);
}
async function build_page(file, options) {
const template = (options && options.template) || "";
const content = await fs.readFile(file, "utf8");
const title = page_name(file.split("/").pop());
set_active_link(options.active_link || file.split("/").pop());
return template_replace(template, {
title,
body: marked(content),
summary: marked(options.summary || ""),
});
}

module.exports = {build_page,link_page};
async function link_page(file, previous, next) {
const content = await fs.readFile(file, "utf8");
let index = content.indexOf('<div class="footnote">');
if (index === -1) index = content.lastIndexOf("</article>");
if (index === -1) console.warn(`Unable to link ${file}`);
return index === -1
? content
: content.slice(0, index) +
'<div class="prevnext">' +
(previous ? `<a href="${previous}" class="prev"></a>` : "<div></div>") +
(next ? `<a href="${next}" class="next"></a></div>` : "<div></div>") +
content.slice(index);
}

export { build_page, link_page };
Loading