Skip to content

Commit

Permalink
feat:添加控制是否解析标题进行返回数据
Browse files Browse the repository at this point in the history
  • Loading branch information
SunLxy committed Jul 20, 2023
1 parent c595d0b commit d57d61a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
7 changes: 6 additions & 1 deletion core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ export type Options = {
* Add babel plugins.
*/
babelPlugins?: PluginItem[];
/**Do you want to parse the title*/
isHeading?: boolean
}
```
Expand All @@ -112,6 +114,7 @@ import mdObj from 'markdown-react-code-preview-loader/README.md';
mdObj.source // => `README.md` raw string text
mdObj.components // => The component index object, the React component converted from the markdown indexed example. (need to configure meta)
mdObj.data // => The component source code index object, the sample source code indexed from markdown. (need to configure meta)
mdObj.headings // => This is the parsed header data
```

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

Expand All @@ -155,6 +159,7 @@ export type CodeBlockData = {
source: string;
components: Record<CodeBlockItem['name'], React.FC>;
data: Record<CodeBlockItem['name'], CodeBlockItem>;
headings?: HeadingItem[]
};
```

Expand Down
7 changes: 5 additions & 2 deletions core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { PluginItem } from '@babel/core';
import { Options as RIOptions } from 'babel-plugin-transform-remove-imports';
import { getProcessor, getCodeBlock, getHeadings } from './utils';
import { getProcessor, getCodeBlock, getHeadings, HeadingItem } from './utils';
import { LoaderDefinitionFunction } from 'webpack';
export * from './utils';

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

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

const codePreviewLoader: LoaderDefinitionFunction = function (source) {
Expand All @@ -57,7 +60,7 @@ const codePreviewLoader: LoaderDefinitionFunction = function (source) {
this.emitError(error);
}

const headings = getHeadings(child);
const headings = options.isHeading ? getHeadings(child) : [];

return `\nexport default {
components: { ${components} },
Expand Down

0 comments on commit d57d61a

Please sign in to comment.