Skip to content

Commit fe1c21f

Browse files
committed
Fix handling of images to match GitHub
1 parent 4fdcf45 commit fe1c21f

File tree

6 files changed

+64
-19
lines changed

6 files changed

+64
-19
lines changed

lib/search.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Slugger from 'github-slugger'
2-
import toString from 'mdast-util-to-string'
2+
import {toString} from 'mdast-util-to-string'
33
import {visit} from 'unist-util-visit'
44
import {convert} from 'unist-util-is'
55
import {toExpression} from './to-expression.js'
@@ -30,7 +30,7 @@ export function search(root, expression, settings) {
3030
}
3131

3232
function onheading(node, position, parent) {
33-
const value = toString(node)
33+
const value = toString(node, {includeImageAlt: false})
3434
// Remove this when `remark-attr` is up to date w/ micromark.
3535
/* c8 ignore next */
3636
const id = node.data && node.data.hProperties && node.data.hProperties.id
@@ -52,9 +52,8 @@ export function search(root, expression, settings) {
5252
endIndex = position
5353
}
5454

55-
// A non-empty heading after the closing (if we were looking for one).
55+
// A heading after the closing (if we were looking for one).
5656
if (
57-
value &&
5857
(endIndex || !expression) &&
5958
(!settings.maxDepth || node.depth <= settings.maxDepth) &&
6059
(!skip || !skip.test(value))

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"@types/unist": "^2.0.3",
4040
"extend": "^3.0.2",
4141
"github-slugger": "^1.2.1",
42-
"mdast-util-to-string": "^2.0.0",
42+
"mdast-util-to-string": "^3.1.0",
4343
"unist-util-is": "^5.0.0",
4444
"unist-util-visit": "^3.0.0"
4545
},

readme.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@ There is no default export.
6767

6868
Generate a Table of Contents from a [tree][].
6969

70-
Looks for the first [heading][] matching `options.heading` (case insensitive,
71-
supports alt/title attributes for links and images too), and returns a table
72-
of contents ([list][]) for all following headings.
70+
Looks for the first [heading][] matching `options.heading` (case insensitive),
71+
and returns a table of contents ([list][]) for all following headings.
7372
If no `heading` is specified, creates a table of contents for all headings in
7473
`tree`.
7574
`tree` is not changed.
+48-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,52 @@
11
{
22
"index": null,
33
"endIndex": null,
4-
"map": null
4+
"map": {
5+
"type": "list",
6+
"ordered": false,
7+
"spread": false,
8+
"children": [
9+
{
10+
"type": "listItem",
11+
"spread": false,
12+
"children": [
13+
{
14+
"type": "paragraph",
15+
"children": [
16+
{
17+
"type": "link",
18+
"title": null,
19+
"url": "#",
20+
"children": []
21+
}
22+
]
23+
}
24+
]
25+
},
26+
{
27+
"type": "listItem",
28+
"spread": false,
29+
"children": [
30+
{
31+
"type": "paragraph",
32+
"children": [
33+
{
34+
"type": "link",
35+
"title": null,
36+
"url": "#-1",
37+
"children": [
38+
{
39+
"type": "image",
40+
"title": null,
41+
"url": "an-image.svg",
42+
"alt": ""
43+
}
44+
]
45+
}
46+
]
47+
}
48+
]
49+
}
50+
]
51+
}
552
}

test/fixtures/phrasing-content/output.json

+7-7
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@
215215
{
216216
"type": "link",
217217
"title": null,
218-
"url": "#image",
218+
"url": "#",
219219
"children": [
220220
{
221221
"type": "image",
@@ -239,14 +239,14 @@
239239
{
240240
"type": "link",
241241
"title": null,
242-
"url": "#image-reference",
242+
"url": "#-1",
243243
"children": [
244244
{
245245
"type": "imageReference",
246+
"alt": "image reference",
246247
"identifier": "image",
247248
"label": "image",
248-
"referenceType": "full",
249-
"alt": "image reference"
249+
"referenceType": "full"
250250
}
251251
]
252252
}
@@ -286,14 +286,14 @@
286286
{
287287
"type": "link",
288288
"title": null,
289-
"url": "#link-reference",
289+
"url": "#-2",
290290
"children": [
291291
{
292292
"type": "imageReference",
293+
"alt": "link reference",
293294
"identifier": "link",
294295
"label": "link",
295-
"referenceType": "full",
296-
"alt": "link reference"
296+
"referenceType": "full"
297297
}
298298
]
299299
}

test/fixtures/slug-for-images-and-links/output.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
{
4242
"type": "link",
4343
"title": null,
44-
"url": "#something-else",
44+
"url": "#",
4545
"children": [
4646
{
4747
"type": "image",
@@ -65,7 +65,7 @@
6565
{
6666
"type": "link",
6767
"title": null,
68-
"url": "#something-elsefi",
68+
"url": "#-1",
6969
"children": [
7070
{
7171
"type": "image",
@@ -93,7 +93,7 @@
9393
{
9494
"type": "link",
9595
"title": null,
96-
"url": "#another-title",
96+
"url": "#-2",
9797
"children": [
9898
{
9999
"type": "image",

0 commit comments

Comments
 (0)