-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Big release—adds support for Nunjucks highlighter, adds options to al…
…low specifying using only some of the syntax highlighter pack (Fixes #2), adds `options.init` to make it possible to further customize Prism (Fixes #4)
- Loading branch information
Showing
10 changed files
with
241 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,31 @@ | ||
const liquidPlain = require("./src/liquidSyntaxHighlightPlain"); | ||
const liquidPrismJs = require("./src/liquidSyntaxHighlightPrism"); | ||
const Prism = require("prismjs"); | ||
const hasTemplateFormat = require("./src/hasTemplateFormat"); | ||
const HighlightPairedShortcode = require("./src/HighlightPairedShortcode"); | ||
const LiquidHighlightTag = require("./src/LiquidHighlightTag"); | ||
const markdownPrismJs = require("./src/markdownSyntaxHighlight"); | ||
|
||
module.exports = function(eleventyConfig, pluginNamespace) { | ||
eleventyConfig.namespace(pluginNamespace, () => { | ||
// compatibility with existing {% highlight js %} and others | ||
eleventyConfig.addLiquidTag("highlight", liquidPrismJs); | ||
eleventyConfig.addMarkdownHighlighter(markdownPrismJs); | ||
module.exports = { | ||
initArguments: { Prism }, | ||
configFunction: function(eleventyConfig, options = {}) { | ||
// TODO hbs? | ||
if( hasTemplateFormat(options.templateFormats, "liquid") ) { | ||
eleventyConfig.addLiquidTag("highlight", (liquidEngine) => { | ||
// {% highlight js 0 2 %} | ||
let highlight = new LiquidHighlightTag(liquidEngine); | ||
return highlight.getObject(); | ||
}); | ||
} | ||
|
||
// Deprecated, use {% highlight text %} instead. | ||
eleventyConfig.addLiquidTag("highlight-plain", liquidPlain); | ||
}); | ||
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(" ")); | ||
}); | ||
} | ||
|
||
if( hasTemplateFormat(options.templateFormats, "md") ) { | ||
eleventyConfig.addMarkdownHighlighter(markdownPrismJs); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const Prism = require("prismjs"); | ||
const PrismLoader = require("prismjs/components/index.js"); | ||
const HighlightLinesGroup = require("./HighlightLinesGroup"); | ||
|
||
module.exports = function(content, language, highlightNumbers) { | ||
let highlightedContent; | ||
if( language === "text" ) { | ||
highlightedContent = content.trim(); | ||
} else { | ||
if( !Prism.languages[ language ] ) { | ||
PrismLoader([language]); | ||
} | ||
|
||
highlightedContent = Prism.highlight(content.trim(), Prism.languages[ language ]); | ||
} | ||
|
||
let group = new HighlightLinesGroup(highlightNumbers); | ||
|
||
let lines = highlightedContent.split("\n").map(function(line, j) { | ||
return group.getLineMarkup(j, line); | ||
}); | ||
|
||
return `<pre class="language-${language}"><code class="language-${language}">` + lines.join("<br>") + "</code></pre>"; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module.exports = function(templateFormats = ["*"], format = false) { | ||
if(!Array.isArray(templateFormats)) { | ||
templateFormats = [templateFormats]; | ||
} | ||
|
||
if( Array.isArray(templateFormats) ) { | ||
if( templateFormats.indexOf("*") > -1 || templateFormats.indexOf(format) > -1 ) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
}; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.