Skip to content

Commit

Permalink
fix:修复标题解析取值
Browse files Browse the repository at this point in the history
  • Loading branch information
SunLxy committed Aug 18, 2023
1 parent 9d72fdf commit 7423076
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
11 changes: 7 additions & 4 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, HeadingItem } from './utils';
import { getProcessor, getCodeBlock, getHeadings, HeadingItem, HeadingListType } from './utils';
import { LoaderDefinitionFunction } from 'webpack';
export * from './utils';

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

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

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

let components = '';
let codeBlock = {} as CodeBlockData['data'];
const child = getProcessor(source);
try {
codeBlock = getCodeBlock(child, options, this.resourcePath);
codeBlock = getCodeBlock(child, rest, this.resourcePath);
Object.keys(codeBlock).forEach((key) => {
components += `${key}: (function() { ${codeBlock[key].code} })(),`;
});
} catch (error) {
this.emitError(error);
}

const headings = options.isHeading ? getHeadings(child) : [];
const { headingsList, headings } = isHeading ? getHeadings(child) : { headingsList: [], headings: [] };

return `\nexport default {
components: { ${components} },
data: ${JSON.stringify(codeBlock, null, 2)},
source: ${JSON.stringify(source)},
headings:${JSON.stringify(headings)}
headings:${JSON.stringify(headings)},
headingsList:${JSON.stringify(headingsList)},
}`;
};

Expand Down
20 changes: 13 additions & 7 deletions core/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export const mdCodeModulesLoader = (
export interface HeadingListType {
depth: number;
value: string;
key: number;
}

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

child.forEach((item) => {
child.forEach((item, index) => {
if (item && item.type === 'heading') {
const { depth, children } = item;
if (Array.isArray(children) && children.length && depth) {
const [firstItem] = children || [];
if (firstItem && firstItem?.value) {
headingList.push({ depth, value: firstItem?.value });
}
if (Array.isArray(children) && children.length) {
const value = children.map((item) => item.value).join('');
headingList.push({
key: index,
value,
depth,
});
}
}
});

return getSameLevelHeading(headingList);
return {
headings: getSameLevelHeading(headingList),
headingsList: headingList,
};
};

0 comments on commit 7423076

Please sign in to comment.