forked from nodejs/nodejs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanchor-mardown-headings.test.js
121 lines (102 loc) · 4.28 KB
/
anchor-mardown-headings.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
'use strict'
const marked = require('marked')
const test = require('tape')
const anchorMarkdownHeadings = require('../../scripts/plugins/anchor-markdown-headings')
test('anchorMarkdownHeadings', (t) => {
t.plan(8)
t.test('correctly parses markdown heading without links', (t) => {
const text = 'Simple title'
const level = 1
const raw = 'Simple title'
const output = anchorMarkdownHeadings(text, level, raw)
const expected = '<h1 id="header-simple-title">Simple title' +
'<a id="simple-title" class="anchor" href="#simple-title" ' +
'aria-labelledby="header-simple-title"></a></h1>'
t.plan(1)
t.equal(output, expected)
})
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](#)'
const output = anchorMarkdownHeadings(text, level, raw)
const expected = '<h3 id="header-title-with-link">Title with ' +
'<a href="#">link</a>' +
'<a id="title-with-link" class="anchor" href="#title-with-link" ' +
'aria-labelledby="header-title-with-link"></a></h3>'
t.plan(1)
t.equal(output, expected)
})
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'
const output = anchorMarkdownHeadings(text, level, raw)
const expected = '<h2 id="header-a-b-cde">a <a href="b">b</a> c' +
'<a href="d">d</a>e' +
'<a id="a-b-cde" class="anchor" href="#a-b-cde" ' +
'aria-labelledby="header-a-b-cde"></a></h2>'
t.plan(1)
t.equal(output, expected)
})
t.test('makes pretty slugs', (t) => {
const text = '$$$ WIN BIG! $$$'
const level = 4
const raw = '$$$ WIN BIG! $$$'
const output = anchorMarkdownHeadings(text, level, raw)
const expected = '<h4 id="header-win-big">$$$ WIN BIG! $$$' +
'<a id="win-big" class="anchor" href="#win-big" ' +
'aria-labelledby="header-win-big"></a></h4>'
t.plan(1)
t.equal(output, expected)
})
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)'
const output = anchorMarkdownHeadings(text, level, raw)
const expected = '<h2 id="header-anchor-with-non-english-characters">' +
'这是<a href="b">链接</a>的<a href="d">测试!</a>' +
'<a id="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('correctly parses markdown heading with empty spaces', (t) => {
const text = '这是<a href="b">链接</a>的<a href="d">测试!</a>'
const level = 2
const raw = '<!-- empty spaces - id -->这是[链接](b)c[测试!](d)'
const output = anchorMarkdownHeadings(text, level, raw)
const expected = '<h2 id="header-empty-spaces-id">' +
'这是<a href="b">链接</a>的<a href="d">测试!</a>' +
'<a id="empty-spaces-id" class="anchor" ' +
'href="#empty-spaces-id" ' +
'aria-labelledby="header-empty-spaces-id"></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)
})
t.test('does not generate duplicate IDs', (t) => {
const renderer = new marked.Renderer()
renderer.heading = anchorMarkdownHeadings
const text = '# Title\n# Title'
const output = marked(text, { renderer })
const expected = '<h1 id="header-title">Title' +
'<a id="title" class="anchor" ' +
'href="#title" aria-labelledby="header-title"></a></h1>' +
'<h1 id="header-title-1">Title' +
'<a id="title-1" class="anchor" ' +
'href="#title-1" aria-labelledby="header-title-1"></a></h1>'
t.plan(1)
t.equal(output, expected)
})
})