|
| 1 | +import syntaxHighlight from "@11ty/eleventy-plugin-syntaxhighlight"; |
| 2 | +import attributes from "./src/_data/pdfjsAttributes.js"; |
| 3 | + |
| 4 | +const slugify = (text) => |
| 5 | + text.toLowerCase().replace(/<[^>]+>/g, "").replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, ""); |
| 6 | + |
| 7 | +const escapeHtml = (text) => |
| 8 | + String(text).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """); |
| 9 | + |
| 10 | +export default function (eleventyConfig) { |
| 11 | + eleventyConfig.addPlugin(syntaxHighlight, { preAttributes: { tabindex: 0 } }); |
| 12 | + |
| 13 | + eleventyConfig.addPassthroughCopy("src/assets"); |
| 14 | + eleventyConfig.addPassthroughCopy("src/pdfjs-viewer/samples"); |
| 15 | + eleventyConfig.addPassthroughCopy("src/pdfjs-viewer/docs.js"); |
| 16 | + // The docs run the exact package version they describe, served from this site. |
| 17 | + eleventyConfig.addPassthroughCopy({ "node_modules/@missing-elements/pdfjs-viewer/dist": "pdfjs-viewer/vendor" }); |
| 18 | + |
| 19 | + let markdown; |
| 20 | + eleventyConfig.amendLibrary("md", (md) => { |
| 21 | + markdown = md; |
| 22 | + |
| 23 | + // Heading ids, so the table of contents and deep links work. |
| 24 | + md.core.ruler.push("heading_ids", (state) => { |
| 25 | + state.tokens.forEach((token, i) => { |
| 26 | + if (token.type === "heading_open" && !token.attrGet("id")) { |
| 27 | + token.attrSet("id", slugify(state.tokens[i + 1].content)); |
| 28 | + } |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + // A fence flagged with `try` (```html try, ```js try, ```js try=fresh) gets a Try button |
| 33 | + // in the browser that applies the snippet to the preview viewer. |
| 34 | + const fence = md.renderer.rules.fence; |
| 35 | + md.renderer.rules.fence = (tokens, idx, options, env, self) => { |
| 36 | + const html = fence(tokens, idx, options, env, self); |
| 37 | + const flag = tokens[idx].info.split(/\s+/).slice(1).find((word) => word.startsWith("try")); |
| 38 | + if (!flag) return html; |
| 39 | + const mode = flag === "try=fresh" ? "fresh" : "apply"; |
| 40 | + return `<div class="example" data-try="${mode}">${html}</div>\n`; |
| 41 | + }; |
| 42 | + }); |
| 43 | + |
| 44 | + // h2/h3 headings of the rendered page, for the sidebar table of contents. |
| 45 | + eleventyConfig.addFilter("toc", (content) => |
| 46 | + [...content.matchAll(/<h([23]) id="([^"]+)">(.*?)<\/h\1>/g)].map(([, level, id, text]) => ({ |
| 47 | + level: Number(level), |
| 48 | + id, |
| 49 | + text: text.replace(/<[^>]+>/g, ""), |
| 50 | + })) |
| 51 | + ); |
| 52 | + |
| 53 | + const inline = (text) => markdown.renderInline(text); |
| 54 | + |
| 55 | + const tableCaptions = { |
| 56 | + document: "Document and navigation attributes", |
| 57 | + interface: "Interface attributes", |
| 58 | + files: "Runtime file attributes", |
| 59 | + resources: "PDF.js resource attributes", |
| 60 | + }; |
| 61 | + |
| 62 | + eleventyConfig.addShortcode("attributeTable", (group) => { |
| 63 | + const rows = attributes |
| 64 | + .filter((attr) => attr.group === group) |
| 65 | + .map( |
| 66 | + (attr) => `<tr id="attr-${attr.name}"> |
| 67 | +<th scope="row"><a href="#attr-${attr.name}"><code>${attr.name}</code></a></th> |
| 68 | +<td>${inline(attr.description)}</td> |
| 69 | +<td class="default">${attr.default ? inline(attr.default) : `<span class="muted"><span aria-hidden="true">—</span><span class="visually-hidden">None</span></span>`}</td> |
| 70 | +<td><span class="update update-${attr.update}">${{ live: "Live", load: "Next load", init: "Before connect", none: "No effect" }[attr.update]}</span></td> |
| 71 | +</tr>` |
| 72 | + ) |
| 73 | + .join(""); |
| 74 | + return `<div class="table-wrap"><table class="attributes"><caption class="visually-hidden">${tableCaptions[group]}</caption><thead><tr><th scope="col">Attribute</th><th scope="col">Description</th><th scope="col">Default</th><th scope="col">Updates</th></tr></thead><tbody>${rows}</tbody></table></div>`; |
| 75 | + }); |
| 76 | + |
| 77 | + eleventyConfig.addShortcode("playground", () => { |
| 78 | + const controls = attributes |
| 79 | + .filter((attr) => attr.playground) |
| 80 | + .map(({ name, playground: control }) => { |
| 81 | + const id = `pg-${name}`; |
| 82 | + let field; |
| 83 | + if (control.type === "select") { |
| 84 | + const options = control.options |
| 85 | + .map((option) => { |
| 86 | + const [value, label] = Array.isArray(option) ? option : [option, option || "(unset)"]; |
| 87 | + return `<option value="${escapeHtml(value)}">${escapeHtml(label)}</option>`; |
| 88 | + }) |
| 89 | + .join(""); |
| 90 | + field = `<select id="${id}" data-attr="${name}">${options}</select>`; |
| 91 | + } else if (control.type === "checkbox") { |
| 92 | + field = `<input id="${id}" type="checkbox" data-attr="${name}" value="true">`; |
| 93 | + } else { |
| 94 | + field = `<input id="${id}" type="${control.type}" data-attr="${name}"${control.min ? ` min="${control.min}"` : ""} placeholder="${escapeHtml(control.placeholder ?? "")}" autocomplete="off">`; |
| 95 | + } |
| 96 | + return `<div class="control"><label for="${id}"><code>${name}</code></label>${field}</div>`; |
| 97 | + }) |
| 98 | + .join(""); |
| 99 | + return `<form class="playground" data-playground aria-label="Attribute playground"><p class="playground-hint">Each change applies to the live preview straight away.</p>${controls}</form>`; |
| 100 | + }); |
| 101 | + |
| 102 | + return { |
| 103 | + dir: { input: "src", output: "_site" }, |
| 104 | + markdownTemplateEngine: "njk", |
| 105 | + htmlTemplateEngine: "njk", |
| 106 | + }; |
| 107 | +} |
0 commit comments