Skip to content

Commit 40bef97

Browse files
committed
fix(md-enhance): fix tex conflict with attrs, fix vuepress-theme-hope#2048
1 parent e34a90e commit 40bef97

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

packages/md-enhance/__tests__/unit/attrs.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, it, expect, vi } from "vitest";
22
import { createMarkdown } from "@vuepress/markdown";
33
import MarkdownIt from "markdown-it";
44
import { attrs, getAttrs } from "../../src/node/markdown-it/attrs";
5+
import { katex } from "../../src/node/markdown-it/katex";
56

67
import type { AttrsOptions } from "../../src/shared";
78

@@ -756,3 +757,11 @@ it("should not break code blocks line highlight", () => {
756757

757758
expect(markdownIt.render(src)).not.toContain("1-3");
758759
});
760+
761+
it("should work with katex plugin", () => {
762+
const markdownIt = createMarkdown().use(attrs).use(katex);
763+
764+
expect(markdownIt.render("$a^{3}$")).toBe(
765+
'<p><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>a</mi><mn>3</mn></msup></mrow><annotation encoding="application/x-tex">a^{3}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8141em;"></span><span class="mord"><span class="mord mathnormal">a</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">3</span></span></span></span></span></span></span></span></span></span></span></span></p>\n'
766+
);
767+
});

packages/md-enhance/src/node/markdown-it/katex.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ const inlineTex: RuleInline = (state, silent) => {
129129
}
130130

131131
if (!silent) {
132-
token = state.push("inlineTex", "math", 0);
132+
token = state.push("math_inline", "math", 0);
133133
token.markup = "$";
134134
token.content = state.src.slice(start, match);
135135
}
@@ -184,7 +184,7 @@ const blockTex: RuleBlock = (state, start, end, silent) => {
184184

185185
state.line = next + 1;
186186

187-
const token = state.push("blockTex", "math", 0);
187+
const token = state.push("math_block", "math", 0);
188188

189189
token.block = true;
190190
token.content =
@@ -231,14 +231,13 @@ export const katex: PluginWithOptions<KatexOptions> = (
231231
md,
232232
options = { throwOnError: false }
233233
) => {
234-
md.inline.ruler.after("escape", "inlineTex", inlineTex);
235-
// It’s a workaround here because types issue
236-
md.block.ruler.after("blockquote", "blockTex", blockTex, {
234+
md.inline.ruler.after("escape", "math_inline", inlineTex);
235+
md.block.ruler.after("blockquote", "math_block", blockTex, {
237236
alt: ["paragraph", "reference", "blockquote", "list"],
238237
});
239238

240-
md.renderer.rules["inlineTex"] = (tokens, index): string =>
239+
md.renderer.rules["math_inline"] = (tokens, index): string =>
241240
katexInline(tokens[index].content, options);
242-
md.renderer.rules["blockTex"] = (tokens, index): string =>
241+
md.renderer.rules["math_block"] = (tokens, index): string =>
243242
`${katexBlock(tokens[index].content, options)}\n`;
244243
};

0 commit comments

Comments
 (0)