Skip to content

Commit

Permalink
Editorial: Fix doc build.js for localized page
Browse files Browse the repository at this point in the history
Fix build script to replace relative path in head
and tail content. Because localized page uses sub
dir so those failed to import playground.js and
etc.
  • Loading branch information
nabe1653 authored and ptomato committed Feb 21, 2024
1 parent 6ff7f56 commit 0c44b87
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions docs/buildDocs.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,21 @@ class CustomRenderer extends marked.Renderer {
}
}

async function render(markdownFile, head, tail) {
async function render(markdownFile, head, tail, basePath) {
await mkdirp('../out/docs/assets');
await mkdirp(path.join('../out/docs', path.dirname(markdownFile)));
let markdownText = await fs.readFile(markdownFile, { encoding });

// replace relative path in header/footer for sub dir pages. this expects '.' is doc root
const replacePath = (html) => {
const replacingPathPart = [/(href=)"\./g, /(src=)"\./g];
let relativePath = path.relative(basePath, '.');
relativePath = relativePath === '' ? '.' : relativePath;
return replacingPathPart.reduce((pre, cur) => pre.replace(cur, `$1"${relativePath}`), html);
};
const _head = replacePath(head);
const _tail = replacePath(tail);

// Resolve transcludes
const lines = await Promise.all(
markdownText.split('\n').map(async (line) => {
Expand All @@ -110,7 +120,7 @@ async function render(markdownFile, head, tail) {
markdownText = lines.join('\n');

const renderer = new CustomRenderer();
let htmlText = head + marked.parse(markdownText, { renderer }) + tail;
let htmlText = _head + marked.parse(markdownText, { renderer }) + _tail;
htmlText = htmlText.replace(/^<!-- toc -->$/m, () => renderer.renderTOC());

const htmlFile = path.resolve('../out/docs', markdownFile.replace(/\.md$/, '') + '.html');
Expand All @@ -122,13 +132,21 @@ async function go() {
try {
const head = await fs.readFile('head.html.part', { encoding });
const tail = await fs.readFile('tail.html.part', { encoding });

// read files and return content with sub dir structure
const basePaths = ['.', './ja', './zh_CN'];
const files = await Promise.all(
basePaths.map(async (base) => {
return (await fs.readdir(base)).map((file) => ({
base,
file: `${base}/${file}`
}));
})
).then((blocks) => blocks.flat());

// copy or render /docs/* to /out/docs/
await Promise.all(
[
...(await fs.readdir('.')),
...(await fs.readdir('ja')).map((x) => 'ja/' + x),
...(await fs.readdir('zh_CN')).map((x) => 'zh_CN/' + x)
].map((file) => {
files.map(({ base, file }) => {
switch (path.extname(file)) {
// copy files *.css, *.html, *.svg to /out/docs
case '.css':
Expand All @@ -137,7 +155,7 @@ async function go() {
return fs.copyFile(file, path.resolve('../out/docs/' + file));
// convert files *.md to /out/docs/*.html
case '.md':
return render(file, head, tail);
return render(file, head, tail, base);
// skip remaining files
default:
return new Promise((resolve) => resolve());
Expand Down

0 comments on commit 0c44b87

Please sign in to comment.