forked from patternfly/patternfly-elements
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml-example.cjs
More file actions
41 lines (36 loc) · 1.12 KB
/
Copy pathhtml-example.cjs
File metadata and controls
41 lines (36 loc) · 1.12 KB
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
const { readFile } = require('node:fs/promises');
const { dirname, join } = require('node:path');
function dedent(str) {
const stripped = str.replace(/^\n/, '');
const match = stripped.match(/^\s+/);
return match ? stripped.replace(new RegExp(`^${match[0]}`, 'gm'), '') : str;
}
async function renderFromFile(content, { src }) {
let path;
if (src.startsWith('/')) {
path = join(process.cwd(), src);
} else {
path = join(dirname(this.ctx._.docsTemplatePath), src);
}
if (path) {
const fileContent = await readFile(path, 'utf-8');
return fileContent;
} else {
return content;
}
}
module.exports = function(eleventyConfig) {
eleventyConfig.addPairedShortcode('htmlexample', async function(content, kwargs) {
if (kwargs?.src) {
content = await renderFromFile.call(this, dedent(content), kwargs);
}
return /* html */`
<div class="example-preview ${kwargs?.class ?? ''}">${content.trim()}</div>
<details class="html-example ${kwargs?.class ?? ''}"${!kwargs?.style ? '' : ` style="${kwargs.style}"`}>
<summary>View HTML Source</summary>
~~~html
${dedent(content)}
~~~
</details>`;
});
};