Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fixed code highlighting - show line number, highlight line #249

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/src/index.tsx
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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'];
}
});
};
};
You are viewing a condensed version of this merge commit. You can view the full changes here.