diff --git a/src/transform/utils.ts b/src/transform/utils.ts index 44879248..fd4295f4 100644 --- a/src/transform/utils.ts +++ b/src/transform/utils.ts @@ -50,20 +50,20 @@ export function headingInfo(tokens: Token[], idx: number) { const openToken = tokens[idx]; const inlineToken = tokens[idx + 1]; - let lastTextToken, + let title = '', i = 0; while (inlineToken.children && i < inlineToken.children.length) { const token = inlineToken.children[i]; if (token.type === 'text') { - lastTextToken = token; + title += token.content; } i++; } const level = Number.parseInt(openToken.tag.slice(1), 10); - const title = (lastTextToken && lastTextToken.content) || inlineToken.content; + title ||= inlineToken.content; return { level, diff --git a/test/anchors.test.ts b/test/anchors.test.ts index 7f42eadc..6a3313aa 100644 --- a/test/anchors.test.ts +++ b/test/anchors.test.ts @@ -118,4 +118,16 @@ describe('Anchors', () => { '
After include
\n', ); }); + + it('should add anchor with auto naming, using entire heading text', () => { + expect( + transformYfm('## _Lorem ~~ipsum **dolor** sit~~ amet_\n\nParagraph\n'), + ).toBe( + 'Paragraph
\n', + ) + }) });