-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: correct jekyll highlight tag implementation
- Loading branch information
Showing
27 changed files
with
195 additions
and
23 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
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
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
21 changes: 0 additions & 21 deletions
21
javascript-modules/engines/jekyll-engine/lib/plugins/highlight.js
This file was deleted.
Oops, something went wrong.
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 @@ | ||
engine-strict = true |
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 @@ | ||
engine-strict = true |
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 @@ | ||
# Bookshop helpers |
46 changes: 46 additions & 0 deletions
46
javascript-modules/helpers/lib/plugins/liquid/highlight.js
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,46 @@ | ||
/** | ||
* Rough skeleton for the highlight tag in Jekyll. | ||
* This won't perform any code highlighting, but should create the same outer DOM structure. | ||
* This should also escape any HTML within. | ||
*/ | ||
|
||
const escapeHtml = (input) => { | ||
return input | ||
.replace(/&/g, "&") | ||
.replace(/</g, "<") | ||
.replace(/>/g, ">") | ||
.replace(/"/g, """) | ||
.replace(/'/g, "'"); | ||
} | ||
|
||
export const liquidHighlight = function (Liquid) { | ||
this.registerTag('highlight', { | ||
parse: function(token, remainingTokens) { | ||
this.lang = token.args.split(' ')[0]; | ||
this.contents = []; | ||
|
||
const stream = this.liquid.parser.parseStream(remainingTokens) | ||
.on('tag:endhighlight', () => stream.stop()) | ||
.on('template', (tpl) => this.contents.push(tpl)) | ||
.on('end', () => { | ||
throw new Error(`tag ${token.raw} not closed`) | ||
}); | ||
|
||
stream.start(); | ||
}, | ||
render: function*(ctx, hash) { | ||
const r = this.liquid.renderer; | ||
const html = yield r.renderTemplates(this.contents, ctx); | ||
const langAttrs = this.lang | ||
? ` class="language-${this.lang}" data-lang="${this.lang}"` | ||
: ''; | ||
return `<figure class="highlight"> | ||
<pre> | ||
<code${langAttrs}> | ||
${escapeHtml(html)} | ||
</code> | ||
</pre> | ||
</figure>`; | ||
} | ||
}); | ||
} |
56 changes: 56 additions & 0 deletions
56
javascript-modules/helpers/lib/plugins/liquid/highlight.test.js
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,56 @@ | ||
import test from 'ava'; | ||
import { Liquid } from 'liquidjs'; | ||
import { liquidHighlight } from './highlight.js'; | ||
|
||
test("should escape HTML", async t => { | ||
const liquid = new Liquid({}); | ||
liquid.plugin(liquidHighlight); | ||
|
||
const result = await liquid.parseAndRender(` | ||
{%- highlight -%} | ||
<h1>Hello World</h1> | ||
{%- endhighlight -%} | ||
`); | ||
t.notRegex(result, /<h1>Hello World<\/h1>/); | ||
t.regex(result, /<h1>/); | ||
}); | ||
|
||
test("should evaluate inner liquid", async t => { | ||
const liquid = new Liquid({}); | ||
liquid.plugin(liquidHighlight); | ||
|
||
const result = await liquid.parseAndRender(` | ||
{%- highlight -%} | ||
<h1>Hello {{ test }}</h1> | ||
{%- endhighlight -%} | ||
`, {test: "Bookshop"}); | ||
t.regex(result, /<h1>Hello Bookshop<\/h1>/); | ||
}); | ||
|
||
test("should respect raw tags", async t => { | ||
const liquid = new Liquid({}); | ||
liquid.plugin(liquidHighlight); | ||
|
||
const result = await liquid.parseAndRender(` | ||
{%- highlight -%} | ||
{% raw %}<h1>Hello {{ test }}</h1>{% endraw %} | ||
{%- endhighlight -%} | ||
`, {test: "Bookshop"}); | ||
t.regex(result, /<h1>Hello {{ test }}<\/h1>/); | ||
}); | ||
|
||
test("should handle no language", async t => { | ||
const liquid = new Liquid({}); | ||
liquid.plugin(liquidHighlight); | ||
|
||
const result = await liquid.parseAndRender(`{% highlight %}{% endhighlight %}`); | ||
t.regex(result, /<code>/); | ||
}); | ||
|
||
test("should handle language", async t => { | ||
const liquid = new Liquid({}); | ||
liquid.plugin(liquidHighlight); | ||
|
||
const result = await liquid.parseAndRender(`{% highlight ruby %}{% endhighlight %}`); | ||
t.regex(result, /<code class="language-ruby" data-lang="ruby">/); | ||
}); |
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 @@ | ||
export {liquidHighlight} from './lib/plugins/liquid/highlight.js'; |
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 @@ | ||
{ | ||
"name": "@bookshop/helpers", | ||
"packageManager": "yarn@3.0.0", | ||
"version": "2.0.6", | ||
"description": "", | ||
"type": "module", | ||
"main": "main.js", | ||
"scripts": { | ||
"test": "nyc ava -v" | ||
}, | ||
"author": "CloudCannon", | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"devDependencies": { | ||
"ava": "^3.15.0", | ||
"liquidjs": "9.28.0", | ||
"nyc": "^15.1.0" | ||
}, | ||
"engines": { | ||
"node": ">=14.16" | ||
} | ||
} |
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
Oops, something went wrong.