Skip to content

Commit

Permalink
💄 style: make latex always ltr (#253)
Browse files Browse the repository at this point in the history
* 🐛 fix: make latex always ltr

* Update rehypePlugin.ts
  • Loading branch information
arvinxx authored Jan 26, 2025
1 parent a2fb7d5 commit d41d8aa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Markdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type { AProps } from '@/types';
import { CodeFullFeatured, CodeLite } from './CodeBlock';
import type { TypographyProps } from './Typography';
import { useStyles as useMarkdownStyles } from './markdown.style';
import { rehypeKatexDir } from './rehypePlugin';
import { useStyles } from './style';
import { escapeBrackets, escapeMhchem, fixMarkdownBold } from './utils';

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

const memoRehypePlugins = useMemo(
() =>
[allowHtml && rehypeRaw, enableLatex && rehypeKatex, ...innerRehypePlugins].filter(
Boolean,
) as any,
[
allowHtml && rehypeRaw,
enableLatex && rehypeKatex,
enableLatex && rehypeKatexDir,
...innerRehypePlugins,
].filter(Boolean) as any,
[allowHtml, enableLatex, ...innerRehypePlugins],
);

Expand Down
13 changes: 13 additions & 0 deletions src/Markdown/rehypePlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// katex-directive
// 给 class="katex" 的节点加上 dir="ltr" 属性
import type { Node } from 'unist';
import { visit } from 'unist-util-visit';

// eslint-disable-next-line unicorn/consistent-function-scoping
export const rehypeKatexDir = () => (tree: Node) => {
visit(tree, 'element', (node: any) => {
if (node.properties?.className?.includes('katex')) {
node.properties.dir = 'ltr';
}
});
};

0 comments on commit d41d8aa

Please sign in to comment.