Skip to content

Commit a50d8a7

Browse files
authored
fix(YfmHeading): ignore anchor links inside headings when parse dom (#348)
1 parent a762690 commit a50d8a7

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/extensions/yfm/YfmHeading/YfmHeading.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,20 @@ para
110110
doc(h({[YfmHeadingAttr.Level]: lvl}, `Heading ${lvl}`)),
111111
);
112112
});
113+
114+
it.each([1, 2, 3, 4, 5, 6])(
115+
'should parse html and ignore yfm-anchors inside headings - h%s tag',
116+
(lvl) => {
117+
parseDOM(
118+
schema,
119+
`<h${lvl} id="zagolovok-${lvl}" data-line="0" class="line"><a href="#zagolovok-${lvl}" class="yfm-anchor" aria-hidden="true"><span class="visually-hidden">Заголовок ${lvl}</span></a>Заголовок ${lvl}</h${lvl}>`,
120+
doc(
121+
h(
122+
{[YfmHeadingAttr.Level]: lvl, [YfmHeadingAttr.Id]: `zagolovok-${lvl}`},
123+
`Заголовок ${lvl}`,
124+
),
125+
),
126+
);
127+
},
128+
);
113129
});

src/extensions/yfm/YfmHeading/YfmHeadingSpecs/index.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,20 @@ export const YfmHeadingSpecs: ExtensionAuto<YfmHeadingSpecsOptions> = (builder,
3131
defining: true,
3232
selectable: true,
3333
parseDOM: [
34-
{tag: 'h1', getAttrs: getNodeAttrs(1)},
35-
{tag: 'h2', getAttrs: getNodeAttrs(2)},
36-
{tag: 'h3', getAttrs: getNodeAttrs(3)},
37-
{tag: 'h4', getAttrs: getNodeAttrs(4)},
38-
{tag: 'h5', getAttrs: getNodeAttrs(5)},
39-
{tag: 'h6', getAttrs: getNodeAttrs(6)},
34+
{tag: 'h1', getAttrs: getNodeAttrs(1), priority: 100, consuming: true},
35+
{tag: 'h2', getAttrs: getNodeAttrs(2), priority: 100, consuming: true},
36+
{tag: 'h3', getAttrs: getNodeAttrs(3), priority: 100, consuming: true},
37+
{tag: 'h4', getAttrs: getNodeAttrs(4), priority: 100, consuming: true},
38+
{tag: 'h5', getAttrs: getNodeAttrs(5), priority: 100, consuming: true},
39+
{tag: 'h6', getAttrs: getNodeAttrs(6), priority: 100, consuming: true},
40+
{
41+
// ignore anchor link inside headings
42+
tag: 'a.yfm-anchor',
43+
context: `${headingNodeName}/`,
44+
skip: true,
45+
ignore: true,
46+
priority: 1000,
47+
},
4048
],
4149
toDOM(node) {
4250
const id = node.attrs[YfmHeadingAttr.Id];

0 commit comments

Comments
 (0)