Skip to content

Commit aeb4769

Browse files
committed
Meta: Remove the non-strict mode from scripts/insert_warning.mjs
1 parent de139ce commit aeb4769

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

.github/workflows/publish-pr.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ jobs:
8585
}
8686
' > "$tmp"
8787
find result -name '*.html' -exec \
88-
node scripts/insert_warning.mjs --strict scripts/pr_preview_warning.html "$tmp" '{}' '+'
88+
node scripts/insert_warning.mjs scripts/pr_preview_warning.html "$tmp" '{}' '+'
8989
- name: Publish to gh-pages
9090
uses: JamesIves/[email protected]
9191
with:

scripts/insert_warning.mjs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,48 @@ import pathlib from 'node:path';
33
import { parseArgs } from 'node:util';
44
import { JSDOM, VirtualConsole } from 'jsdom';
55

6-
const { positionals: cliArgs, values: cliOpts } = parseArgs({
6+
const { positionals: cliArgs } = parseArgs({
77
allowPositionals: true,
8-
options: {
9-
strict: { type: 'boolean' },
10-
},
8+
options: {},
119
});
1210
if (cliArgs.length < 3) {
1311
const self = pathlib.relative(process.cwd(), process.argv[1]);
14-
console.error(`Usage: node ${self} [--strict] <template.html> <data.json> <file.html>...
12+
console.error(`Usage: node ${self} <template.html> <data.json> <file.html>...
1513
1614
{{identifier}} substrings in template.html are replaced from data.json, then
1715
the result is inserted at the start of the body element in each file.html.`);
1816
process.exit(64);
1917
}
2018

21-
const main = async (args, options) => {
19+
const main = async args => {
2220
const [templateFile, dataFile, ...files] = args;
23-
const { strict } = options;
2421

25-
// Evaluate the template and parse it into nodes for inserting.
26-
// Everything will be prepended to body elements except metadata elements,
27-
// which will be appended to head elements.
22+
// Substitute data into the template.
23+
const template = fs.readFileSync(templateFile, 'utf8');
24+
const { default: data } =
25+
await import(pathlib.resolve(dataFile), { with: { type: 'json' } });
26+
const formatErrors = [];
27+
const placeholderPatt = /[{][{](?:([\p{ID_Start}$_][\p{ID_Continue}$]*)[}][}]|.*?(?:[}][}]|(?=[{][{])|$))/gsu;
28+
const resolved = template.replaceAll(placeholderPatt, (m, name, i) => {
29+
if (!name) {
30+
const trunc = m.replace(/([^\n]{29}(?!$)|[^\n]{,29}(?=\n)).*/s, '$1…');
31+
formatErrors.push(Error(`bad placeholder at index ${i}: ${trunc}`));
32+
} else if (!Object.hasOwn(data, name)) {
33+
formatErrors.push(Error(`no data for ${m}`));
34+
}
35+
return data[name];
36+
});
37+
if (formatErrors.length > 0) throw AggregateError(formatErrors);
38+
39+
// Parse the template into DOM nodes for appending to page <head>s (metadata
40+
// such as <style> elements) or prepending to page <body>s (everything else).
2841
// https://html.spec.whatwg.org/multipage/dom.html#metadata-content-2
2942
const metadataNames =
3043
'base, link, meta, noscript, script, style, template, title'
3144
.toUpperCase()
3245
.split(', ');
33-
const template = fs.readFileSync(templateFile, 'utf8');
34-
const { default: data } =
35-
await import(pathlib.resolve(dataFile), { with: { type: 'json' } });
36-
const namePatt = /[{][{]([\p{ID_Start}$_][\p{ID_Continue}$]*)[}][}]/gu;
37-
const resolved = template.replaceAll(namePatt, (_, name) => {
38-
if (Object.hasOwn(data, name)) return data[name];
39-
if (strict) throw Error(`no data for {{${name}}}`);
40-
return '';
41-
});
46+
const insertDom = JSDOM.fragment(resolved);
4247
const headInserts = [], bodyInserts = [];
43-
let insertDom = JSDOM.fragment(resolved);
4448
for (const node of insertDom.childNodes) {
4549
if (metadataNames.includes(node.nodeName)) headInserts.push(node);
4650
else bodyInserts.push(node);
@@ -67,7 +71,7 @@ const main = async (args, options) => {
6771
if (failures.length > 0) throw AggregateError(failures);
6872
};
6973

70-
main(cliArgs, cliOpts).catch(err => {
74+
main(cliArgs).catch(err => {
7175
console.error(err);
7276
process.exit(1);
7377
});

0 commit comments

Comments
 (0)