Skip to content

Commit b02d9ce

Browse files
committed
Refactor to improve bundle size
1 parent f16b6e3 commit b02d9ce

File tree

8 files changed

+85
-127
lines changed

8 files changed

+85
-127
lines changed

lib/contents.js

Lines changed: 41 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,14 @@ var extend = require('extend')
44

55
module.exports = contents
66

7-
var LIST = 'list'
8-
var LIST_ITEM = 'listItem'
9-
var PARAGRAPH = 'paragraph'
10-
var LINK = 'link'
11-
var LINK_REFERENCE = 'linkReference'
12-
var FOOTNOTE = 'footnote'
13-
var FOOTNOTE_REFERENCE = 'footnoteReference'
14-
157
// Transform a list of heading objects to a markdown list.
168
function contents(map, tight, prefix) {
9+
var table = {type: 'list', ordered: false, spread: false, children: []}
1710
var minDepth = Infinity
1811
var index = -1
19-
var length = map.length
20-
var table
2112

2213
// Find minimum depth.
23-
while (++index < length) {
14+
while (++index < map.length) {
2415
if (map[index].depth < minDepth) {
2516
minDepth = map[index].depth
2617
}
@@ -29,17 +20,14 @@ function contents(map, tight, prefix) {
2920
// Normalize depth.
3021
index = -1
3122

32-
while (++index < length) {
23+
while (++index < map.length) {
3324
map[index].depth -= minDepth - 1
3425
}
3526

36-
// Construct the main list.
37-
table = list()
38-
3927
// Add TOC to list.
4028
index = -1
4129

42-
while (++index < length) {
30+
while (++index < map.length) {
4331
insert(map[index], table, tight, prefix)
4432
}
4533

@@ -48,72 +36,67 @@ function contents(map, tight, prefix) {
4836

4937
// Insert an entry into `parent`.
5038
function insert(entry, parent, tight, prefix) {
51-
var children = parent.children
52-
var length = children.length
53-
var last = children[length - 1]
54-
var index
39+
var siblings = parent.children
40+
var tail = siblings[siblings.length - 1]
41+
var index = -1
5542
var item
5643

5744
if (entry.depth === 1) {
58-
item = listItem()
59-
60-
item.children.push({
61-
type: PARAGRAPH,
45+
siblings.push({
46+
type: 'listItem',
47+
spread: false,
6248
children: [
6349
{
64-
type: LINK,
65-
title: null,
66-
url: '#' + (prefix || '') + entry.id,
67-
children: all(entry.children)
50+
type: 'paragraph',
51+
children: [
52+
{
53+
type: 'link',
54+
title: null,
55+
url: '#' + (prefix || '') + entry.id,
56+
children: all(entry.children)
57+
}
58+
]
6859
}
6960
]
7061
})
71-
72-
children.push(item)
73-
} else if (last && last.type === LIST_ITEM) {
74-
insert(entry, last, tight, prefix)
75-
} else if (last && last.type === LIST) {
62+
} else if (tail && tail.type === 'listItem') {
63+
insert(entry, siblings[siblings.length - 1], tight, prefix)
64+
} else if (tail && tail.type === 'list') {
7665
entry.depth--
77-
78-
insert(entry, last, tight, prefix)
79-
} else if (parent.type === LIST) {
80-
item = listItem()
81-
66+
insert(entry, tail, tight, prefix)
67+
} else if (parent.type === 'list') {
68+
item = {type: 'listItem', spread: false, children: []}
69+
siblings.push(item)
8270
insert(entry, item, tight, prefix)
83-
84-
children.push(item)
8571
} else {
86-
item = list()
72+
item = {type: 'list', ordered: false, spread: false, children: []}
73+
siblings.push(item)
8774
entry.depth--
88-
8975
insert(entry, item, tight, prefix)
90-
91-
children.push(item)
9276
}
9377

94-
// Properly style list-items with new lines.
95-
parent.spread = !tight
96-
97-
if (parent.type === LIST && parent.spread) {
78+
if (parent.type === 'list' && !tight) {
9879
parent.spread = false
99-
index = -1
10080

101-
while (++index < length) {
102-
if (children[index].children.length > 1) {
81+
while (++index < siblings.length) {
82+
if (siblings[index].children.length > 1) {
10383
parent.spread = true
10484
break
10585
}
10686
}
87+
} else {
88+
parent.spread = !tight
10789
}
10890
}
10991

11092
function all(children) {
11193
var result = []
112-
var length = children ? children.length : 0
11394
var index = -1
11495

115-
while (++index < length) {
116-
result = result.concat(one(children[index]))
96+
if (children) {
97+
while (++index < children.length) {
98+
result = result.concat(one(children[index]))
99+
}
117100
}
118101

119102
return result
@@ -123,10 +106,10 @@ function one(node) {
123106
var copy
124107

125108
if (
126-
node.type === LINK ||
127-
node.type === LINK_REFERENCE ||
128-
node.type === FOOTNOTE ||
129-
node.type === FOOTNOTE_REFERENCE
109+
node.type === 'link' ||
110+
node.type === 'linkReference' ||
111+
node.type === 'footnote' ||
112+
node.type === 'footnoteReference'
130113
) {
131114
return all(node.children)
132115
}
@@ -144,13 +127,3 @@ function one(node) {
144127

145128
return copy
146129
}
147-
148-
// Create a list.
149-
function list() {
150-
return {type: LIST, ordered: false, spread: false, children: []}
151-
}
152-
153-
// Create a list item.
154-
function listItem() {
155-
return {type: LIST_ITEM, spread: false, children: []}
156-
}

lib/index.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@ function toc(node, options) {
1111
var settings = options || {}
1212
var heading = settings.heading ? toExpression(settings.heading) : null
1313
var result = search(node, heading, settings)
14-
var map = result.map
1514

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

1919
// No given heading.
2020
if (!heading) {
21-
result.index = null
22-
result.endIndex = null
21+
result.endIndex = result.index = null
2322
}
2423

2524
return result

lib/search.js

Lines changed: 24 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,86 +8,58 @@ var convert = require('unist-util-is/convert')
88
var slugs = require('github-slugger')()
99
var toExpression = require('./to-expression')
1010

11-
var heading = convert('heading')
12-
1311
// Search a node for a location.
1412
function search(root, expression, settings) {
15-
var length = root.children.length
16-
var depth = null
17-
var lookingForToc = expression !== null
18-
var maxDepth = settings.maxDepth || 6
19-
var skip = settings.skip ? toExpression(settings.skip) : null
13+
var skip = settings.skip && toExpression(settings.skip)
2014
var parents = convert(settings.parents || root)
2115
var map = []
22-
var headingIndex
23-
var closingIndex
24-
25-
if (!lookingForToc) {
26-
headingIndex = -1
27-
}
16+
var index
17+
var endIndex
18+
var opening
2819

2920
slugs.reset()
3021

3122
// Visit all headings in `root`. We `slug` all headings (to account for
3223
// duplicates), but only create a TOC from top-level headings.
3324
visit(root, 'heading', onheading)
3425

35-
if (headingIndex && !closingIndex) {
36-
closingIndex = length + 1
26+
return {
27+
index: index || -1,
28+
endIndex: index ? endIndex || root.children.length : -1,
29+
map: map
3730
}
3831

39-
if (headingIndex === undefined) {
40-
headingIndex = -1
41-
closingIndex = -1
42-
map = []
43-
}
44-
45-
return {index: headingIndex, endIndex: closingIndex, map: map}
46-
47-
function onheading(child, index, parent) {
48-
var value = toString(child)
32+
function onheading(node, position, parent) {
33+
var value = toString(node)
4934
/* istanbul ignore next - to do: remove this when `remark-attr` is up to
5035
* date w/ micromark. */
51-
var id = child.data && child.data.hProperties && child.data.hProperties.id
36+
var id = node.data && node.data.hProperties && node.data.hProperties.id
5237
var slug = slugs.slug(id || value)
5338

5439
if (!parents(parent)) {
5540
return
5641
}
5742

58-
if (lookingForToc) {
59-
if (isClosingHeading(child, depth)) {
60-
closingIndex = index
61-
lookingForToc = false
62-
}
43+
// Our opening heading.
44+
if (expression && !index && expression.test(value)) {
45+
index = position + 1
46+
opening = node
47+
return
48+
}
6349

64-
if (isOpeningHeading(child, depth, expression)) {
65-
headingIndex = index + 1
66-
depth = child.depth
67-
}
50+
// Our closing heading.
51+
if (opening && !endIndex && node.depth <= opening.depth) {
52+
endIndex = position
6853
}
6954

55+
// A non-empty heading after the closing (if we were looking for one).
7056
if (
71-
!lookingForToc &&
7257
value &&
73-
child.depth <= maxDepth &&
58+
(endIndex || !expression) &&
59+
(!settings.maxDepth || node.depth <= settings.maxDepth) &&
7460
(!skip || !skip.test(value))
7561
) {
76-
map.push({
77-
depth: child.depth,
78-
children: child.children,
79-
id: slug
80-
})
62+
map.push({depth: node.depth, children: node.children, id: slug})
8163
}
8264
}
8365
}
84-
85-
// Check if `node` is the main heading.
86-
function isOpeningHeading(node, depth, expression) {
87-
return depth === null && heading(node) && expression.test(toString(node))
88-
}
89-
90-
// Check if `node` is the next heading.
91-
function isClosingHeading(node, depth) {
92-
return depth && heading(node) && node.depth <= depth
93-
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@
8888
"prettier": true,
8989
"esnext": false,
9090
"rules": {
91+
"no-multi-assign": "off",
92+
"unicorn/explicit-length-check": "off",
9193
"unicorn/prefer-optional-catch-binding": "off"
9294
},
9395
"ignores": [

readme.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,11 @@ An object representing the table of contents.
125125
###### Properties
126126

127127
* `index` (`number?`)
128-
[Index][] of the found table of contents [heading][] in `tree`.
128+
[Index][] of the node right after the table of contents [heading][].
129129
`-1` if no heading was found, `null` if no `heading` was given
130130
* `endIndex` (`number?`)
131-
[Index][] of the last node after `heading` before the TOC starts.
131+
[Index][] of the first node after `heading` that is not part of its
132+
section.
132133
`-1` if no heading was found, `null` if no `heading` was given,
133134
same as `index` if there are no nodes between `heading` and the
134135
first heading in the TOC
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"heading": "fo+"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Some content.
2+
3+
# Foo
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"index": 2,
3+
"endIndex": 2,
4+
"map": null
5+
}

0 commit comments

Comments
 (0)