`;
// Move all elements out of the top level body and into a subelement
const body = dom.window.document.getElementsByTagName("body")[0];
const bp = body.parentNode;
div = dom.window.document.createElement("div");
div.setAttribute("class", "body");
let sawFirstTable = false;
for (const e of body.childNodes) {
body.removeChild(e);
if (div.childNodes.length === 0 && e.tagName === 'P') {
// update title element to
const newTitle = document.createElement('h2');
newTitle.setAttribute("class", "uaxtitle");
newTitle.appendChild(document.createTextNode(e.textContent));
div.appendChild(newTitle);
} else {
if (!sawFirstTable && e.tagName === 'TABLE') {
// Update first table to simple width=90%
e.setAttribute("class", "simple");
e.setAttribute("width", "90%");
sawFirstTable = true;
}
div.appendChild(e);
}
}
body.appendChild(header);
body.appendChild(div);
// now, fix all links from ….md#… to ….html#…
for (const e of dom.window.document.getElementsByTagName("a")) {
const href = e.getAttribute("href");
let m;
if ((m = /^(.*)\.md#(.*)$/.exec(href))) {
e.setAttribute("href", `${m[1]}.html#${m[2]}`);
} else if ((m = /^(.*)\.md$/.exec(href))) {
e.setAttribute("href", `${m[1]}.html`);
}
}
// OK, done munging the DOM, write it out.
console.log(`Writing ${outfile}`);
// TODO: assume that DOCTYPE is not written.
await fs.writeFile(outfile, `\n` + dom.serialize());
return outfile;
}
async function fixall() {
outbox = "./dist";
// TODO: move source file copy into JavaScript?
// srcbox = '../../../docs/ldml';
const fileList = (await fs.readdir(outbox))
.filter((f) => /\.md$/.test(f))
.map((f) => path.join(outbox, f));
return Promise.all(fileList.map(renderit));
}
fixall().then(
(x) => console.dir(x),
(e) => {
console.error(e);
process.exitCode = 1;
}
);