Skip to content

Commit 6e48d45

Browse files
committed
feat: post-specific katex macros
1 parent f3d45ed commit 6e48d45

4 files changed

Lines changed: 10 additions & 6 deletions

File tree

src/assets/ts/latex.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ const replaceNewcommandWithGdef = (raw: string): string => {
4545
};
4646

4747

48-
const _render = (options: KatexOptions) => (raw: string) => {
48+
const _render = (options: KatexOptions) => (raw: string, macros?: any) => {
4949
raw = replaceNewcommandWithGdef(raw);
5050

5151
try {
52-
return katex.renderToString(raw, options);
52+
return katex.renderToString(raw, { ...options, macros: macros || {} });
5353
} catch (error) {
5454
console.error(error);
5555
return `<span style="color: red">${raw}</span>`;
@@ -63,6 +63,6 @@ const config: KatexOptions = {
6363

6464
const render_inline = _render({ displayMode: false, ...config });
6565
const render_block = _render({ displayMode: true, ...config });
66-
const render = (raw: string, opts: KatexOptions) => _render(opts)(raw);
66+
const render = (raw: string, opts: KatexOptions, macros?: any) => _render(opts)(raw, macros);
6767

6868
export { render, render_inline, render_block };

src/components/md/BlockMath.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<script setup lang="ts">
2+
import { inject } from "vue";
23
import { render_block } from "@/assets/ts/latex";
34
import { OverlayScrollbarsComponent } from "overlayscrollbars-vue";
45
56
import type { PartialOptions } from "overlayscrollbars";
67
78
const props = defineProps<{ data: string }>();
8-
const parsed: string = render_block(props.data);
9+
const macros = inject("katex-macros", {});
10+
const parsed: string = render_block(props.data, macros);
911
1012
/** @see https://github.com/KingSora/OverlayScrollbars/ */
1113
const osOptions: PartialOptions = {

src/components/md/InlineMath.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<script setup lang="ts">
2+
import { inject } from "vue";
23
import { render_inline } from "@/assets/ts/latex";
34
const props = defineProps<{ data: string }>();
45
5-
const parsed: string = render_inline(props.data);
6+
const macros = inject("katex-macros", {});
7+
const parsed: string = render_inline(props.data, macros);
68
</script>
79

810
<template>

0 commit comments

Comments
 (0)