Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to load languages explicitly #90

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 48 additions & 43 deletions .eleventy.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,60 @@
const pkg = require("./package.json");
const Prism = require("prismjs");
const PrismLoader = require("./src/PrismLoader");
const hasTemplateFormat = require("./src/hasTemplateFormat");
const HighlightPairedShortcode = require("./src/HighlightPairedShortcode");
const LiquidHighlightTag = require("./src/LiquidHighlightTag");
const markdownPrismJs = require("./src/markdownSyntaxHighlightOptions");

module.exports = {
initArguments: { Prism },
configFunction: function(eleventyConfig, options = {}) {
try {
eleventyConfig.versionCheck(pkg["11ty"].compatibility);
} catch(e) {
console.log( `WARN: Eleventy Plugin (${pkg.name}) Compatibility: ${e.message}` );
}

options = Object.assign({
lineSeparator: "\n",
errorOnInvalidLanguage: false,
alwaysWrapLineHighlights: false,
preAttributes: {},
codeAttributes: {}
}, options);

if( hasTemplateFormat(options.templateFormats, "liquid") ) {
eleventyConfig.addLiquidTag("highlight", (liquidEngine) => {
// {% highlight js 0 2 %}
let highlight = new LiquidHighlightTag(liquidEngine);
return highlight.getObject(options);
});
}

if( hasTemplateFormat(options.templateFormats, "njk") ) {
eleventyConfig.addPairedNunjucksShortcode("highlight", (content, args) => {
// {% highlight "js 0 2-3" %}
let [language, ...highlightNumbers] = args.split(" ");
return HighlightPairedShortcode(content, language, highlightNumbers.join(" "), options);
});
}

if( hasTemplateFormat(options.templateFormats, "md") ) {
// ```js/0,2-3
eleventyConfig.addMarkdownHighlighter(markdownPrismJs(options));
}

// we need to add this as many template languages (Vue, WebC) rely on JavaScript functions (not just 11ty.js)
eleventyConfig.addJavaScriptFunction("highlight", (language, content, highlight1, highlight2) => {
let highlightLines = [highlight1, highlight2].filter(entry => entry).join(" ");
let result = HighlightPairedShortcode(content, language, highlightLines, options);
return result;
module.exports = function(eleventyConfig, options){
try {
eleventyConfig.versionCheck(pkg["11ty"].compatibility);
} catch(e) {
console.log( `WARN: Eleventy Plugin (${pkg.name}) Compatibility: ${e.message}` );
}
options = Object.assign({
init: function({Prism}){},
lineSeparator: "\n",
errorOnInvalidLanguage: false,
alwaysWrapLineHighlights: false,
preAttributes: {},
codeAttributes: {},
languages: [],
}, options);

for(const language of options.languages){
PrismLoader(language)
}

if( hasTemplateFormat(options.templateFormats, "liquid") ) {
eleventyConfig.addLiquidTag("highlight", (liquidEngine) => {
// {% highlight js 0 2 %}
let highlight = new LiquidHighlightTag(liquidEngine);
return highlight.getObject(options);
});
}

if( hasTemplateFormat(options.templateFormats, "njk") ) {
eleventyConfig.addPairedNunjucksShortcode("highlight", (content, args) => {
// {% highlight "js 0 2-3" %}
let [language, ...highlightNumbers] = args.split(" ");
return HighlightPairedShortcode(content, language, highlightNumbers.join(" "), options);
});
}

if( hasTemplateFormat(options.templateFormats, "md") ) {
// ```js/0,2-3
eleventyConfig.addMarkdownHighlighter(markdownPrismJs(options));
}

// we need to add this as many template languages (Vue, WebC) rely on JavaScript functions (not just 11ty.js)
eleventyConfig.addJavaScriptFunction("highlight", (language, content, highlight1, highlight2) => {
let highlightLines = [highlight1, highlight2].filter(entry => entry).join(" ");
let result = HighlightPairedShortcode(content, language, highlightLines, options);
return result;
});

options.init({Prism})
};

module.exports.pairedShortcode = HighlightPairedShortcode;