Skip to content

Commit 7423076

Browse files
committed
fix:修复标题解析取值
1 parent 9d72fdf commit 7423076

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

core/src/index.ts

Lines changed: 7 additions & 4 deletions
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, HeadingItem } from './utils';
4+
import { getProcessor, getCodeBlock, getHeadings, HeadingItem, HeadingListType } from './utils';
55
import { LoaderDefinitionFunction } from 'webpack';
66
export * from './utils';
77

@@ -23,6 +23,7 @@ export type CodeBlockData = {
2323
components: Record<CodeBlockItem['name'], React.FC>;
2424
data: Record<CodeBlockItem['name'], CodeBlockItem>;
2525
headings?: HeadingItem[];
26+
headingsList: HeadingListType[];
2627
};
2728

2829
export const FUNNAME_PREFIX = '__BaseCode__';
@@ -47,26 +48,28 @@ export type Options = {
4748

4849
const codePreviewLoader: LoaderDefinitionFunction = function (source) {
4950
const options: Options = this.getOptions();
51+
const { isHeading, ...rest } = options;
5052

5153
let components = '';
5254
let codeBlock = {} as CodeBlockData['data'];
5355
const child = getProcessor(source);
5456
try {
55-
codeBlock = getCodeBlock(child, options, this.resourcePath);
57+
codeBlock = getCodeBlock(child, rest, this.resourcePath);
5658
Object.keys(codeBlock).forEach((key) => {
5759
components += `${key}: (function() { ${codeBlock[key].code} })(),`;
5860
});
5961
} catch (error) {
6062
this.emitError(error);
6163
}
6264

63-
const headings = options.isHeading ? getHeadings(child) : [];
65+
const { headingsList, headings } = isHeading ? getHeadings(child) : { headingsList: [], headings: [] };
6466

6567
return `\nexport default {
6668
components: { ${components} },
6769
data: ${JSON.stringify(codeBlock, null, 2)},
6870
source: ${JSON.stringify(source)},
69-
headings:${JSON.stringify(headings)}
71+
headings:${JSON.stringify(headings)},
72+
headingsList:${JSON.stringify(headingsList)},
7073
}`;
7174
};
7275

core/src/utils/index.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ export const mdCodeModulesLoader = (
131131
export interface HeadingListType {
132132
depth: number;
133133
value: string;
134+
key: number;
134135
}
135136

136137
export interface HeadingItem extends HeadingListType {
@@ -185,17 +186,22 @@ export const getSameLevelHeading = (list: HeadingListType[]) => {
185186
export const getHeadings = (child: MarkdownParseData['children']) => {
186187
const headingList: HeadingListType[] = [];
187188

188-
child.forEach((item) => {
189+
child.forEach((item, index) => {
189190
if (item && item.type === 'heading') {
190191
const { depth, children } = item;
191-
if (Array.isArray(children) && children.length && depth) {
192-
const [firstItem] = children || [];
193-
if (firstItem && firstItem?.value) {
194-
headingList.push({ depth, value: firstItem?.value });
195-
}
192+
if (Array.isArray(children) && children.length) {
193+
const value = children.map((item) => item.value).join('');
194+
headingList.push({
195+
key: index,
196+
value,
197+
depth,
198+
});
196199
}
197200
}
198201
});
199202

200-
return getSameLevelHeading(headingList);
203+
return {
204+
headings: getSameLevelHeading(headingList),
205+
headingsList: headingList,
206+
};
201207
};

0 commit comments

Comments
 (0)