Skip to content

Commit d41d8aa

Browse files
authored
💄 style: make latex always ltr (#253)
* 🐛 fix: make latex always ltr * Update rehypePlugin.ts
1 parent a2fb7d5 commit d41d8aa

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/Markdown/index.tsx

+7-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import type { AProps } from '@/types';
2323
import { CodeFullFeatured, CodeLite } from './CodeBlock';
2424
import type { TypographyProps } from './Typography';
2525
import { useStyles as useMarkdownStyles } from './markdown.style';
26+
import { rehypeKatexDir } from './rehypePlugin';
2627
import { useStyles } from './style';
2728
import { escapeBrackets, escapeMhchem, fixMarkdownBold } from './utils';
2829

@@ -139,9 +140,12 @@ const Markdown = memo<MarkdownProps>(
139140

140141
const memoRehypePlugins = useMemo(
141142
() =>
142-
[allowHtml && rehypeRaw, enableLatex && rehypeKatex, ...innerRehypePlugins].filter(
143-
Boolean,
144-
) as any,
143+
[
144+
allowHtml && rehypeRaw,
145+
enableLatex && rehypeKatex,
146+
enableLatex && rehypeKatexDir,
147+
...innerRehypePlugins,
148+
].filter(Boolean) as any,
145149
[allowHtml, enableLatex, ...innerRehypePlugins],
146150
);
147151

src/Markdown/rehypePlugin.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// katex-directive
2+
// 给 class="katex" 的节点加上 dir="ltr" 属性
3+
import type { Node } from 'unist';
4+
import { visit } from 'unist-util-visit';
5+
6+
// eslint-disable-next-line unicorn/consistent-function-scoping
7+
export const rehypeKatexDir = () => (tree: Node) => {
8+
visit(tree, 'element', (node: any) => {
9+
if (node.properties?.className?.includes('katex')) {
10+
node.properties.dir = 'ltr';
11+
}
12+
});
13+
};

0 commit comments

Comments
 (0)