Skip to content

Commit 6f8993f

Browse files
authored
fixup! [NFY] Setup Typst syntax highlighting via WASM module (#21)
1 parent 6085b8a commit 6f8993f

5 files changed

Lines changed: 40 additions & 27 deletions

File tree

build.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { build } from "esbuild";
2+
import { wasmLoader } from 'esbuild-plugin-wasm'
23

34
await build({
45
entryPoints: ["src/codemirror-bundle.js"],
@@ -11,5 +12,9 @@ await build({
1112
outbase: "src",
1213
assetNames: "[name]",
1314
entryNames: "codemirror",
15+
plugins: [wasmLoader()],
1416
publicPath: "/",
17+
supported: {
18+
'top-level-await': true,
19+
},
1520
});

package-lock.json

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"autoprefixer": "^10.4.7",
1919
"bulma": "^0.9.3",
2020
"esbuild": "^0.27.0",
21+
"esbuild-plugin-wasm": "^1.1.0",
2122
"pagedjs": "^0.4.3",
2223
"pagedjs-cli": "^0.4.3",
2324
"postcss-cli": "^10.1.0",

src/codemirror-bundle.js

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { markdown } from "@codemirror/lang-markdown";
1111
import { xml } from "@codemirror/lang-xml";
1212
import { yaml } from "@codemirror/lang-yaml";
1313
import { latex } from "codemirror-lang-latex";
14+
import { typst } from "codemirror-lang-typst";
1415

1516
const languageMap = {
1617
// Official CodeMirror languages
@@ -22,14 +23,15 @@ const languageMap = {
2223
yaml,
2324
// Unofficial 3rd party languages
2425
latex,
26+
typst,
2527
// Aliases to near-matches
2628
pagedjs: html,
2729
sile: latex,
2830
weasyprint: html,
2931
xelatex: latex,
3032
};
3133

32-
window.initCodeMirror = async function (element, content, language) {
34+
export async function initCodeMirror(element, content, language) {
3335
let polyTheme = EditorView.baseTheme({
3436
"&light": {
3537
backgroundColor: "#fff",
@@ -39,27 +41,12 @@ window.initCodeMirror = async function (element, content, language) {
3941
},
4042
});
4143

42-
let languageExtension = [];
43-
const spec = languageMap[language];
44-
if (spec) {
45-
const ext = spec();
46-
languageExtension = ext instanceof Promise ? await ext : ext;
47-
} else if (language === "typst") {
48-
console.log("Loading Typst language support");
49-
try {
50-
const { typst } = await import("codemirror-lang-typst");
51-
languageExtension = typst();
52-
} catch (e) {
53-
console.warn("Failed to load Typst language support:", e);
54-
}
55-
}
56-
5744
const extensions = [
5845
EditorState.readOnly.of(true),
5946
polyTheme,
6047
lineNumbers(),
6148
syntaxHighlighting(defaultHighlightStyle),
62-
languageExtension,
49+
languageMap[language] ? languageMap[language]() : [],
6350
];
6451

6552
let div = document.createElement("div");

templates/default.html

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,21 @@
99
<link rel="alternate" type="application/rss+xml" title="RSS" href="{{ get_url(path=config.feed_filenames[0]) }}">
1010
{% endblock %}
1111
<link rel="stylesheet" href="{{ get_url(path="main.css") }}">
12-
<script type="module" src="{{ get_url(path="codemirror.js") }}"></script>
12+
<script type="module">
13+
import { initCodeMirror } from '{{ get_url(path="codemirror.js") }}'
14+
function setupCodeMirror() {
15+
document.querySelectorAll('pre.sample').forEach(async function(container) {
16+
const content = container.textContent;
17+
const language = container.dataset.language;
18+
await initCodeMirror(container, content, language);
19+
});
20+
}
21+
if (document.readyState === "loading") {
22+
document.addEventListener('DOMContentLoaded', setupCodeMirror);
23+
} else {
24+
setupCodeMirror();
25+
}
26+
</script>
1327
</head>
1428
<body>
1529
<!--
@@ -27,14 +41,5 @@ <h1 class="title is-1 navbar-brand"><a href="{{ config.base_url }}">{{ config.ti
2741
{%- endblock %}
2842
</div>
2943
</section>
30-
<script type="module">
31-
document.addEventListener('DOMContentLoaded', function() {
32-
document.querySelectorAll('pre.sample').forEach(async function(container) {
33-
const content = container.textContent;
34-
const language = container.dataset.language;
35-
await window.initCodeMirror(container, content, language);
36-
});
37-
});
38-
</script>
3944
</body>
4045
</html>

0 commit comments

Comments
 (0)