Skip to content

Commit 41cf2c5

Browse files
committed
feat: add getMetaId method.
1 parent 21c66ea commit 41cf2c5

File tree

4 files changed

+38
-41
lines changed

4 files changed

+38
-41
lines changed

core/README-zh.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,15 @@ export type CodeBlockData = {
137137
};
138138
```
139139

140+
## getMetaId
141+
142+
```js
143+
import { getMetaId } from 'markdown-react-code-preview-loader';
144+
145+
getMetaId('mdx:preview') // => ''
146+
getMetaId('mdx:preview:demo12') // => 'demo12'
147+
```
148+
140149
## getCodeBlockString
141150

142151
传递 `markdown` 文件内容字符串,返回转换好的需要预览的代码块解析数据。

core/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ export type CodeBlockData = {
136136
};
137137
```
138138

139+
## getMetaId
140+
141+
```js
142+
import { getMetaId } from 'markdown-react-code-preview-loader';
143+
144+
getMetaId('mdx:preview') // => ''
145+
getMetaId('mdx:preview:demo12') // => 'demo12'
146+
```
147+
139148
## getCodeBlockString
140149

141150
Pass the `markdown` file content string, and return the converted code block parsing data that needs to be previewed.

core/src/utils/index.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,17 @@ const getProcessor = (scope: string) => {
1818
}
1919
};
2020

21-
const getMeta = (meta: string | null): Record<string, string | boolean> => {
22-
let metaData: Record<string, string | boolean> = {};
23-
try {
24-
if (meta) {
25-
const [metaItem] = /mdx:(.[\w|:]+)/i.exec(meta) || [];
26-
const [_, field, val] = (metaItem || '').split(':').map((item) => item.trim());
27-
metaData[field] = val || true;
28-
}
29-
} catch (err) {
30-
console.warn(err);
31-
}
32-
return metaData;
21+
/**
22+
* ```js
23+
* 'mdx:preview' => '' // Empty
24+
* 'mdx:preview:demo12' => 'demo12' // return meta id => 'demo12'
25+
* ```
26+
* @param meta string
27+
* @returns string?
28+
*/
29+
export const getMetaId = (meta: string = '') => {
30+
const [metaRaw = ''] = /mdx:(.[\w|:]+)/i.exec(meta) || [];
31+
return metaRaw.replace(/^mdx:preview:?/, '');
3332
};
3433

3534
/** 获取需要渲染的代码块 **/
@@ -41,9 +40,9 @@ const getCodeBlock = (child: MarkDownTreeType['children'], opts: Options = {}) =
4140
child.forEach((item) => {
4241
if (item && item.type === 'code' && lang.includes(item.lang)) {
4342
const line = item.position.start.line;
44-
const metaData = getMeta(item.meta);
45-
if (metaData.preview) {
46-
let name = typeof metaData.preview === 'string' ? metaData.preview : line;
43+
const metaId = getMetaId(item.meta);
44+
if (metaId) {
45+
let name = typeof metaId === 'string' ? metaId : line;
4746
const funName = `BaseCode${line}`;
4847
const returnCode = getTransformValue(item.value, `${funName}.${lang}`, funName, opts);
4948
codeBlock[line] = {

website/src/pages/example/index.tsx

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
11
import MarkdownPreview from '@uiw/react-markdown-preview';
2+
import { getMetaId } from 'markdown-react-code-preview-loader';
23
import PreView from '../../components/CodeLayout';
34
import useMdData from './../../components/useMdData';
45
import { Loader } from 'uiw';
56
import styles from './../index.module.less';
6-
const getMetaData = (meta: string) => {
7-
if (meta) {
8-
const [metaItem] = /mdx:(.[\w|:]+)/i.exec(meta) || [];
9-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
10-
const [_, field, val] = (metaItem || '').split(':').map((item) => item.trim());
11-
if (val) {
12-
return val;
13-
}
14-
}
15-
return '';
16-
};
177

188
export function ExamplePage() {
199
const { mdData, loading } = useMdData((path) => import(`./App${path}.md`));
@@ -25,25 +15,15 @@ export function ExamplePage() {
2515
className={styles.markdown}
2616
components={{
2717
code: ({ inline, node, ...props }) => {
28-
const {
29-
'data-meta': meta,
30-
noPreview,
31-
noScroll,
32-
bgWhite,
33-
noCode,
34-
codePen,
35-
codeSandboxOption,
36-
codeSandbox,
37-
...rest
38-
} = props as any;
18+
const { 'data-meta': meta, ...rest } = props as any;
3919
if (inline) {
4020
return <code {...props} />;
4121
}
4222
const line = node.position?.start.line;
43-
const funName = getMetaData(meta || '') || line;
44-
const Child = mdData.components[funName || ''];
45-
if (funName && typeof Child === 'function') {
46-
const copyNodes = mdData.codeBlock[funName] || '';
23+
const metaId = getMetaId(meta) || line;
24+
const Child = mdData.components[metaId || ''];
25+
if (metaId && typeof Child === 'function') {
26+
const copyNodes = mdData.codeBlock[metaId] || '';
4727
return (
4828
<PreView code={<code {...rest} />} copyNodes={copyNodes}>
4929
<Child />

0 commit comments

Comments
 (0)