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 77cadfd commit c595d0b
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 3 deletions.
10 changes: 7 additions & 3 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 } from './utils';
import { getProcessor, getCodeBlock, getHeadings } from './utils';
import { LoaderDefinitionFunction } from 'webpack';
export * from './utils';

Expand Down Expand Up @@ -47,19 +47,23 @@ const codePreviewLoader: LoaderDefinitionFunction = function (source) {

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

const headings = getHeadings(child);

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

Expand Down
74 changes: 74 additions & 0 deletions core/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export interface MarkdownDataChild extends Node {
lang: string;
meta: string;
value: string;
depth?: number;
children?: Array<MarkdownDataChild>;
}

export interface MarkdownParseData extends Parent<MarkdownDataChild> {}
Expand Down Expand Up @@ -125,3 +127,75 @@ export const mdCodeModulesLoader = (
});
return config;
};

export interface HeadingListType {
depth: number;
value: string;
}

export interface HeadingItem extends HeadingListType {
/**嵌套子标题*/
children?: HeadingItem[];
}

/**进行获取同级别标题数据*/
export const getSameLevelHeading = (list: HeadingListType[]) => {
const newList: { start: number; end: number }[] = [];
let level: number = 0;
let satrtIndex = 0;
let lg = list.length;

// 对同级别数据进行区分
for (let index = 0; index < lg; index++) {
const element = list[index];
if (index === 0) {
satrtIndex = 0;
/**默认第一个数据的层级进行查找*/
level = element.depth;
} else if (element.depth === level) {
// 层级相同则进行赋值
// 这个位置相等,说明这些数据是一组数据
newList.push({ start: satrtIndex, end: index });
/**重新赋值开始下标数据*/
satrtIndex = index;
}
}
// 如果最后位置没找到
if (satrtIndex <= lg - 1) {
newList.push({ start: satrtIndex, end: lg });
}

const saveList: HeadingItem[] = [];

/**对标题数据进行处理*/
newList.forEach((item) => {
const { start, end } = item;
const [firstItem, ...lastItems] = list.slice(start, end);
const newItem: HeadingItem = { ...firstItem };
if (Array.isArray(lastItems) && lastItems.length) {
newItem.children = getSameLevelHeading(lastItems);
}
saveList.push(newItem);
});

return saveList;
};

/**获取标题*/
export const getHeadings = (child: MarkdownParseData['children']) => {
const headingList: HeadingListType[] = [];

child.forEach((item) => {
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 });
}
}
}
});

return getSameLevelHeading(headingList);
};

0 comments on commit c595d0b

Please sign in to comment.