generated from w3c/wai-resource-template
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtransformPattern.js
48 lines (39 loc) · 1.56 KB
/
transformPattern.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const path = require("path");
const formatForJekyll = require("./formatForJekyll");
const { rewriteSourcePath } = require("./rewritePath");
const { parse: parseHtml } = require("node-html-parser");
const rewriteElementPaths = require("./rewriteElementPaths");
const removeDuplicateMainTag = require("./removeDuplicateMainTag");
const removeConflictingCss = require("./removeConflictingCss");
const transformPattern = async (sourcePath, sourceContents) => {
const { sitePath, githubPath } = rewriteSourcePath(sourcePath);
const html = parseHtml(sourceContents);
const slug = path.basename(sitePath);
const title = html.querySelector("h1").innerHTML;
html.querySelector("h1").remove();
removeConflictingCss(html);
const findExamplesSection = () => {
const sectionElements = html.querySelectorAll("section");
for (const sectionElement of sectionElements) {
const h2 = sectionElement.querySelector("h2");
if (!h2) continue;
if (h2.textContent === "Example" || h2.textContent === "Examples") {
return sectionElement;
}
}
throw new Error(`Expected pattern ${slug} to have an Example(s) section`);
};
examplesSection = findExamplesSection();
examplesSection.classList.add("examples-section");
await rewriteElementPaths(html, { onSourcePath: sourcePath });
return formatForJekyll({
title,
sitePath,
githubPath,
content: removeDuplicateMainTag(html.querySelector("body").innerHTML),
enableSidebar: true,
head: html.querySelector("head"),
footer: "",
});
};
module.exports = transformPattern;