Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions scripts/plugins/anchor-markdown-headings.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ module.exports = function anchorMarkdownHeadings (text, level, raw) {
.replace(/-{2,}/g, '-')
.replace(/(^-|-$)/g, '')
}

if (!anchorTitle) {
return `<h${level}>${text}</h${level}>`
}

anchorTitle = anchorTitle.toLowerCase()

return '<h' + level + ' id="header-' + anchorTitle + '">' + text + '<a name="' +
Expand Down
24 changes: 18 additions & 6 deletions tests/scripts/anchor-mardown-headings.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict'

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

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

t.plan(5)
t.test('correctly pharses markdown heading without links', (t) => {
t.test('correctly parses markdown heading without links', (t) => {
const text = 'Simple title'
const level = 1
const raw = 'Simple title'
Expand All @@ -19,7 +19,7 @@ test('anchorMarkdownHeadings', (t) => {
t.equal(output, expected)
})

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

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

t.test('correctly pharses markdown heading with non-English characters', (t) => {
t.test('correctly parses markdown heading with non-English characters', (t) => {
const text = '这是<a href="b">链接</a>的<a href="d">测试!</a>'
const level = 2
const raw = '<!-- anchor-With-Non-English-Characters -->这是[链接](b)c[测试!](d)'
Expand All @@ -69,6 +69,18 @@ test('anchorMarkdownHeadings', (t) => {
'这是<a href="b">链接</a>的<a href="d">测试!</a>' +
'<a name="anchor-with-non-english-characters" class="anchor" href="#anchor-with-non-english-characters" ' +
'aria-labelledby="header-anchor-with-non-english-characters"></a></h2>'

t.plan(1)
t.equal(output, expected)
})

t.test('does not generate empty anchors', (t) => {
const text = 'إنضم إلينا'
const level = 2
const raw = 'إنضم إلينا'
const output = anchorMarkdownHeadings(text, level, raw)
const expected = '<h2>إنضم إلينا</h2>'

t.plan(1)
t.equal(output, expected)
})
Expand Down