Skip to content

Commit d680e3e

Browse files
authored
fix: Fixed code highlighting - show line number, highlight line (#249)
1 parent ddcc2c4 commit d680e3e

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

core/src/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import rehypeRewrite from 'rehype-rewrite';
66
import rehypeAttrs from 'rehype-attr';
77
import rehypeRaw from 'rehype-raw';
88
import { reservedMeta } from './plugins/reservedMeta';
9+
import { retrieveMeta } from './plugins/retrieveMeta';
910
import { rehypeRewriteHandle, defaultRehypePlugins } from './rehypePlugins';
1011

1112
export * from './preview';
@@ -14,6 +15,7 @@ export default React.forwardRef<MarkdownPreviewRef, MarkdownPreviewProps>((props
1415
const rehypePlugins: PluggableList = [
1516
reservedMeta,
1617
rehypeRaw,
18+
retrieveMeta,
1719
[rehypePrism, { ignoreMissing: true }],
1820
...defaultRehypePlugins,
1921
[rehypeRewrite, { rewrite: rehypeRewriteHandle(props.disableCopy ?? false, props.rehypeRewrite) }],

core/src/nohighlight.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import MarkdownPreview, { type MarkdownPreviewProps, type MarkdownPreviewRef } f
33
import { PluggableList } from 'unified';
44
import rehypeRewrite from 'rehype-rewrite';
55
import { reservedMeta } from './plugins/reservedMeta';
6+
import { retrieveMeta } from './plugins/retrieveMeta';
67
import rehypeAttrs from 'rehype-attr';
78
import rehypeRaw from 'rehype-raw';
89
import { rehypeRewriteHandle, defaultRehypePlugins } from './rehypePlugins';
@@ -11,6 +12,7 @@ export default React.forwardRef<MarkdownPreviewRef, MarkdownPreviewProps>((props
1112
const rehypePlugins: PluggableList = [
1213
reservedMeta,
1314
rehypeRaw,
15+
retrieveMeta,
1416
...defaultRehypePlugins,
1517
[rehypeRewrite, { rewrite: rehypeRewriteHandle(props.disableCopy ?? false, props.rehypeRewrite) }],
1618
[rehypeAttrs, { properties: 'attr' }],

core/src/plugins/reservedMeta.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import { Plugin } from 'unified';
2-
import { Root, RootContent, Element } from 'hast';
2+
import { Root, RootContent } from 'hast';
33
import { visit } from 'unist-util-visit';
44

55
export interface ReservedMetaOptions {}
66

77
export const reservedMeta: Plugin<[ReservedMetaOptions?], Root> = (options = {}) => {
88
return (tree) => {
9-
visit(tree, (node: Root | RootContent | Element) => {
10-
if (node.type === 'element' && node.tagName === 'code' && node.properties && node.properties.meta) {
11-
node.properties = { ...node.properties, 'data-meta': String(node.properties.meta) };
12-
}
9+
visit(tree, (node: Root | RootContent) => {
1310
if (node.type === 'element' && node.tagName === 'code' && node.data && node.data.meta) {
1411
node.properties = { ...node.properties, 'data-meta': String(node.data.meta) };
1512
}

core/src/plugins/retrieveMeta.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Plugin } from 'unified';
2+
import { Root, RootContent } from 'hast';
3+
import { visit } from 'unist-util-visit';
4+
5+
export interface RetrieveMetaOptions {}
6+
7+
export const retrieveMeta: Plugin<[RetrieveMetaOptions?], Root> = (options = {}) => {
8+
return (tree) => {
9+
visit(tree, (node: Root | RootContent) => {
10+
if (node.type === 'element' && node.tagName === 'code' && node.properties && node.properties['dataMeta']) {
11+
if (!node.data) {
12+
node.data = {};
13+
}
14+
node.data.meta = node.properties['dataMeta'];
15+
delete node.properties['dataMeta'];
16+
}
17+
});
18+
};
19+
};

0 commit comments

Comments
 (0)