Skip to content

Commit 85ed7d5

Browse files
committed
add custom plugins for prism
1 parent 5c16832 commit 85ed7d5

File tree

6 files changed

+174
-10
lines changed

6 files changed

+174
-10
lines changed

docusaurus.config.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ const config = {
6767
theme: {
6868
customCss: ["./src/css/custom.css"],
6969
},
70-
sitemap: {
71-
lastmod: 'date',
72-
changefreq: 'weekly',
73-
priority: 0.5,
74-
filename: 'sitemap.xml',
70+
sitemap: {
71+
lastmod: "date",
72+
changefreq: "weekly",
73+
priority: 0.5,
74+
filename: "sitemap.xml",
7575
},
7676
}),
7777
],
@@ -141,14 +141,23 @@ const config = {
141141
prism: {
142142
theme: prismThemes.github,
143143
darkTheme: prismThemes.oneDark,
144-
additionalLanguages: ["bash", "apacheconf"],
145-
plugins: ["line-numbers", "command-line"],
144+
additionalLanguages: [
145+
"bash",
146+
"apacheconf",
147+
"custom-yaml-scalar",
148+
"custom-go-template",
149+
"custom-yaml-go-template",
150+
"custom-yaml-empty-key",
151+
],
146152
},
147153
metadata: [
148-
{ name: "google-site-verification", content: "g8d_0UGQgwAYseQGOOqRvsTPup3xawCbb-i2jT9HyVc" },
154+
{
155+
name: "google-site-verification",
156+
content: "g8d_0UGQgwAYseQGOOqRvsTPup3xawCbb-i2jT9HyVc",
157+
},
149158
{ name: "og:site_name", content: "Secured Signal API" },
150-
]
151-
}),
159+
],
160+
}),
152161
}
153162

