|
1 | 1 | const pkg = require("./package.json"); |
2 | 2 | const Prism = require("prismjs"); |
| 3 | +const PrismLoader = require("./src/PrismLoader"); |
3 | 4 | const hasTemplateFormat = require("./src/hasTemplateFormat"); |
4 | 5 | const HighlightPairedShortcode = require("./src/HighlightPairedShortcode"); |
5 | 6 | const LiquidHighlightTag = require("./src/LiquidHighlightTag"); |
6 | 7 | const markdownPrismJs = require("./src/markdownSyntaxHighlightOptions"); |
7 | 8 |
|
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); |
51 | 34 | }); |
52 | 35 | } |
| 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}) |
53 | 58 | }; |
54 | 59 |
|
55 | 60 | module.exports.pairedShortcode = HighlightPairedShortcode; |
0 commit comments