Skip to content

Commit 9dbd54a

Browse files
committed
Big release—adds support for Nunjucks highlighter, adds options to allow specifying using only some of the syntax highlighter pack (Fixes #2), adds options.init to make it possible to further customize Prism (Fixes #4)
1 parent a523832 commit 9dbd54a

10 files changed

+241
-79
lines changed

.eleventy.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
1-
const liquidPlain = require("./src/liquidSyntaxHighlightPlain");
2-
const liquidPrismJs = require("./src/liquidSyntaxHighlightPrism");
1+
const Prism = require("prismjs");
2+
const hasTemplateFormat = require("./src/hasTemplateFormat");
3+
const HighlightPairedShortcode = require("./src/HighlightPairedShortcode");
4+
const LiquidHighlightTag = require("./src/LiquidHighlightTag");
35
const markdownPrismJs = require("./src/markdownSyntaxHighlight");
46

5-
module.exports = function(eleventyConfig, pluginNamespace) {
6-
eleventyConfig.namespace(pluginNamespace, () => {
7-
// compatibility with existing {% highlight js %} and others
8-
eleventyConfig.addLiquidTag("highlight", liquidPrismJs);
9-
eleventyConfig.addMarkdownHighlighter(markdownPrismJs);
7+
module.exports = {
8+
initArguments: { Prism },
9+
configFunction: function(eleventyConfig, options = {}) {
10+
// TODO hbs?
11+
if( hasTemplateFormat(options.templateFormats, "liquid") ) {
12+
eleventyConfig.addLiquidTag("highlight", (liquidEngine) => {
13+
// {% highlight js 0 2 %}
14+
let highlight = new LiquidHighlightTag(liquidEngine);
15+
return highlight.getObject();
16+
});
17+
}
1018

11-
// Deprecated, use {% highlight text %} instead.
12-
eleventyConfig.addLiquidTag("highlight-plain", liquidPlain);
13-
});
19+
if( hasTemplateFormat(options.templateFormats, "njk") ) {
20+
eleventyConfig.addPairedNunjucksShortcode("highlight", (content, args) => {
21+
// {% highlight "js 0 2-3" %}
22+
let [language, ...highlightNumbers] = args.split(" ");
23+
return HighlightPairedShortcode(content, language, highlightNumbers.join(" "));
24+
});
25+
}
26+
27+
if( hasTemplateFormat(options.templateFormats, "md") ) {
28+
eleventyConfig.addMarkdownHighlighter(markdownPrismJs);
29+
}
30+
}
1431
};

README.md

Lines changed: 120 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,36 @@ You are responsible for including [your favorite PrismJS theme CSS](https://gith
2323

2424
Read more about [Eleventy plugins.](https://www.11ty.io/docs/plugins/)
2525

26+
### Options
27+
28+
Optionally pass in an options object as the second argument to `addPlugin` to further customize this plugin pack.
29+
30+
```
31+
const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
32+
module.exports = function(eleventyConfig) {
33+
eleventyConfig.addPlugin(syntaxHighlight, {
34+
35+
// Change which syntax highlighters are installed
36+
templateFormats: ["*"], // default
37+
38+
// Or, just njk and md syntax highlighters (do not install liquid)
39+
// templateFormats: ["njk", "md"],
40+
41+
// init callback lets you customize Prism
42+
init: function({ Prism }) {
43+
Prism.languages.myCustomLanguage = /* */;
44+
}
45+
});
46+
};
47+
```
48+
2649
## Usage
2750

2851
### This plugin provides:
2952

3053
* Markdown Highlighter: syntax highlights using PrismJS
31-
* Liquid Tag `{% highlight %}`: syntax highlights using PrismJS.
54+
* Liquid Custom Tag `{% highlight %}`: syntax highlights using PrismJS.
55+
* Nunjucks Paired Shortcode `{% highlight %}`: syntax highlights using PrismJS.
3256

3357
### Markdown Highlighter
3458

@@ -37,6 +61,7 @@ Optionally specify a language after the start of the markdown fenced code block.
3761
* [List of supported PrismJS languages](http://prismjs.com/#languages-list)
3862

3963
````
64+
<!-- Markdown Template -->
4065
``` js
4166
function myFunction() {
4267
return true;
@@ -45,8 +70,12 @@ function myFunction() {
4570
````
4671

4772
````
48-
// Line highlighting classes (single highlight)
49-
// Adds `highlight-line-active` class to lines 1,3,4,5 (for line highlighting)
73+
<!--
74+
Line highlighting classes (single highlight)
75+
Adds `highlight-line-active` class to lines 1,3,4,5 (for line highlighting)
76+
-->
77+
78+
<!-- Markdown Template -->
5079
``` js/1,3-5
5180
function myFunction() {
5281
// …
@@ -56,9 +85,13 @@ function myFunction() {
5685
````
5786

5887
````
59-
// Line highlighting classes (add and remove mode)
60-
// Adds `highlight-line-add` class to lines 1,3
61-
// Adds `highlight-line-remove` class to lines 5,6,7,8
88+
<!--
89+
Line highlighting classes (add and remove mode)
90+
Adds `highlight-line-add` class to lines 1,3
91+
Adds `highlight-line-remove` class to lines 5,6,7,8
92+
-->
93+
94+
<!-- Markdown Template -->
6295
``` js/1,3/5-8
6396
function myFunction() {
6497
// …
@@ -85,6 +118,7 @@ function myFunction() {
85118
* [List of supported PrismJS languages](http://prismjs.com/#languages-list)
86119

87120
```
121+
<!-- Liquid Template -->
88122
{% highlight js %}
89123
function myFunction() {
90124
return true;
@@ -93,8 +127,12 @@ function myFunction() {
93127
```
94128

95129
```
96-
// Line highlighting classes (single highlight)
97-
// Adds `highlight-line-active` class to lines 1,3,4,5 (for line highlighting)
130+
<!--
131+
Line highlighting classes (single highlight)
132+
Adds `highlight-line-active` class to lines 1,3,4,5 (for line highlighting)
133+
-->
134+
135+
<!-- Liquid Template -->
98136
{% highlight js 1,3-5 %}
99137
function myFunction() {
100138
// …
@@ -104,9 +142,13 @@ function myFunction() {
104142
```
105143

106144
```
107-
// Line highlighting classes (add and remove)
108-
// Adds `highlight-line-add` class to lines 1,3
109-
// Adds `highlight-line-remove` class to lines 5,6,7,8
145+
<!--
146+
Line highlighting classes (add and remove)
147+
Adds `highlight-line-add` class to lines 1,3
148+
Adds `highlight-line-remove` class to lines 5,6,7,8
149+
-->
150+
151+
<!-- Liquid Template -->
110152
{% highlight js 1,3 5-8 %}
111153
function myFunction() {
112154
// …
@@ -120,6 +162,7 @@ function myFunction() {
120162
Use `text` to use the line highlighting features without PrismJS.
121163

122164
```
165+
<!-- Liquid Template -->
123166
{% highlight text 1-2 %}
124167
function myFunction() {
125168
let highlighted = true;
@@ -128,16 +171,79 @@ function myFunction() {
128171
{% endhighlight %}
129172
```
130173

131-
### Sample Line Highlighting CSS
174+
### Nunjucks Paired Shortcode: Prism Syntax Highlighter
175+
176+
* [List of supported PrismJS languages](http://prismjs.com/#languages-list)
177+
178+
```
179+
<!-- Nunjucks Template -->
180+
{% highlight "js" %}
181+
function myFunction() {
182+
return true;
183+
}
184+
{% endhighlight %}
185+
```
132186

133187
```
188+
<!--
189+
Line highlighting classes (single highlight)
190+
Adds `highlight-line-active` class to lines 1,3,4,5 (for line highlighting)
191+
-->
192+
193+
<!-- Nunjucks Template -->
194+
{% highlight "js 1,3-5" %}
195+
function myFunction() {
196+
// …
197+
return true;
198+
}
199+
{% endhighlight %}
200+
```
201+
202+
```
203+
<!--
204+
Line highlighting classes (add and remove)
205+
Adds `highlight-line-add` class to lines 1,3
206+
Adds `highlight-line-remove` class to lines 5,6,7,8
207+
-->
208+
209+
<!-- Nunjucks Template -->
210+
{% highlight "js 1,3 5-8" %}
211+
function myFunction() {
212+
// …
213+
return true;
214+
}
215+
{% endhighlight %}
216+
```
217+
218+
#### Plain text
219+
220+
Use `text` to use the line highlighting features without PrismJS.
221+
222+
```
223+
<!-- Nunjucks Template -->
224+
{% highlight "text 1-2" %}
225+
function myFunction() {
226+
let highlighted = true;
227+
return highlighted;
228+
}
229+
{% endhighlight %}
230+
```
231+
232+
### Sample Line Highlighting CSS
233+
234+
```css
134235
.highlight-line {
135236
display: block;
136-
padding: 0.125em 1em;
137237
text-decoration: none; /* override del, ins, mark defaults */
138238
color: inherit; /* override del, ins, mark defaults */
139239
}
140-
.highlight-line:not(:empty) + br {
240+
241+
/* allow highlighting empty lines */
242+
.highlight-line:empty:before {
243+
content: " ";
244+
}
245+
/* avoid double line breaks when using display: block; */
246+
.highlight-line + br {
141247
display: none;
142248
}
143249

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"ava": "^0.25.0"
2828
},
2929
"dependencies": {
30-
"@11ty/eleventy": "^0.3.5",
31-
"prismjs": "^1.13.0"
30+
"@11ty/eleventy": "^0.5.4",
31+
"prismjs": "^1.15.0"
3232
}
3333
}

src/HighlightLinesGroup.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ class HighlightLinesGroup {
55
this.init(str, delimiter);
66
}
77

8-
init(str, delimiter) {
8+
init(str = "", delimiter = " ") {
99
this.str = str;
10-
this.delimiter = delimiter || " ";
10+
this.delimiter = delimiter;
1111

1212
let split = str.split(this.delimiter);
1313
this.highlights = new HighlightLines(split.length === 1 ? split[0] : "");

src/HighlightPairedShortcode.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const Prism = require("prismjs");
2+
const PrismLoader = require("prismjs/components/index.js");
3+
const HighlightLinesGroup = require("./HighlightLinesGroup");
4+
5+
module.exports = function(content, language, highlightNumbers) {
6+
let highlightedContent;
7+
if( language === "text" ) {
8+
highlightedContent = content.trim();
9+
} else {
10+
if( !Prism.languages[ language ] ) {
11+
PrismLoader([language]);
12+
}
13+
14+
highlightedContent = Prism.highlight(content.trim(), Prism.languages[ language ]);
15+
}
16+
17+
let group = new HighlightLinesGroup(highlightNumbers);
18+
19+
let lines = highlightedContent.split("\n").map(function(line, j) {
20+
return group.getLineMarkup(j, line);
21+
});
22+
23+
return `<pre class="language-${language}"><code class="language-${language}">` + lines.join("<br>") + "</code></pre>";
24+
};
Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
const HighlightLinesGroup = require("./HighlightLinesGroup");
2+
const HighlightPairedShortcode = require("./HighlightPairedShortcode");
23

3-
class LiquidHighlight {
4+
class LiquidHighlightTag {
45
constructor(liquidEngine) {
56
this.liquidEngine = liquidEngine;
6-
this.hooks = [];
7-
this.classHooks = [];
8-
}
9-
10-
addHook(hookFunction) {
11-
this.hooks.push(hookFunction);
12-
}
13-
14-
addClassHook(hookFunction) {
15-
this.classHooks.push(hookFunction);
167
}
178

189
getObject() {
@@ -22,7 +13,7 @@ class LiquidHighlight {
2213
let split = tagToken.args.split(" ");
2314

2415
this.language = split.shift();
25-
this.highlights = new HighlightLinesGroup(split.join(" "));
16+
this.highlightLines = split.join(" ");
2617

2718
this.tokens = [];
2819

@@ -46,23 +37,7 @@ class LiquidHighlight {
4637
let tokens = this.tokens.map(token => token.raw);
4738
let tokenStr = tokens.join("").trim();
4839

49-
for( let hook of highlighter.hooks ) {
50-
tokenStr = hook.call(this, this.language, tokenStr);
51-
}
52-
53-
let lines = tokenStr.split("\n").map(function(line, j) {
54-
let classHookClasses = [];
55-
for( let classHook of highlighter.classHooks ) {
56-
let ret = classHook(this.language, line, j);
57-
if( ret ) {
58-
classHookClasses.push(ret);
59-
}
60-
}
61-
62-
return this.highlights.getLineMarkup(j, line, classHookClasses);
63-
}.bind(this));
64-
65-
return Promise.resolve(`<pre class="language-${this.language}"><code class="language-${this.language}">` + lines.join("<br>") + "</code></pre>");
40+
return Promise.resolve(HighlightPairedShortcode(tokenStr, this.language, this.highlightLines));
6641
}
6742
};
6843
};
@@ -71,4 +46,4 @@ class LiquidHighlight {
7146
}
7247
}
7348

74-
module.exports = LiquidHighlight;
49+
module.exports = LiquidHighlightTag;

src/hasTemplateFormat.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = function(templateFormats = ["*"], format = false) {
2+
if(!Array.isArray(templateFormats)) {
3+
templateFormats = [templateFormats];
4+
}
5+
6+
if( Array.isArray(templateFormats) ) {
7+
if( templateFormats.indexOf("*") > -1 || templateFormats.indexOf(format) > -1 ) {
8+
return true;
9+
}
10+
}
11+
12+
return false;
13+
};

src/liquidSyntaxHighlightPrism.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)