Skip to content

Commit d57d61a

Browse files
committed
feat:添加控制是否解析标题进行返回数据
1 parent c595d0b commit d57d61a

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

core/README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ export type Options = {
9999
* Add babel plugins.
100100
*/
101101
babelPlugins?: PluginItem[];
102+
/**Do you want to parse the title*/
103+
isHeading?: boolean
102104
}
103105
```
104106
@@ -112,6 +114,7 @@ import mdObj from 'markdown-react-code-preview-loader/README.md';
112114
mdObj.source // => `README.md` raw string text
113115
mdObj.components // => The component index object, the React component converted from the markdown indexed example. (need to configure meta)
114116
mdObj.data // => The component source code index object, the sample source code indexed from markdown. (need to configure meta)
117+
mdObj.headings // => This is the parsed header data
115118
```
116119

117120
```js
@@ -133,7 +136,8 @@ mdObj.data // => The component source code index object, the sample source
133136
}
134137
},
135138
components: { 77: ƒ, demo12: ƒ },
136-
source: "# Alert 确认对话框...."
139+
source: "# Alert 确认对话框....",
140+
headings:[{depth:1,value:"标题", ...},...]
137141
}
138142
```
139143

@@ -155,6 +159,7 @@ export type CodeBlockData = {
155159
source: string;
156160
components: Record<CodeBlockItem['name'], React.FC>;
157161
data: Record<CodeBlockItem['name'], CodeBlockItem>;
162+
headings?: HeadingItem[]
158163
};
159164
```
160165

core/src/index.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import { PluginItem } from '@babel/core';
33
import { Options as RIOptions } from 'babel-plugin-transform-remove-imports';
4-
import { getProcessor, getCodeBlock, getHeadings } from './utils';
4+
import { getProcessor, getCodeBlock, getHeadings, HeadingItem } from './utils';
55
import { LoaderDefinitionFunction } from 'webpack';
66
export * from './utils';
77

@@ -22,6 +22,7 @@ export type CodeBlockData = {
2222
source: string;
2323
components: Record<CodeBlockItem['name'], React.FC>;
2424
data: Record<CodeBlockItem['name'], CodeBlockItem>;
25+
headings?: HeadingItem[];
2526
};
2627

2728
export const FUNNAME_PREFIX = '__BaseCode__';
@@ -40,6 +41,8 @@ export type Options = {
4041
* Add babel plugins.
4142
*/
4243
babelPlugins?: PluginItem[];
44+
/**是否进行标题解析*/
45+
isHeading?: boolean;
4346
};
4447

4548
const codePreviewLoader: LoaderDefinitionFunction = function (source) {
@@ -57,7 +60,7 @@ const codePreviewLoader: LoaderDefinitionFunction = function (source) {
5760
this.emitError(error);
5861
}
5962

60-
const headings = getHeadings(child);
63+
const headings = options.isHeading ? getHeadings(child) : [];
6164

6265
return `\nexport default {
6366
components: { ${components} },

0 commit comments

Comments
 (0)