-
Notifications
You must be signed in to change notification settings - Fork 72
/
permalink.js
247 lines (204 loc) · 7.42 KB
/
permalink.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
let emittedWarning = false
const position = {
false: 'push',
true: 'unshift',
after: 'push',
before: 'unshift'
}
const permalinkSymbolMeta = {
isPermalinkSymbol: true
}
export function legacy (slug, opts, state, idx) {
if (!emittedWarning) {
const warningText = 'Using deprecated markdown-it-anchor permalink option, see https://github.com/valeriangalliat/markdown-it-anchor#permalinks'
if (typeof process === 'object' && process && process.emitWarning) {
process.emitWarning(warningText)
} else {
console.warn(warningText)
}
emittedWarning = true
}
const linkTokens = [
Object.assign(new state.Token('link_open', 'a', 1), {
attrs: [
...(opts.permalinkClass ? [['class', opts.permalinkClass]] : []),
['href', opts.permalinkHref(slug, state)],
...Object.entries(opts.permalinkAttrs(slug, state))
]
}),
Object.assign(new state.Token('html_block', '', 0), { content: opts.permalinkSymbol, meta: permalinkSymbolMeta }),
new state.Token('link_close', 'a', -1)
]
if (opts.permalinkSpace) {
state.tokens[idx + 1].children[position[opts.permalinkBefore]](Object.assign(new state.Token('text', '', 0), { content: ' ' }))
}
state.tokens[idx + 1].children[position[opts.permalinkBefore]](...linkTokens)
}
export function renderHref (slug) {
return `#${slug}`
}
export function renderAttrs (slug) {
return {}
}
const commonDefaults = {
class: 'header-anchor',
symbol: '#',
renderHref,
renderAttrs
}
export function makePermalink (renderPermalinkImpl) {
function renderPermalink (opts) {
opts = Object.assign({}, renderPermalink.defaults, opts)
return (slug, anchorOpts, state, idx) => {
return renderPermalinkImpl(slug, opts, anchorOpts, state, idx)
}
}
renderPermalink.defaults = Object.assign({}, commonDefaults)
renderPermalink.renderPermalinkImpl = renderPermalinkImpl
return renderPermalink
}
function mergeDuplicateClassAttrs(attrs) {
const classValues = [];
const mergedAttrs = attrs.filter(([key, value]) => {
if (key !== 'class') {
return true;
}
classValues.push(value);
});
if (classValues.length > 0) {
mergedAttrs.unshift(['class', classValues.join(' ')]);
}
return mergedAttrs;
}
export const linkInsideHeader = makePermalink((slug, opts, anchorOpts, state, idx) => {
const linkTokens = [
Object.assign(new state.Token('link_open', 'a', 1), {
attrs: mergeDuplicateClassAttrs([
...(opts.class ? [['class', opts.class]] : []),
['href', opts.renderHref(slug, state)],
...(opts.ariaHidden ? [['aria-hidden', 'true']] : []),
...Object.entries(opts.renderAttrs(slug, state))
])
}),
Object.assign(new state.Token('html_inline', '', 0), { content: opts.symbol, meta: permalinkSymbolMeta }),
new state.Token('link_close', 'a', -1)
]
if (opts.space) {
const space = typeof opts.space === 'string' ? opts.space : ' '
const type = typeof opts.space === 'string' ? 'html_inline' : 'text'
state.tokens[idx + 1].children[position[opts.placement]](Object.assign(new state.Token(type, '', 0), { content: space }))
}
state.tokens[idx + 1].children[position[opts.placement]](...linkTokens)
})
Object.assign(linkInsideHeader.defaults, {
space: true,
placement: 'after',
ariaHidden: false
})
export const ariaHidden = makePermalink(linkInsideHeader.renderPermalinkImpl)
ariaHidden.defaults = Object.assign({}, linkInsideHeader.defaults, {
ariaHidden: true
})
export const headerLink = makePermalink((slug, opts, anchorOpts, state, idx) => {
const linkTokens = [
Object.assign(new state.Token('link_open', 'a', 1), {
attrs: mergeDuplicateClassAttrs([
...(opts.class ? [['class', opts.class]] : []),
['href', opts.renderHref(slug, state)],
...Object.entries(opts.renderAttrs(slug, state))
])
}),
...(opts.safariReaderFix ? [new state.Token('span_open', 'span', 1)] : []),
...state.tokens[idx + 1].children,
...(opts.safariReaderFix ? [new state.Token('span_close', 'span', -1)] : []),
new state.Token('link_close', 'a', -1)
]
state.tokens[idx + 1] = Object.assign(new state.Token('inline', '', 0), {
children: linkTokens
})
})
Object.assign(headerLink.defaults, {
safariReaderFix: false
})
export const linkAfterHeader = makePermalink((slug, opts, anchorOpts, state, idx) => {
if (!['visually-hidden', 'aria-label', 'aria-describedby', 'aria-labelledby'].includes(opts.style)) {
throw new Error(`\`permalink.linkAfterHeader\` called with unknown style option \`${opts.style}\``)
}
if (!['aria-describedby', 'aria-labelledby'].includes(opts.style) && !opts.assistiveText) {
throw new Error(`\`permalink.linkAfterHeader\` called without the \`assistiveText\` option in \`${opts.style}\` style`)
}
if (opts.style === 'visually-hidden' && !opts.visuallyHiddenClass) {
throw new Error('`permalink.linkAfterHeader` called without the `visuallyHiddenClass` option in `visually-hidden` style')
}
const title = state.tokens[idx + 1]
.children
.filter(token => token.type === 'text' || token.type === 'code_inline')
.reduce((acc, t) => acc + t.content, '')
const subLinkTokens = []
const linkAttrs = []
if (opts.class) {
linkAttrs.push(['class', opts.class])
}
linkAttrs.push(['href', opts.renderHref(slug, state)])
linkAttrs.push(...Object.entries(opts.renderAttrs(slug, state)))
if (opts.style === 'visually-hidden') {
subLinkTokens.push(
Object.assign(new state.Token('span_open', 'span', 1), {
attrs: [['class', opts.visuallyHiddenClass]],
}),
Object.assign(new state.Token('text', '', 0), {
content: opts.assistiveText(title)
}),
new state.Token('span_close', 'span', -1)
)
if (opts.space) {
const space = typeof opts.space === 'string' ? opts.space : ' '
const type = typeof opts.space === 'string' ? 'html_inline' : 'text'
subLinkTokens[position[opts.placement]](Object.assign(new state.Token(type, '', 0), { content: space }))
}
subLinkTokens[position[opts.placement]](
Object.assign(new state.Token('span_open', 'span', 1), {
attrs: [['aria-hidden', 'true']],
}),
Object.assign(new state.Token('html_inline', '', 0), {
content: opts.symbol,
meta: permalinkSymbolMeta
}),
new state.Token('span_close', 'span', -1)
)
} else {
subLinkTokens.push(
Object.assign(new state.Token('html_inline', '', 0), {
content: opts.symbol,
meta: permalinkSymbolMeta
})
)
}
if (opts.style === 'aria-label') {
linkAttrs.push(['aria-label', opts.assistiveText(title)])
} else if (['aria-describedby', 'aria-labelledby'].includes(opts.style)) {
linkAttrs.push([opts.style, slug])
}
const linkTokens = [
Object.assign(new state.Token('link_open', 'a', 1), {
attrs: mergeDuplicateClassAttrs(linkAttrs)
}),
...subLinkTokens,
new state.Token('link_close', 'a', -1),
]
state.tokens.splice(idx + 3, 0, ...linkTokens)
if (opts.wrapper) {
state.tokens.splice(idx, 0, Object.assign(new state.Token('html_block', '', 0), {
content: opts.wrapper[0] + '\n'
}))
state.tokens.splice(idx + 3 + linkTokens.length + 1, 0, Object.assign(new state.Token('html_block', '', 0), {
content: opts.wrapper[1] + '\n'
}))
}
})
Object.assign(linkAfterHeader.defaults, {
style: 'visually-hidden',
space: true,
placement: 'after',
wrapper: null
})