1
0
mirror of https://github.com/twirl/The-API-Book.git synced 2025-01-05 10:20:22 +02:00
The-API-Book/build.mjs

79 lines
2.4 KiB
JavaScript
Raw Normal View History

2021-11-15 20:28:32 +02:00
import { resolve as pathResolve } from 'path';
import templates from './src/templates.js';
2021-12-09 21:22:00 +02:00
import { init, plugins } from '@twirl/book-builder';
2022-07-06 23:41:03 +02:00
import { readFileSync, writeFileSync } from 'fs';
2021-11-15 20:28:32 +02:00
const l10n = {
en: JSON.parse(readFileSync('./src/en/l10n.json', 'utf-8')),
ru: JSON.parse(readFileSync('./src/ru/l10n.json', 'utf-8'))
};
const langsToBuild = (process.argv[2] &&
process.argv[2].split(',').map((s) => s.trim())) || ['ru', 'en'];
const targets = (
2022-07-06 23:41:03 +02:00
(process.argv[3] && process.argv[3].split(',')) || [
'html',
'pdf',
'epub',
'landing'
]
2021-11-15 20:28:32 +02:00
).reduce((targets, arg) => {
targets[arg.trim()] = true;
return targets;
}, {});
console.log(`Building langs: ${langsToBuild.join(', ')}`);
langsToBuild.forEach((lang) => {
init({
l10n: l10n[lang],
basePath: pathResolve(`src`),
path: pathResolve(`src/${lang}/clean-copy`),
templates,
pipeline: {
css: {
beforeAll: [
plugins.css.backgroundImageDataUri,
plugins.css.fontFaceDataUri
]
},
ast: {
preProcess: [
plugins.ast.h3ToTitle,
plugins.ast.h5Counter,
plugins.ast.aImg,
plugins.ast.imgSrcResolve,
plugins.ast.ref,
plugins.ast.ghTableFix
]
},
htmlSourceValidator: {
validator: 'WHATWG',
ignore: ['heading-level', 'no-raw-characters', 'wcag/h37']
},
html: {
postProcess: [plugins.html.imgDataUri]
}
}
}).then((builder) => {
Object.keys(targets).forEach((target) => {
2022-07-06 23:41:03 +02:00
if (target !== 'landing') {
builder.build(
target,
pathResolve('docs', `${l10n[lang].file}.${lang}.${target}`)
);
} else {
const landingHtml = templates.landing(
builder.structure,
l10n[lang],
lang
);
writeFileSync(
pathResolve('docs', l10n[lang].landingFile),
landingHtml
);
}
2021-11-15 20:28:32 +02:00
});
});
});