Skip to content

Commit 2bd31ba

Browse files
Merge pull request #34 from Hypercubed/feature.use-existing-ids
Feature: use existing ids
2 parents a36825b + 213be1a commit 2bd31ba

File tree

5 files changed

+120
-2
lines changed

5 files changed

+120
-2
lines changed

lib/search.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ function search(root, expression, maxDepth) {
5050

5151
visit(root, HEADING, function (child, index, parent) {
5252
var value = toString(child);
53-
var id = slugs.slug(value);
53+
var id = child.data && child.data.hProperties && child.data.hProperties.id;
54+
id = slugs.slug(id || value);
5455

5556
if (parent !== root) {
5657
return;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"browserify": "^16.2.1",
4343
"esmangle": "^1.0.1",
4444
"istanbul": "^0.4.4",
45+
"remark-attr": "^0.6.2",
4546
"remark": "^9.0.0",
4647
"remark-cli": "^5.0.0",
4748
"remark-comment-config": "^5.0.0",

test/fixtures/normal-attr/input.md

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

test/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var test = require('tape');
33
var fs = require('fs');
44
var path = require('path');
55
var remark = require('remark');
6+
var remarkAttr = require('remark-attr');
67
var toc = require('..');
78

89
var read = fs.readFileSync;
@@ -29,7 +30,7 @@ test('Fixtures', function (t) {
2930
}).forEach(function (fixture) {
3031
var filepath = join(ROOT, fixture);
3132
var output = JSON.parse(read(join(filepath, 'output.json'), 'utf8'));
32-
var input = remark().parse(read(join(filepath, 'input.md'), 'utf-8'));
33+
var input = remark().use(remarkAttr).parse(read(join(filepath, 'input.md'), 'utf-8'));
3334
var config = join(filepath, 'config.json');
3435
var result;
3536

0 commit comments

Comments
 (0)