|
1 | 1 | const Remark = require(`remark`)
|
| 2 | +const toString = require(`mdast-util-to-string`) |
2 | 3 | const visit = require(`unist-util-visit`)
|
3 | 4 |
|
4 | 5 | const plugin = require(`../`)
|
@@ -182,4 +183,60 @@ describe(`gatsby-remark-autolink-headers`, () => {
|
182 | 183 | expect(node.data.id).toEqual(expect.stringMatching(/^heading/))
|
183 | 184 | })
|
184 | 185 | })
|
| 186 | + |
| 187 | + it(`extracts custom id`, () => { |
| 188 | + const markdownAST = remark.parse(` |
| 189 | +# Heading One {#custom_h1} |
| 190 | +
|
| 191 | +## Heading Two {#custom-heading-two} |
| 192 | +
|
| 193 | +# With *Bold* {#custom-withbold} |
| 194 | +
|
| 195 | +# Invalid {#this_is_italic} |
| 196 | +
|
| 197 | +# No custom ID |
| 198 | +
|
| 199 | +# {#id-only} |
| 200 | +
|
| 201 | +# {#text-after} custom ID |
| 202 | + `) |
| 203 | + const enableCustomId = true |
| 204 | + |
| 205 | + const transformed = plugin({ markdownAST }, { enableCustomId }) |
| 206 | + |
| 207 | + const headers = [] |
| 208 | + visit(transformed, `heading`, node => { |
| 209 | + headers.push({ text: toString(node), id: node.data.id }) |
| 210 | + }) |
| 211 | + expect(headers).toEqual([ |
| 212 | + { |
| 213 | + id: `custom_h1`, |
| 214 | + text: `Heading One`, |
| 215 | + }, |
| 216 | + { |
| 217 | + id: `custom-heading-two`, |
| 218 | + text: `Heading Two`, |
| 219 | + }, |
| 220 | + { |
| 221 | + id: `custom-withbold`, |
| 222 | + text: `With Bold`, |
| 223 | + }, |
| 224 | + { |
| 225 | + id: `invalid-thisisitalic`, |
| 226 | + text: `Invalid {#thisisitalic}`, |
| 227 | + }, |
| 228 | + { |
| 229 | + id: `no-custom-id`, |
| 230 | + text: `No custom ID`, |
| 231 | + }, |
| 232 | + { |
| 233 | + id: `id-only`, |
| 234 | + text: `{#id-only}`, |
| 235 | + }, |
| 236 | + { |
| 237 | + id: `text-after-custom-id`, |
| 238 | + text: `{#text-after} custom ID`, |
| 239 | + }, |
| 240 | + ]) |
| 241 | + }) |
185 | 242 | })
|
0 commit comments