Skip to content

Commit b51f018

Browse files
committed
feat: add common syntax highlight entry. #291
1 parent 367c06a commit b51f018

5 files changed

Lines changed: 74 additions & 1 deletion

File tree

core/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,29 @@ export default function Demo() {
216216
}
217217
```
218218

219+
## Common Code Highlight
220+
221+
Use `@uiw/react-markdown-preview/common` to keep syntax highlighting enabled with the `rehype-prism-plus/common` language subset. This is a middle ground between the default all-language build and `nohighlight`.
222+
223+
```jsx mdx:preview
224+
import React from 'react';
225+
import MarkdownPreview from '@uiw/react-markdown-preview/common';
226+
227+
const source = `
228+
\`\`\`js
229+
function greet(name) {
230+
console.log('hello', name);
231+
}
232+
\`\`\`
233+
`;
234+
235+
export default function Demo() {
236+
return (
237+
<MarkdownPreview source={source} style={{ padding: 16 }} />
238+
);
239+
}
240+
```
241+
219242
## Remove Code Highlight
220243

221244
The following example can help you _exclude code highlighting code_<!--rehype:style=color: #333;background-color: rgb(196 255 122 / 86%);--> from being included in the bundle. `@uiw/react-markdown-preview/nohighlight`<!--rehype:style=color: #e24444;--> component does not contain the `rehype-prism-plus` code highlighting package, `showLineNumbers` and `highlight line` functions will no longer work. ([#586](https://github.com/uiwjs/react-md-editor/issues/586))

core/common.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
declare module '@uiw/react-markdown-preview/common' {
2+
import React from 'react';
3+
import { MarkdownPreviewProps, MarkdownPreviewRef } from '@uiw/react-markdown-preview/lib/Props';
4+
export * from '@uiw/react-markdown-preview/lib/Props';
5+
const _default: React.ForwardRefExoticComponent<MarkdownPreviewProps & React.RefAttributes<MarkdownPreviewRef>>;
6+
export default _default;
7+
}

core/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
"types": "./lib/index.d.ts",
1616
"require": "./lib/index.js"
1717
},
18+
"./common": {
19+
"import": "./esm/common.js",
20+
"types": "./lib/common.d.ts",
21+
"require": "./lib/common.js"
22+
},
1823
"./nohighlight": {
1924
"import": "./esm/nohighlight.js",
2025
"types": "./lib/nohighlight.d.ts",
@@ -40,6 +45,7 @@
4045
"dist",
4146
"lib",
4247
"esm",
48+
"common.d.ts",
4349
"nohighlight.d.ts",
4450
"markdown.css",
4551
"src/**/*.{ts,tsx,less}"

core/src/common.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import React from 'react';
2+
import rehypePrism from 'rehype-prism-plus/common';
3+
import type { PluggableList } from 'unified';
4+
import rehypeRewrite from 'rehype-rewrite';
5+
import rehypeAttrs from 'rehype-attr';
6+
import rehypeRaw from 'rehype-raw';
7+
import MarkdownPreview from './preview';
8+
import { reservedMeta } from './plugins/reservedMeta';
9+
import { retrieveMeta } from './plugins/retrieveMeta';
10+
import { rehypeRewriteHandle, defaultRehypePlugins } from './rehypePlugins';
11+
import type { MarkdownPreviewProps, MarkdownPreviewRef } from './Props';
12+
13+
export * from './Props';
14+
15+
export default React.forwardRef<MarkdownPreviewRef, MarkdownPreviewProps>((props, ref) => {
16+
const rehypePlugins: PluggableList = [
17+
reservedMeta,
18+
rehypeRaw,
19+
retrieveMeta,
20+
...defaultRehypePlugins,
21+
[rehypeRewrite, { rewrite: rehypeRewriteHandle(props.disableCopy ?? false, props.rehypeRewrite) }],
22+
[rehypeAttrs, { properties: 'attr' }],
23+
...(props.rehypePlugins || []),
24+
[rehypePrism, { ignoreMissing: true }],
25+
];
26+
return <MarkdownPreview {...props} rehypePlugins={rehypePlugins} ref={ref} />;
27+
});

test/index.test.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { useEffect, useRef } from 'react';
22
import TestRenderer from 'react-test-renderer';
33
import { render } from '@testing-library/react';
44
import MarkdownPreview, { MarkdownPreviewRef } from '../core/src';
5+
import CommonMarkdownPreview from '../core/src/common';
56

67
it('Should output a TestRenderer', async () => {
78
const component = TestRenderer.create(<MarkdownPreview source="## Hello World!" />);
@@ -60,4 +61,13 @@ it('MarkdownPreview Ref', async () => {
6061
);
6162
}
6263
render(<Demo />);
63-
});
64+
});
65+
66+
it('Common MarkdownPreview entrypoint', async () => {
67+
const component = TestRenderer.create(<CommonMarkdownPreview source="```js\nconst a = 1;\n```" />);
68+
const tree = component.toJSON();
69+
if (tree && !Array.isArray(tree)) {
70+
expect(tree.type).toEqual('div');
71+
expect(tree.props.className).toEqual('wmde-markdown wmde-markdown-color ');
72+
}
73+
});

0 commit comments

Comments
 (0)