Skip to content

Commit e402573

Browse files
committed
create snippet and filter for code blocks
1 parent 455239f commit e402573

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

.eleventy.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,20 @@ let markdownItAnchorOptions = {
7878
);
7979
});
8080

81+
eleventyConfig.addFilter("esmToCjs", async (sourceCode) => {
82+
const { ImportTransformer } = await import('esm-import-transformer');
83+
try {
84+
let it = new ImportTransformer(sourceCode);
85+
let outputCode = it.transformToRequire();
86+
87+
// lol
88+
return outputCode.replace("export default ", "module.exports = ");
89+
} catch(e) {
90+
console.log( sourceCode );
91+
throw e;
92+
}
93+
});
94+
8195
eleventyConfig.addNunjucksFilter("getDocumentsFromCollection", function (collection, value) {
8296
const docs = collection.filter(doc => doc.data.repo === value)
8397
return docs

src/_includes/snippets/esmCjsTabs.njk

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{# This code is borrowed from the 11ty docs website #}
2+
<seven-minute-tabs persist="persist" sync="sync">
3+
<ol role="tablist" aria-label="Select to view response for green, and not green URLs.">
4+
<li>
5+
<a href="{{ tabid }}-esm" role="tab" data-tabs-persist="group:esm" tabindex="-1" aria-controls="{{ tabid }}-esm">ESM</a>
6+
</li>
7+
<li>
8+
<a href="{{ tabid }}-cjs" role="tab" data-tabs-persist="group:cjs" tabindex="-1" aria-controls="{{ tabid }}-cjs">CommonJS</a>
9+
</li>
10+
</ol>
11+
<div id="{{ tabid }}-esm" role="tabpanel">
12+
{% highlight "js" %}
13+
{%- if codeContent %}
14+
{{ codeContent | safe }}
15+
{% endif %}
16+
{% endhighlight %}
17+
</div>
18+
<div id="{{ tabid }}-cjs" role="tabpanel">
19+
{% highlight "js" %}
20+
{%- if codeContentCjs %}
21+
{{ codeContentCjs | safe }}
22+
{%- else if codeContent %}
23+
{{ codeContent | esmToCjs | safe }}
24+
{% endif %}
25+
{% endhighlight %}
26+
</div>
27+
</seven-minute-tabs>

0 commit comments

Comments
 (0)