Skip to content

Commit 410bd4c

Browse files
XhmikosRSEWeiTung
authored andcommitted
Fix generating anchors with empty IDs and links. (#2401)
Previously this resulted in duplicate IDs like header- and empty name attributes in translations, this is the fixture for that.
1 parent ced980e commit 410bd4c

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

scripts/plugins/anchor-markdown-headings.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ module.exports = function anchorMarkdownHeadings (text, level, raw) {
3535
.replace(/-{2,}/g, '-')
3636
.replace(/(^-|-$)/g, '')
3737
}
38+
39+
if (!anchorTitle) {
40+
return `<h${level}>${text}</h${level}>`
41+
}
42+
3843
anchorTitle = anchorTitle.toLowerCase()
3944

4045
return '<h' + level + ' id="header-' + anchorTitle + '">' + text + '<a name="' +

tests/scripts/anchor-mardown-headings.test.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict'
22

33
const test = require('tape')
4+
const anchorMarkdownHeadings = require('../../scripts/plugins/anchor-markdown-headings')
45

56
test('anchorMarkdownHeadings', (t) => {
6-
const anchorMarkdownHeadings = require('../../scripts/plugins/anchor-markdown-headings')
7+
t.plan(6)
78

8-
t.plan(5)
9-
t.test('correctly pharses markdown heading without links', (t) => {
9+
t.test('correctly parses markdown heading without links', (t) => {
1010
const text = 'Simple title'
1111
const level = 1
1212
const raw = 'Simple title'
@@ -19,7 +19,7 @@ test('anchorMarkdownHeadings', (t) => {
1919
t.equal(output, expected)
2020
})
2121

22-
t.test('correctly pharses markdown heading with a single link', (t) => {
22+
t.test('correctly parses markdown heading with a single link', (t) => {
2323
const text = 'Title with <a href="#">link</a>'
2424
const level = 3
2525
const raw = 'Title with [link](#)'
@@ -33,7 +33,7 @@ test('anchorMarkdownHeadings', (t) => {
3333
t.equal(output, expected)
3434
})
3535

36-
t.test('correctly pharses markdown heading with multiple links', (t) => {
36+
t.test('correctly parses markdown heading with multiple links', (t) => {
3737
const text = 'a <a href="b">b</a> c<a href="d">d</a>e'
3838
const level = 2
3939
const raw = 'a [b](b) c[d](d)e'
@@ -60,7 +60,7 @@ test('anchorMarkdownHeadings', (t) => {
6060
t.equal(output, expected)
6161
})
6262

63-
t.test('correctly pharses markdown heading with non-English characters', (t) => {
63+
t.test('correctly parses markdown heading with non-English characters', (t) => {
6464
const text = '这是<a href="b">链接</a>的<a href="d">测试!</a>'
6565
const level = 2
6666
const raw = '<!-- anchor-With-Non-English-Characters -->这是[链接](b)c[测试!](d)'
@@ -69,6 +69,18 @@ test('anchorMarkdownHeadings', (t) => {
6969
'这是<a href="b">链接</a>的<a href="d">测试!</a>' +
7070
'<a name="anchor-with-non-english-characters" class="anchor" href="#anchor-with-non-english-characters" ' +
7171
'aria-labelledby="header-anchor-with-non-english-characters"></a></h2>'
72+
73+
t.plan(1)
74+
t.equal(output, expected)
75+
})
76+
77+
t.test('does not generate empty anchors', (t) => {
78+
const text = 'إنضم إلينا'
79+
const level = 2
80+
const raw = 'إنضم إلينا'
81+
const output = anchorMarkdownHeadings(text, level, raw)
82+
const expected = '<h2>إنضم إلينا</h2>'
83+
7284
t.plan(1)
7385
t.equal(output, expected)
7486
})

0 commit comments

Comments
 (0)