154163
export default config
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import siteConfig from "@generated/docusaurus.config"
2+
export default function prismIncludeLanguages(PrismObject) {
3+
const {
4+
themeConfig: { prism },
5+
} = siteConfig
6+
const { additionalLanguages } = prism
7+
// Prism components work on the Prism instance on the window, while prism-
8+
// react-renderer uses its own Prism instance. We temporarily mount the
9+
// instance onto window, import components to enhance it, then remove it to
10+
// avoid polluting global namespace.
11+
// You can mutate PrismObject: registering plugins, deleting languages... As
12+
// long as you don't re-assign it
13+
const PrismBefore = globalThis.Prism
14+
globalThis.Prism = PrismObject
15+
additionalLanguages.forEach((lang) => {
16+
if (lang.startsWith("custom-")) {
17+
const customLang = lang.slice("custom-".length)
18+
19+
require(`./prism/custom/prism-${customLang}`)
20+
} else {
21+
require(`prismjs/components/prism-${lang}`)
22+
}
23+
})
24+
25+
// Clean up and eventually restore former globalThis.Prism object (if any)
26+
delete globalThis.Prism
27+
if (typeof PrismBefore !== "undefined") {
28+
globalThis.Prism = PrismObject
29+
}
30+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const lang = "go-template"
2+
3+
;(function (Prism) {
4+
Prism.languages[lang] = {
5+
// Go template comments {{/* ... */}}
6+
"go-template-comment": {
7+
pattern: /\{\{\/\*[\s\S]*?\*\/\}\}/,
8+
greedy: true,
9+
alias: "comment",
10+
inside: {
11+
delimiter: /^\{\{\/\*|\*\/\}\}/,
12+
content: /[\s\S]+/, // the inner comment content
13+
},
14+
},
15+
16+
// Regular Go template expressions {{ ... }}
17+
"go-template-variable": {
18+
pattern: /\{\{(?!\/\*)[\s\S]+?\}\}/,
19+
greedy: true,
20+
alias: "variable",
21+
inside: {
22+
delimiter: /^\{\{|\}\}$/,
23+
expression: /[\s\S]+/, // the inner expression
24+
},
25+
},
26+
}
27+
})(Prism)
28+
29+
export function implement(Prism, object) {
30+
if (!object) return
31+
32+
const base = Prism.languages[lang]
33+
34+
object.inside = object.inside || {}
35+
36+
Object.keys(base).forEach((attribute) => {
37+
object.inside[attribute] = base[attribute]
38+
})
39+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
;(function (Prism) {
2+
// https://yaml.org/spec/1.2/spec.html#c-ns-anchor-property
3+
// https://yaml.org/spec/1.2/spec.html#c-ns-alias-node
4+
var anchorOrAlias = /[*&][^\s[\]{},]+/
5+
// https://yaml.org/spec/1.2/spec.html#c-ns-tag-property
6+
var tag =
7+
/!(?:<[\w\-%#;/?:@&=+$,.!~*'()[\]]+>|(?:[a-zA-Z\d-]*!)?[\w\-%#;/?:@&=+$.~*'()]+)?/
8+
// https://yaml.org/spec/1.2/spec.html#c-ns-properties(n,c)
9+
var properties =
10+
"(?:" +
11+
tag.source +
12+
"(?:[ \t]+" +
13+
anchorOrAlias.source +
14+
")?|" +
15+
anchorOrAlias.source +
16+
"(?:[ \t]+" +
17+
tag.source +
18+
")?)"
19+
// https://yaml.org/spec/1.2/spec.html#ns-plain(n,c)
20+
// This is a simplified version that doesn't support "#" and multiline keys
21+
// All these long scarry character classes are simplified versions of YAML's characters
22+
var plainKey =
23+
/(?:[^\s\x00-\x08\x0e-\x1f!"#%&'*,\-:>?@[\]`{|}\x7f-\x84\x86-\x9f\ud800-\udfff\ufffe\uffff]|[?:-]<PLAIN>)(?:[ \t]*(?:(?![#:])<PLAIN>|:<PLAIN>))*/.source.replace(
24+
/<PLAIN>/g,
25+
function () {
26+
return /[^\s\x00-\x08\x0e-\x1f,[\]{}\x7f-\x84\x86-\x9f\ud800-\udfff\ufffe\uffff]/
27+
.source
28+
}
29+
)
30+
var string = /"(?:[^"\\\r\n]|\\.)*"|'(?:[^'\\\r\n]|\\.)*'/.source
31+
32+
globalThis.Prism.languages.yaml["key"] = {
33+
pattern: RegExp(
34+
/((?:^|[:\-,[{\r\n?])[ \t]*(?:<<prop>>[ \t]+)?)<<key>>(?=\s*:(?:\s*|$))/.source
35+
.replace(/<<prop>>/g, function () {
36+
return properties
37+
})
38+
.replace(/<<key>>/g, function () {
39+
return "(?:" + plainKey + "|" + string + ")"
40+
})
41+
),
42+
greedy: true,
43+
lookbehind: true,
44+
alias: "atrule",
45+
}
46+
})(Prism)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { implement } from "./prism-go-template"
2+
;(function (Prism) {
3+
const yaml = Prism.languages.yaml
4+
if (!yaml) return
5+
6+
implement(Prism, yaml.scalar)
7+
implement(Prism, yaml.string)
8+
})(Prism)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
;(function (Prism) {
2+
// https://yaml.org/spec/1.2/spec.html#c-ns-anchor-property
3+
// https://yaml.org/spec/1.2/spec.html#c-ns-alias-node
4+
var anchorOrAlias = /[*&][^\s[\]{},]+/
5+
// https://yaml.org/spec/1.2/spec.html#c-ns-tag-property
6+
var tag =
7+
/!(?:<[\w\-%#;/?:@&=+$,.!~*'()[\]]+>|(?:[a-zA-Z\d-]*!)?[\w\-%#;/?:@&=+$.~*'()]+)?/
8+
// https://yaml.org/spec/1.2/spec.html#c-ns-properties(n,c)
9+
var properties =
10+
"(?:" +
11+
tag.source +
12+
"(?:[ \t]+" +
13+
anchorOrAlias.source +
14+
")?|" +
15+
anchorOrAlias.source +
16+
"(?:[ \t]+" +
17+
tag.source +
18+
")?)"
19+
20+
globalThis.Prism.languages.yaml["scalar"] = {
21+
pattern: RegExp(
22+
/([\-:]\s*(?:\s<<prop>>[ \t]+)?[|>])[ \t]*([\s\S]*?)(?=(\r?\n[\S\-]|$))/.source.replace(
23+
/<<prop>>/g,
24+
function () {
25+
return properties
26+
}
27+
)
28+
),
29+
lookbehind: true,
30+
alias: "string",
31+
}
32+
})(Prism)

0 commit comments

Comments
 (0)