Skip to content

Commit 30fad83

Browse files
committed
Open option to load languages explicitly
1 parent 8ce0632 commit 30fad83

File tree

1 file changed

+48
-43
lines changed

1 file changed

+48
-43
lines changed

.eleventy.js

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,60 @@
11
const pkg = require("./package.json");
22
const Prism = require("prismjs");
3+
const PrismLoader = require("./src/PrismLoader");
34
const hasTemplateFormat = require("./src/hasTemplateFormat");
45
const HighlightPairedShortcode = require("./src/HighlightPairedShortcode");
56
const LiquidHighlightTag = require("./src/LiquidHighlightTag");
67
const markdownPrismJs = require("./src/markdownSyntaxHighlightOptions");
78

8-
module.exports = {
9-
initArguments: { Prism },
10-
configFunction: function(eleventyConfig, options = {}) {
11-
try {
12-
eleventyConfig.versionCheck(pkg["11ty"].compatibility);
13-
} catch(e) {
14-
console.log( `WARN: Eleventy Plugin (${pkg.name}) Compatibility: ${e.message}` );
15-
}
16-
17-
options = Object.assign({
18-
lineSeparator: "\n",
19-
errorOnInvalidLanguage: false,
20-
alwaysWrapLineHighlights: false,
21-
preAttributes: {},
22-
codeAttributes: {}
23-
}, options);
24-
25-
if( hasTemplateFormat(options.templateFormats, "liquid") ) {
26-
eleventyConfig.addLiquidTag("highlight", (liquidEngine) => {
27-
// {% highlight js 0 2 %}
28-
let highlight = new LiquidHighlightTag(liquidEngine);
29-
return highlight.getObject(options);
30-
});
31-
}
32-
33-
if( hasTemplateFormat(options.templateFormats, "njk") ) {
34-
eleventyConfig.addPairedNunjucksShortcode("highlight", (content, args) => {
35-
// {% highlight "js 0 2-3" %}
36-
let [language, ...highlightNumbers] = args.split(" ");
37-
return HighlightPairedShortcode(content, language, highlightNumbers.join(" "), options);
38-
});
39-
}
40-
41-
if( hasTemplateFormat(options.templateFormats, "md") ) {
42-
// ```js/0,2-3
43-
eleventyConfig.addMarkdownHighlighter(markdownPrismJs(options));
44-
}
45-
46-
// we need to add this as many template languages (Vue, WebC) rely on JavaScript functions (not just 11ty.js)
47-
eleventyConfig.addJavaScriptFunction("highlight", (language, content, highlight1, highlight2) => {
48-
let highlightLines = [highlight1, highlight2].filter(entry => entry).join(" ");
49-
let result = HighlightPairedShortcode(content, language, highlightLines, options);
50-
return result;
9+
module.exports = function(eleventyConfig, options){
10+
try {
11+
eleventyConfig.versionCheck(pkg["11ty"].compatibility);
12+
} catch(e) {
13+
console.log( `WARN: Eleventy Plugin (${pkg.name}) Compatibility: ${e.message}` );
14+
}
15+
options = Object.assign({
16+
init: function({Prism}){},
17+
lineSeparator: "\n",
18+
errorOnInvalidLanguage: false,
19+
alwaysWrapLineHighlights: false,
20+
preAttributes: {},
21+
codeAttributes: {},
22+
languages: [],
23+
}, options);
24+
25+
for(const language of options.languages){
26+
PrismLoader(language)
27+
}
28+
29+
if( hasTemplateFormat(options.templateFormats, "liquid") ) {
30+
eleventyConfig.addLiquidTag("highlight", (liquidEngine) => {
31+
// {% highlight js 0 2 %}
32+
let highlight = new LiquidHighlightTag(liquidEngine);
33+
return highlight.getObject(options);
5134
});
5235
}
36+
37+
if( hasTemplateFormat(options.templateFormats, "njk") ) {
38+
eleventyConfig.addPairedNunjucksShortcode("highlight", (content, args) => {
39+
// {% highlight "js 0 2-3" %}
40+
let [language, ...highlightNumbers] = args.split(" ");
41+
return HighlightPairedShortcode(content, language, highlightNumbers.join(" "), options);
42+
});
43+
}
44+
45+
if( hasTemplateFormat(options.templateFormats, "md") ) {
46+
// ```js/0,2-3
47+
eleventyConfig.addMarkdownHighlighter(markdownPrismJs(options));
48+
}
49+
50+
// we need to add this as many template languages (Vue, WebC) rely on JavaScript functions (not just 11ty.js)
51+
eleventyConfig.addJavaScriptFunction("highlight", (language, content, highlight1, highlight2) => {
52+
let highlightLines = [highlight1, highlight2].filter(entry => entry).join(" ");
53+
let result = HighlightPairedShortcode(content, language, highlightLines, options);
54+
return result;
55+
});
56+
57+
options.init({Prism})
5358
};
5459

5560
module.exports.pairedShortcode = HighlightPairedShortcode;

0 commit comments

Comments
 (0)