Skip to content

Commit

Permalink
fix(preset): slug collect error when using component in title
Browse files Browse the repository at this point in the history
  • Loading branch information
PeachScript committed Apr 7, 2020
1 parent 521bd37 commit b44f0b0
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion packages/preset-dumi/src/transformer/remark/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,36 @@ import is from 'hast-util-is-element';

const headings = ['h1', 'h2', 'h3', 'h4', 'h5'];

function excludeComponentFromChildren(children) {
const rawStack = [];

return children.filter(item => {
if (item.type === 'raw') {
// ignore self-closing raw node, like <img />
if (/^<[A-Z][^\/]+>$/.test(item.value)) {
rawStack.push(item.value);
} else if (/^<\/[A-Z]/.test(item.value)) {
rawStack.pop();
}

return false;
}

// discard children if it was wrapped by built-in Component
return !rawStack.length;
});
}

export default () => (ast, vFile) => {
// initial slugs meta
vFile.data.slugs = [];

visit(ast, 'element', node => {
if (is(node, headings) && has(node, 'id')) {
const title = toString(node);
const title = toString({
children: excludeComponentFromChildren(node.children),
value: node.value,
});

vFile.data.slugs.push({
depth: parseInt(node.tagName[1], 10),
Expand Down

0 comments on commit b44f0b0

Please sign in to comment.