Skip to content

Commit b8ecca4

Browse files
committed
pat-syntax-highlight: Switch to Prism syntax highlighting.
1 parent 8fd88c0 commit b8ecca4

File tree

3 files changed

+101
-18
lines changed

3 files changed

+101
-18
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"moment-timezone": "^0.5.37",
2828
"photoswipe": "^4.1.3",
2929
"pikaday": "^1.8.0",
30+
"prismjs": "^1.29.0",
3031
"promise-polyfill": "^8.2.3",
3132
"screenfull": "^6.0.2",
3233
"select2": "^3.5.1",
Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,56 @@
11
<!DOCTYPE html>
22
<html>
3-
<head>
4-
<title>pat-syntax-highlight demo page</title>
5-
<meta charset="utf-8">
6-
<link rel="stylesheet" href="/style/common.css" />
7-
<script src="/bundle.min.js"></script>
8-
</head>
9-
<body>
10-
<h2>Sample output:</h2>
11-
<pre class="pat-syntax-highlight light-bg">
12-
var pattern = registry.patterns[name];
3+
<head>
4+
<title>pat-syntax-highlight demo page</title>
5+
<meta charset="utf-8" />
6+
<link href="/style/common.css"
7+
rel="stylesheet"
8+
/>
9+
<script src="/bundle.min.js"></script>
10+
</head>
11+
<body>
12+
<h2>Syntax highlight for JavaScript</h2>
13+
<pre class="pat-syntax-highlight">
14+
<code class="language-javascript">
15+
const pattern = registry.patterns[name];
1316
if (pattern.transform) {
1417
try {
1518
pattern.transform($content);
1619
} catch (e) {
17-
if (dont_catch) { throw(e); }
20+
if (dont_catch) {
21+
throw(e);
22+
}
1823
log.error("Transform error for pattern" + name, e);
1924
}
20-
</pre
21-
>
22-
</body>
25+
}
26+
</code>
27+
</pre>
28+
29+
<h2>Syntax highlight for HTML</h2>
30+
<pre class="pat-syntax-highlight">
31+
<code class="language-html">
32+
&lt;h1&gt;hello&lt;/h1&gt;
33+
</code>
34+
</pre>
35+
36+
<h2>Syntax highlight for JavaScript with line-numbers</h2>
37+
<pre class="pat-syntax-highlight"
38+
data-pat-syntax-highlight="features: line-numbers, line-highlight"
39+
data-line="1-2, 4"
40+
>
41+
<code class="language-javascript">
42+
const pattern = registry.patterns[name];
43+
if (pattern.transform) {
44+
try {
45+
pattern.transform($content);
46+
} catch (e) {
47+
if (dont_catch) {
48+
throw(e);
49+
}
50+
log.error("Transform error for pattern" + name, e);
51+
}
52+
}
53+
</code>
54+
</pre>
55+
</body>
2356
</html>
Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,62 @@
11
import Base from "../../core/base";
2-
import utils from "../../core/utils";
2+
import Parser from "../../core/parser";
3+
4+
export const parser = new Parser("syntax-highlight");
5+
parser.addArgument("language", "html");
6+
parser.addArgument("theme", "dark", ["dark", "light"]);
7+
parser.addArgument("features", null, ["line-highlight", "line-numbers"], true);
38

49
export default Base.extend({
510
name: "syntax-highlight",
611
trigger: ".pat-syntax-highlight",
712

813
async init() {
9-
const Prettify = (await import("google-code-prettify/src/prettify")).default; // prettier-ignore
10-
this.$el.addClass("prettyprint");
11-
utils.debounce(Prettify.prettyPrint, 50)();
14+
this.options = parser.parse(this.el, this.options);
15+
16+
let theme;
17+
if (this.options.theme === "light") {
18+
theme = "";
19+
} else if (this.options.theme === "dark") {
20+
theme = "okaidia";
21+
} else {
22+
theme = this.options.theme;
23+
}
24+
25+
import(`prismjs/themes/prism${theme ? "-" + theme : ""}.css`);
26+
const Prism = (await import("prismjs")).default;
27+
28+
if (this.options.features.includes("line-highlight")) {
29+
import("prismjs/plugins/line-highlight/prism-line-highlight.css");
30+
await import("prismjs/plugins/line-highlight/prism-line-highlight.js");
31+
}
32+
33+
if (this.options.features.includes("line-numbers")) {
34+
import("prismjs/plugins/line-numbers/prism-line-numbers.css");
35+
await import("prismjs/plugins/line-numbers/prism-line-numbers.js");
36+
this.el.classList.add("line-numbers");
37+
}
38+
39+
Prism.manual = true;
40+
41+
let _el = this.el;
42+
const code_el = [...this.el.children].filter((it) => it.tagName === "CODE")?.[0];
43+
if (code_el) {
44+
_el = code_el;
45+
}
46+
47+
// Get the language
48+
let language = [..._el.classList, ...this.el.classList]
49+
.filter((it) => {
50+
return it.startsWith("language-") || it.startsWith("lang-");
51+
})?.[0]
52+
?.replace("language-", "")
53+
?.replace("lang-", "");
54+
// CSS class language always win.
55+
language = language || this.options.language || "html";
56+
// Set the language on the code element (ignored if already set)
57+
this.el.classList.add(`language-${language}`);
58+
_el.classList.add(`language-${language}`);
59+
60+
Prism.highlightElement(_el);
1261
},
1362
});

0 commit comments

Comments
 (0)