Skip to content

Commit 44811ad

Browse files
Add support for ordered lists
Closes GH-69. Reviewed-by: Jonathan Haines <jonno.haines@gmail.com> Reviewed-by: Titus Wormer <tituswormer@gmail.com>
1 parent 342d4bd commit 44811ad

File tree

8 files changed

+155
-10
lines changed

8 files changed

+155
-10
lines changed

lib/contents.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ var extend = require('extend')
55
module.exports = contents
66

77
// Transform a list of heading objects to a markdown list.
8-
function contents(map, tight, prefix) {
9-
var table = {type: 'list', ordered: false, spread: false, children: []}
8+
function contents(map, tight, prefix, ordered) {
9+
var table = {type: 'list', ordered: ordered, spread: false, children: []}
1010
var minDepth = Infinity
1111
var index = -1
1212

@@ -28,14 +28,14 @@ function contents(map, tight, prefix) {
2828
index = -1
2929

3030
while (++index < map.length) {
31-
insert(map[index], table, tight, prefix)
31+
insert(map[index], table, tight, prefix, ordered)
3232
}
3333

3434
return table
3535
}
3636

3737
// Insert an entry into `parent`.
38-
function insert(entry, parent, tight, prefix) {
38+
function insert(entry, parent, tight, prefix, ordered) {
3939
var siblings = parent.children
4040
var tail = siblings[siblings.length - 1]
4141
var index = -1
@@ -60,19 +60,24 @@ function insert(entry, parent, tight, prefix) {
6060
]
6161
})
6262
} else if (tail && tail.type === 'listItem') {
63-
insert(entry, siblings[siblings.length - 1], tight, prefix)
63+
insert(entry, siblings[siblings.length - 1], tight, prefix, ordered)
6464
} else if (tail && tail.type === 'list') {
6565
entry.depth--
66-
insert(entry, tail, tight, prefix)
66+
insert(entry, tail, tight, prefix, ordered)
6767
} else if (parent.type === 'list') {
6868
item = {type: 'listItem', spread: false, children: []}
6969
siblings.push(item)
70-
insert(entry, item, tight, prefix)
70+
insert(entry, item, tight, prefix, ordered)
7171
} else {
72-
item = {type: 'list', ordered: false, spread: false, children: []}
72+
item = {
73+
type: 'list',
74+
ordered: ordered,
75+
spread: false,
76+
children: []
77+
}
7378
siblings.push(item)
7479
entry.depth--
75-
insert(entry, item, tight, prefix)
80+
insert(entry, item, tight, prefix, ordered)
7681
}
7782

7883
if (parent.type === 'list' && !tight) {

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function toc(node, options) {
1313
var result = search(node, heading, settings)
1414

1515
result.map = result.map.length
16-
? contents(result.map, settings.tight, settings.prefix)
16+
? contents(result.map, settings.tight, settings.prefix, settings.ordered || false)
1717
: null
1818

1919
// No given heading.

readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ contents.
9797

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

100+
###### `options.ordered`
101+
102+
Whether to compile list-items as an ordered list (`boolean?`, default: `false`).
103+
100104
###### `options.prefix`
101105

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

types/index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ declare namespace mdastUtilToc {
3333
*/
3434
tight?: boolean
3535

36+
/**
37+
* Whether to compile list-items as an ordered list, otherwise they are unordered.
38+
*
39+
* @default false
40+
*/
41+
ordered?: boolean
42+
3643
/**
3744
* Add a prefix to links to headings in the table of contents.
3845
* Useful for example when later going from mdast to hast and sanitizing with `hast-util-sanitize`.

types/mdast-util-toc-tests.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ toc(tree, {
4848
prefix: '/prefix'
4949
})
5050

51+
toc(tree, {
52+
ordered: true
53+
})
54+
5155
toc(tree, {
5256
parents: ['root', 'blockquote']
5357
})

0 commit comments

Comments
 (0)