Skip to content

Commit 08af263

Browse files
authored
Add support for prefix
Closes GH-59. Closes GH-60. Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com> Reviewed-by: Jonathan Haines <jonno.haines@gmail.com>
1 parent 7ea1642 commit 08af263

File tree

6 files changed

+156
-16
lines changed

6 files changed

+156
-16
lines changed

lib/contents.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var LINK = 'link'
1111
var LINK_REFERENCE = 'linkReference'
1212

1313
// Transform a list of heading objects to a markdown list.
14-
function contents(map, tight) {
14+
function contents(map, tight, prefix) {
1515
var minDepth = Infinity
1616
var index = -1
1717
var length = map.length
@@ -38,14 +38,14 @@ function contents(map, tight) {
3838
index = -1
3939

4040
while (++index < length) {
41-
insert(map[index], table, tight)
41+
insert(map[index], table, tight, prefix)
4242
}
4343

4444
return table
4545
}
4646

4747
// Insert an entry into `parent`.
48-
function insert(entry, parent, tight) {
48+
function insert(entry, parent, tight, prefix) {
4949
var children = parent.children
5050
var length = children.length
5151
var last = children[length - 1]
@@ -62,30 +62,30 @@ function insert(entry, parent, tight) {
6262
{
6363
type: LINK,
6464
title: null,
65-
url: '#' + entry.id,
65+
url: '#' + (prefix || '') + entry.id,
6666
children: all(entry.children)
6767
}
6868
]
6969
})
7070

7171
children.push(item)
7272
} else if (last && last.type === LIST_ITEM) {
73-
insert(entry, last, tight)
73+
insert(entry, last, tight, prefix)
7474
} else if (last && last.type === LIST) {
7575
entry.depth--
7676

77-
insert(entry, last, tight)
77+
insert(entry, last, tight, prefix)
7878
} else if (parent.type === LIST) {
7979
item = listItem()
8080

81-
insert(entry, item, tight)
81+
insert(entry, item, tight, prefix)
8282

8383
children.push(item)
8484
} else {
8585
item = list()
8686
entry.depth--
8787

88-
insert(entry, item, tight)
88+
insert(entry, item, tight, prefix)
8989

9090
children.push(item)
9191
}

lib/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ function toc(node, options) {
1212
var result = search(node, heading, settings)
1313
var map = result.map
1414

15-
result.map = map.length === 0 ? null : contents(map, settings.tight)
15+
result.map =
16+
map.length === 0 ? null : contents(map, settings.tight, settings.prefix)
1617

1718
// No given heading.
1819
if (!heading) {

readme.md

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,23 @@ Yields:
4242

4343
```javascript
4444

45-
{ index: null,
45+
{
46+
index: null,
4647
endIndex: null,
47-
map:
48-
{ type: 'list',
49-
ordered: false,
50-
spread: true,
51-
children:
52-
[ { type: 'listItem', loose: true, spread: true, children: [Array] } ] } }
48+
map: {
49+
type: 'list',
50+
ordered: false,
51+
spread: true,
52+
children: [
53+
{
54+
type: 'listItem',
55+
loose: true,
56+
spread: true,
57+
children: [Array]
58+
}
59+
]
60+
}
61+
}
5362
```
5463

5564
## API
@@ -86,6 +95,12 @@ This is inclusive, thus, when set to `3`, level three headings, are included
8695

8796
Whether to compile list-items tightly (`boolean?`, default: `false`).
8897

98+
###### `options.prefix`
99+
100+
Add a prefix to links to headings (`string?`, default: `null`).
101+
Useful for example when later going from mdast to hast and sanitizing with
102+
[`hast-util-sanitize`][hast-util-sanitize].
103+
89104
###### `options.parents`
90105

91106
Allows headings to be children of certain node types.
@@ -174,3 +189,5 @@ repository, organisation, or community you agree to abide by its terms.
174189
[contributing]: https://github.com/syntax-tree/mdast/blob/master/contributing.md
175190

176191
[coc]: https://github.com/syntax-tree/mdast/blob/master/code-of-conduct.md
192+
193+
[hast-util-sanitize]: https://github.com/syntax-tree/hast-util-sanitize

test/fixtures/prefix/config.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"prefix": "user-content-"
3+
}

test/fixtures/prefix/input.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Something if
2+
3+
## Something else
4+
5+
Text.
6+
7+
## Something elsefi
8+
9+
# Something iffi

test/fixtures/prefix/output.json

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
{
2+
"index": null,
3+
"endIndex": null,
4+
"map": {
5+
"type": "list",
6+
"ordered": false,
7+
"spread": true,
8+
"children": [
9+
{
10+
"type": "listItem",
11+
"loose": true,
12+
"spread": true,
13+
"children": [
14+
{
15+
"type": "paragraph",
16+
"children": [
17+
{
18+
"type": "link",
19+
"title": null,
20+
"url": "#user-content-something-if",
21+
"children": [
22+
{
23+
"type": "text",
24+
"value": "Something if"
25+
}
26+
]
27+
}
28+
]
29+
},
30+
{
31+
"type": "list",
32+
"ordered": false,
33+
"spread": false,
34+
"children": [
35+
{
36+
"type": "listItem",
37+
"loose": false,
38+
"spread": false,
39+
"children": [
40+
{
41+
"type": "paragraph",
42+
"children": [
43+
{
44+
"type": "link",
45+
"title": null,
46+
"url": "#user-content-something-else",
47+
"children": [
48+
{
49+
"type": "text",
50+
"value": "Something else"
51+
}
52+
]
53+
}
54+
]
55+
}
56+
]
57+
},
58+
{
59+
"type": "listItem",
60+
"loose": false,
61+
"spread": false,
62+
"children": [
63+
{
64+
"type": "paragraph",
65+
"children": [
66+
{
67+
"type": "link",
68+
"title": null,
69+
"url": "#user-content-something-elsefi",
70+
"children": [
71+
{
72+
"type": "text",
73+
"value": "Something elsefi"
74+
}
75+
]
76+
}
77+
]
78+
}
79+
]
80+
}
81+
]
82+
}
83+
]
84+
},
85+
{
86+
"type": "listItem",
87+
"loose": false,
88+
"spread": false,
89+
"children": [
90+
{
91+
"type": "paragraph",
92+
"children": [
93+
{
94+
"type": "link",
95+
"title": null,
96+
"url": "#user-content-something-iffi",
97+
"children": [
98+
{
99+
"type": "text",
100+
"value": "Something iffi"
101+
}
102+
]
103+
}
104+
]
105+
}
106+
]
107+
}
108+
]
109+
}
110+
}

0 commit comments

Comments
 (0)