Skip to content

Commit

Permalink
Improve the error reporting in checkAudiences().
Browse files Browse the repository at this point in the history
  • Loading branch information
jyasskin committed Sep 29, 2024
1 parent 0889270 commit 296b672
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,37 @@
'required-sections': false,
},
preProcess: [
function checkAudiences() {
function checkAudiences(_conf, _doc, utils) {
// Ensure every principle has a data-audiences attribute with a
// space-separated list of the target audiences of that principle.
let errors = Array.from(document.querySelectorAll('div.practice')
).flatMap(practice => {
const audiences = practice.dataset.audiences?.split(/\s+/) ?? [];
if (audiences.length === 0) {
return `Missing data-audiences attribute in principle "${practice.textContent}".`;
}
const unknownAudiences = audiences.filter(
audience => !['websites', 'user-agents', 'api-designers'].includes(audience));
if (unknownAudiences.length > 0) {
return `Unknown audience "${unknownAudiences.join(', ')}" in principle "${practice.textContent}".`;
}
return [];
});
if (errors.length !== 0) {
throw new Error(errors.join('\n'));
for (const practice of document.querySelectorAll("div.practice")) {
const audiences = practice.dataset.audiences?.split(/\s+/) ?? [];
if (audiences.length === 0) {
utils.showError(
`Missing data-audiences attribute in principle.`,
{
title: "Missing data-audiences attribute.",
elements: [practice],
hint: 'Set it to a space-separated list of "websites", "user-agents", or "api-designers".',
}
);
continue;
}
const unknownAudiences = audiences.filter(
(audience) =>
!["websites", "user-agents", "api-designers"].includes(audience)
);
if (unknownAudiences.length > 0) {
const list = new Intl.ListFormat().format(unknownAudiences);
utils.showError(
`Unknown audience "${list}" in principle.`,
{
title: "Unknown audience in data-audience attribute.",
elements: [practice],
hint: `Remove ${list}.`,
}
);
}
}
}
],
Expand Down

0 comments on commit 296b672

Please sign in to comment.