Skip to content

Commit

Permalink
fix: Fixed code highlighting - show line number, highlight line (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
RARgames committed Nov 23, 2023
1 parent ddcc2c4 commit d680e3e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
2 changes: 2 additions & 0 deletions core/src/index.tsx
Expand Up @@ -6,6 +6,7 @@ import rehypeRewrite from 'rehype-rewrite';
import rehypeAttrs from 'rehype-attr';
import rehypeRaw from 'rehype-raw';
import { reservedMeta } from './plugins/reservedMeta';
import { retrieveMeta } from './plugins/retrieveMeta';
import { rehypeRewriteHandle, defaultRehypePlugins } from './rehypePlugins';

export * from './preview';
Expand All @@ -14,6 +15,7 @@ export default React.forwardRef<MarkdownPreviewRef, MarkdownPreviewProps>((props
const rehypePlugins: PluggableList = [
reservedMeta,
rehypeRaw,
retrieveMeta,
[rehypePrism, { ignoreMissing: true }],
...defaultRehypePlugins,
[rehypeRewrite, { rewrite: rehypeRewriteHandle(props.disableCopy ?? false, props.rehypeRewrite) }],
Expand Down
2 changes: 2 additions & 0 deletions core/src/nohighlight.tsx
Expand Up @@ -3,6 +3,7 @@ import MarkdownPreview, { type MarkdownPreviewProps, type MarkdownPreviewRef } f
import { PluggableList } from 'unified';
import rehypeRewrite from 'rehype-rewrite';
import { reservedMeta } from './plugins/reservedMeta';
import { retrieveMeta } from './plugins/retrieveMeta';
import rehypeAttrs from 'rehype-attr';
import rehypeRaw from 'rehype-raw';
import { rehypeRewriteHandle, defaultRehypePlugins } from './rehypePlugins';
Expand All @@ -11,6 +12,7 @@ export default React.forwardRef<MarkdownPreviewRef, MarkdownPreviewProps>((props
const rehypePlugins: PluggableList = [
reservedMeta,
rehypeRaw,
retrieveMeta,
...defaultRehypePlugins,
[rehypeRewrite, { rewrite: rehypeRewriteHandle(props.disableCopy ?? false, props.rehypeRewrite) }],
[rehypeAttrs, { properties: 'attr' }],
Expand Down
7 changes: 2 additions & 5 deletions core/src/plugins/reservedMeta.ts
@@ -1,15 +1,12 @@
import { Plugin } from 'unified';
import { Root, RootContent, Element } from 'hast';
import { Root, RootContent } from 'hast';
import { visit } from 'unist-util-visit';

export interface ReservedMetaOptions {}

export const reservedMeta: Plugin<[ReservedMetaOptions?], Root> = (options = {}) => {
return (tree) => {
visit(tree, (node: Root | RootContent | Element) => {
if (node.type === 'element' && node.tagName === 'code' && node.properties && node.properties.meta) {
node.properties = { ...node.properties, 'data-meta': String(node.properties.meta) };
}
visit(tree, (node: Root | RootContent) => {
if (node.type === 'element' && node.tagName === 'code' && node.data && node.data.meta) {
node.properties = { ...node.properties, 'data-meta': String(node.data.meta) };
}
Expand Down
19 changes: 19 additions & 0 deletions core/src/plugins/retrieveMeta.ts
@@ -0,0 +1,19 @@
import { Plugin } from 'unified';
import { Root, RootContent } from 'hast';
import { visit } from 'unist-util-visit';

export interface RetrieveMetaOptions {}

export const retrieveMeta: Plugin<[RetrieveMetaOptions?], Root> = (options = {}) => {
return (tree) => {
visit(tree, (node: Root | RootContent) => {
if (node.type === 'element' && node.tagName === 'code' && node.properties && node.properties['dataMeta']) {
if (!node.data) {
node.data = {};
}
node.data.meta = node.properties['dataMeta'];
delete node.properties['dataMeta'];
}
});
};
};

0 comments on commit d680e3e

Please sign in to comment.