Skip to content

Commit cf0f6e3

Browse files
committed
feat(pat-markdown): Soft-depend on pat-syntax-highlight.
Only highlight code blocks when the pattern is available. Not hard-depending and importing pat-syntax-highlight fixes a problem where only including pat-markup also included pat-syntax-highlight and it's big highlight.js library. This can reduce the generated bundle size significantly.
1 parent 1f81238 commit cf0f6e3

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/pat/markdown/markdown.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,25 @@ class Pattern extends BasePattern {
2929
async render(text) {
3030
const marked = (await import("marked")).marked;
3131
const DOMPurify = (await import("dompurify")).default;
32-
const SyntaxHighlight = (await import("../syntax-highlight/syntax-highlight")).default; // prettier-ignore
3332

3433
const wrapper = document.createElement("div");
3534
const parsed = DOMPurify.sanitize(marked.parse(text));
3635
wrapper.innerHTML = parsed;
37-
for (const item of wrapper.querySelectorAll("pre > code")) {
38-
const pre = item.parentElement;
39-
pre.classList.add("pat-syntax-highlight");
40-
// If the code block language was set in a fenced code block,
41-
// marked has already set the language as a class on the code tag.
42-
// pat-syntax-highlight will understand this.
43-
new SyntaxHighlight(pre);
44-
await events.await_event(pre, "init.syntax-highlight.patterns");
36+
37+
// If pat-syntax-highlight is available, highlight code blocks.
38+
const SyntaxHighlight = registry.patterns["syntax-highlight"];
39+
if (SyntaxHighlight) {
40+
for (const item of wrapper.querySelectorAll("pre > code")) {
41+
const pre = item.parentElement;
42+
pre.classList.add("pat-syntax-highlight");
43+
// If the code block language was set in a fenced code block,
44+
// marked has already set the language as a class on the code tag.
45+
// pat-syntax-highlight will understand this.
46+
new SyntaxHighlight(pre);
47+
await events.await_event(pre, "init.syntax-highlight.patterns");
48+
}
4549
}
50+
4651
return $(wrapper);
4752
}
4853

src/pat/markdown/markdown.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ describe("pat-markdown", function () {
170170

171171
describe("Code blocks", function () {
172172
it("It correctly renders code blocks", async function () {
173+
await import("../syntax-highlight/syntax-highlight");
174+
173175
document.body.innerHTML = `
174176
<main>
175177
<div class="pat-markdown">
@@ -187,7 +189,6 @@ some content
187189

188190
const instance = new Pattern(document.querySelector(".pat-markdown"));
189191
await events.await_pattern_init(instance);
190-
await utils.timeout(1); // wait a tick for async to settle.
191192

192193
expect(document.body.querySelector("main > div > h1").textContent).toBe("Title"); // prettier-ignore
193194
expect(document.body.querySelector("main > div > p").textContent).toBe("some content"); // prettier-ignore

0 commit comments

Comments
 (0)