-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.eleventy.js
182 lines (158 loc) · 4.83 KB
/
.eleventy.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const path = require("path");
// Hack: Stop this plugin clobbering any other uses of Prism
// Related bug: https://github.com/PrismJS/prism/issues/3636
const globalPrism = global.Prism;
const Prism = require("prismjs");
global.Prism = globalPrism;
// Endhack
const prettier = require("prettier");
const fs = require("fs");
const defaults = {
path: path.join(".", "demos"),
parsers: {
html: "html",
css: "css",
javascript: "babel",
},
displayNames: {
html: "HTML",
css: "CSS",
javascript: "JavaScript",
result: "Result",
},
filenames: {
html: "index.*",
css: "styles.css",
javascript: "main.js",
},
open: {
html: false,
css: false,
javascript: false,
result: true,
},
prettier: true,
};
function deepMerge(base, overrides) {
const returnObj = {};
Object.keys(base).forEach((key) => {
if (typeof base[key] === "object" && overrides[key] !== undefined) {
return (returnObj[key] = deepMerge(base[key], overrides[key]));
} else if (overrides[key] !== undefined) {
return (returnObj[key] = overrides[key]);
}
returnObj[key] = base[key];
});
return returnObj;
}
function stripTrailingSlashes(str) {
let end = str.length;
while (str[--end] === "/");
return str.slice(0, end + 1);
}
function demoFileExists(path) {
try {
fs.accessSync(path);
return true;
} catch (e) {
return false;
}
}
module.exports = function demo(eleventyConfig, userOptions = {}) {
const options = deepMerge(defaults, userOptions);
options.path = stripTrailingSlashes(options.path);
const demos = [];
eleventyConfig.addWatchTarget(`${options.path}/**/*`);
eleventyConfig.addCollection("demos", (collectionApi) => {
const filtered = collectionApi.getFilteredByGlob(
`${options.path}/**/${options.filenames.html}`
);
demos.push(...filtered);
return filtered;
});
[
{ name: "demoCss", language: "css", tag: "style" },
{ name: "demoJS", language: "javascript", tag: "script" },
].forEach(({ name, language, tag }) => {
eleventyConfig.addShortcode(name, function () {
const pagePath = path.dirname(path.resolve(this.page.inputPath));
const page = demos.find(
(page) => path.dirname(path.resolve(page.inputPath)) === pagePath
);
const filename =
page.data?.filenames?.[language] || options.filenames[language];
const filepath = path.join(pagePath, filename);
if (demoFileExists(filepath)) {
try {
return `<${tag}>${fs.readFileSync(filepath, "utf-8")}</${tag}>`;
} catch (e) {
throw new Error(
`Problem trying to load demo file "${filepath}": ${e}`
);
}
}
return "";
});
});
eleventyConfig.addShortcode("embeddedDemo", function (demoName) {
if (!demoName) {
throw new Error("No demo name passed");
}
const searchPath = stripTrailingSlashes(
path.join(path.resolve("."), options.path, demoName)
);
const page = demos.find(
(page) => path.dirname(path.resolve(page.inputPath)) === searchPath
);
const files = [];
files.push({ code: page.templateContent, language: "html" });
["css", "javascript"].forEach((language) => {
const filename =
page.data?.filenames?.[language] || options.filenames[language];
const filepath = path.join(searchPath, filename);
if (demoFileExists(filepath)) {
try {
files.push({ code: fs.readFileSync(filepath, "utf-8"), language });
} catch (e) {
throw new Error(
`Problem trying to load demo file "${filepath}": ${e}`
);
}
}
});
const blocks = files.map(({ code, language }) => {
const highlighted = Prism.highlight(
options.prettier
? prettier.format(code, { parser: options.parsers[language] })
: code,
Prism.languages[language],
language
);
const lines = highlighted.split("\n").slice(0, -1);
return `<details class="eleventy-plugin-embedded-demo__code" ${
options.open[language] && "open"
}>
<summary class="eleventy-plugin-embedded-demo__code-toggle">${
options.displayNames[language]
}</summary>
<pre class="language-${language}"><code class="language-${language}">${lines.join(
"<br>"
)}</code></pre>
</details>`;
});
return `<div class="eleventy-plugin-embedded-demo__container">
${blocks.join("")}
<details class="eleventy-plugin-embedded-demo__result" ${
options.open.result && "open"
}>
<summary class="eleventy-plugin-embedded-demo__result-toggle">${
options.displayNames.result
}</summary>
<iframe src="${
page.url
}" title="${page.data.title}" loading="lazy" class="eleventy-plugin-embedded-demo__result-iframe"></iframe>
</details>
</div>`;
});
};
module.exports.defaults = defaults;