Skip to content

Commit de11f3a

Browse files
initial commit
0 parents  commit de11f3a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+843
-0
lines changed

.editorconfig

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 4
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
11+
[*.{json,svg,remarkrc,eslintrc}]
12+
indent_size = 2
13+
14+
[*.md]
15+
trim_trailing_whitespace = false

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.DS_Store
2+
*.log
3+
coverage/
4+
node_modules/
5+
mdast-util-toc.js
6+
mdast-util-toc.min.js

.remarkignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test/

.remarkrc

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"output": true,
3+
"plugins": [
4+
"comment-config",
5+
"lint",
6+
"github",
7+
"usage",
8+
"slug",
9+
"validate-links"
10+
],
11+
"settings": {
12+
"bullet": "*"
13+
}
14+
}

.travis.yml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
language: node_js
2+
node_js:
3+
- '0.11'
4+
- '0.12'
5+
- '4.0'
6+
- '5.0'
7+
- '6.0'
8+
sudo: false
9+
after_success:
10+
- bash <(curl -s https://codecov.io/bash)
11+
deploy:
12+
matrix:
13+
- provider: npm
14+
15+
api_key:
16+
secure: IjSqlNrFIB507wJM1IN54lmym0cvjd6V12W3U6pqFDy4Kfz9Rm781AD0gTL9T5VMZp8cbSteclXEGS2SuGbY7+O20mstPQJoTTxgHCIo/RePZElZNU8WQmE/vt0Phru38Qi5c0sKnvxDukiS5bmkK1Bivlu0KME01ObCM1TQ33iHhiNkjhCDm9PUcdW0EMcEwfuVfwHBBL1e8ZnHgadeS9wIsiqGueJkdJ3iChkqt3Dr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8KX6U6PD84mZud33zFBigl21Eznjc7eLrf6sjj4HVNrR9vCsVhvTTV+9KqhVUnwZbzRl7BeDMg+udo4hmdYQSlSr31ube18=
23+
file:
24+
- remark-toc.js
25+
- remark-toc.min.js
26+
on:
27+
tags: true
28+
node: '6.0'

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

index.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @author Titus Wormer
3+
* @copyright 2015 Titus Wormer
4+
* @license MIT
5+
* @module mdast:toc
6+
* @fileoverview Generate a Table of Contents (TOC) from a given Markdown file.
7+
*/
8+
9+
'use strict';
10+
11+
/* Expose. */
12+
module.exports = require('./lib/index.js');

lib/contents.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* @author Titus Wormer
3+
* @copyright 2015 Titus Wormer
4+
* @license MIT
5+
* @module mdast:toc
6+
* @fileoverview Generate a Table of Contents (TOC) from a given Markdown file.
7+
*/
8+
9+
/* Expose. */
10+
module.exports = contents;
11+
12+
/* Dependencies */
13+
var list = require('./list');
14+
var insert = require('./insert');
15+
16+
/**
17+
* Transform a list of heading objects to a markdown list.
18+
*
19+
* @param {Array.<Object>} map - Heading-map to insert.
20+
* @param {boolean?} [tight] - Prefer tight list-items.
21+
* @return {Object} - List node.
22+
*/
23+
function contents(map, tight) {
24+
var minDepth = Infinity;
25+
var index = -1;
26+
var length = map.length;
27+
var table;
28+
29+
/*
30+
* Find minimum depth.
31+
*/
32+
33+
while (++index < length) {
34+
if (map[index].depth < minDepth) {
35+
minDepth = map[index].depth;
36+
}
37+
}
38+
39+
/*
40+
* Normalize depth.
41+
*/
42+
43+
index = -1;
44+
45+
while (++index < length) {
46+
map[index].depth -= minDepth - 1;
47+
}
48+
49+
/*
50+
* Construct the main list.
51+
*/
52+
53+
table = list();
54+
55+
/*
56+
* Add TOC to list.
57+
*/
58+
59+
index = -1;
60+
61+
while (++index < length) {
62+
insert(map[index], table, tight);
63+
}
64+
65+
return table;
66+
}

lib/index.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @author Titus Wormer
3+
* @copyright 2015 Titus Wormer
4+
* @license MIT
5+
* @module mdast:toc
6+
* @fileoverview Generate a Table of Contents (TOC) from a given Markdown file.
7+
*/
8+
9+
/* Expose. */
10+
module.exports = attacher;
11+
12+
/* Dependencies */
13+
var toExpression = require('./to-expression');
14+
var search = require('./search');
15+
var contents = require('./contents');
16+
17+
/**
18+
* Attacher.
19+
*
20+
* @param {Mdast} node - MDAST.
21+
* @param {Object} options - Configuration.
22+
* @return {Array} - TOC Markdown.
23+
*/
24+
function attacher(node, options) {
25+
var settings = options || {};
26+
var heading = settings.heading ? toExpression(settings.heading) : null;
27+
var depth = settings.maxDepth || 6;
28+
var tight = settings.tight;
29+
30+
var result = search(node, heading, depth);
31+
32+
if (result.index === null || !result.map.length) {
33+
return [];
34+
}
35+
36+
return [contents(result.map, tight)];
37+
}

lib/insert.js

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* @author Titus Wormer
3+
* @copyright 2015 Titus Wormer
4+
* @license MIT
5+
* @module mdast:toc
6+
* @fileoverview Generate a Table of Contents (TOC) from a given Markdown file.
7+
*/
8+
9+
/* Expose. */
10+
module.exports = insert;
11+
12+
/* Dependencies */
13+
var listItem = require('./list-item');
14+
var list = require('./list');
15+
16+
/* Constants */
17+
var LIST = 'list';
18+
var LIST_ITEM = 'listItem';
19+
var PARAGRAPH = 'paragraph';
20+
var LINK = 'link';
21+
var TEXT = 'text';
22+
23+
/**
24+
* Insert a `node` into a `parent`.
25+
*
26+
* @param {Object} node - `node` to insert.
27+
* @param {Object} parent - Parent of `node`.
28+
* @param {boolean?} [tight] - Prefer tight list-items.
29+
* @return {undefined}
30+
*/
31+
function insert(node, parent, tight) {
32+
var children = parent.children;
33+
var length = children.length;
34+
var last = children[length - 1];
35+
var isLoose = false;
36+
var index;
37+
var item;
38+
39+
if (node.depth === 1) {
40+
item = listItem();
41+
42+
item.children.push({
43+
type: PARAGRAPH,
44+
children: [
45+
{
46+
type: LINK,
47+
title: null,
48+
url: '#' + node.id,
49+
children: [
50+
{
51+
type: TEXT,
52+
value: node.value
53+
}
54+
]
55+
}
56+
]
57+
});
58+
59+
children.push(item);
60+
} else if (last && last.type === LIST_ITEM) {
61+
insert(node, last, tight);
62+
} else if (last && last.type === LIST) {
63+
node.depth--;
64+
65+
insert(node, last);
66+
} else if (parent.type === LIST) {
67+
item = listItem();
68+
69+
insert(node, item);
70+
71+
children.push(item);
72+
} else {
73+
item = list();
74+
node.depth--;
75+
76+
insert(node, item);
77+
78+
children.push(item);
79+
}
80+
81+
/*
82+
* Properly style list-items with new lines.
83+
*/
84+
85+
if (parent.type === LIST_ITEM) {
86+
parent.loose = tight ? false : children.length > 1;
87+
} else {
88+
if (tight) {
89+
isLoose = false;
90+
} else {
91+
index = -1;
92+
93+
while (++index < length) {
94+
if (children[index].loose) {
95+
isLoose = true;
96+
97+
break;
98+
}
99+
}
100+
}
101+
102+
index = -1;
103+
104+
while (++index < length) {
105+
children[index].loose = isLoose;
106+
}
107+
}
108+
}

lib/is-closing-heading.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @author Titus Wormer
3+
* @copyright 2015 Titus Wormer
4+
* @license MIT
5+
* @module mdast:toc
6+
* @fileoverview Generate a Table of Contents (TOC) from a given Markdown file.
7+
*/
8+
9+
/* Expose. */
10+
module.exports = isClosingHeading;
11+
12+
/* Constants. */
13+
var HEADING = 'heading';
14+
15+
/**
16+
* Check if `node` is the next heading.
17+
*
18+
* @param {Node} node - Node to check.
19+
* @param {number} depth - Depth of opening heading.
20+
* @return {boolean} - Whether znode is a closing heading.
21+
*/
22+
function isClosingHeading(node, depth) {
23+
return depth && node && node.type === HEADING && node.depth <= depth;
24+
}

lib/is-opening-heading.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @author Titus Wormer
3+
* @copyright 2015 Titus Wormer
4+
* @license MIT
5+
* @module mdast:toc
6+
* @fileoverview Generate a Table of Contents (TOC) from a given Markdown file.
7+
*/
8+
9+
/* Expose. */
10+
module.exports = isOpeningHeading;
11+
12+
/* Dependencies */
13+
var toString = require('mdast-util-to-string');
14+
15+
/* Constants. */
16+
var HEADING = 'heading';
17+
18+
/**
19+
* Check if `node` is the main heading.
20+
*
21+
* @param {Node} node - Node to check.
22+
* @param {number} depth - Depth to check.
23+
* @param {RegExp} expression - Expression to check.
24+
* @return {boolean} - Whether `node` is a main heading.
25+
*/
26+
function isOpeningHeading(node, depth, expression) {
27+
return depth === null && node && node.type === HEADING &&
28+
expression.test(toString(node));
29+
}

lib/list-item.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @author Titus Wormer
3+
* @copyright 2015 Titus Wormer
4+
* @license MIT
5+
* @module mdast:toc
6+
* @fileoverview Generate a Table of Contents (TOC) from a given Markdown file.
7+
*/
8+
9+
/* Expose. */
10+
module.exports = listItem;
11+
12+
/* Constants */
13+
var LIST_ITEM = 'listItem';
14+
15+
/**
16+
* Create a list item.
17+
*
18+
* @return {Object} - List-item node.
19+
*/
20+
function listItem() {
21+
return {
22+
type: LIST_ITEM,
23+
loose: false,
24+
children: []
25+
};
26+
}

0 commit comments

Comments
 (0)