@substrate-system/markdown-toc 1.3.5 → 1.3.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js DELETED
@@ -1,255 +0,0 @@
1
- 'use strict';
2
-
3
- /*!
4
- * markdown-toc <https://github.com/jonschlinkert/markdown-toc>
5
- *
6
- * Copyright © 2013-2023, Jon Schlinkert.
7
- * Released under the MIT License.
8
- */
9
-
10
- var utils = require('./lib/utils');
11
- var querystring = require('querystring');
12
-
13
- /**
14
- * expose `toc`
15
- */
16
-
17
- module.exports = toc;
18
-
19
- /**
20
- * Load `generate` as a remarkable plugin and
21
- * expose the `toc` function.
22
- *
23
- * @param {String} `str` String of markdown
24
- * @param {Object} `options`
25
- * @return {String} Markdown-formatted table of contents
26
- */
27
-
28
- function toc(str, options) {
29
- return new utils.Remarkable()
30
- .use(generate(options))
31
- .render(str);
32
- }
33
-
34
- /**
35
- * Expose `insert` method
36
- */
37
-
38
- toc.insert = require('./lib/insert');
39
-
40
- /**
41
- * Generate a markdown table of contents. This is the
42
- * function that does all of the main work with Remarkable.
43
- *
44
- * @param {Object} `options`
45
- * @return {String}
46
- */
47
-
48
- function generate(options) {
49
- var opts = utils.merge({firsth1: true, maxdepth: 6}, options);
50
- var stripFirst = opts.firsth1 === false;
51
- if (typeof opts.linkify === 'undefined') opts.linkify = true;
52
-
53
- return function(md) {
54
- md.renderer.render = function(tokens) {
55
- tokens = tokens.slice();
56
- var seen = {};
57
- var len = tokens.length, i = 0, num = 0;
58
- var tocstart = -1;
59
- var arr = [];
60
- var res = {};
61
-
62
- while (len--) {
63
- var token = tokens[i++];
64
- if (/<!--[ \t]*toc[ \t]*-->/.test(token.content)) {
65
- tocstart = token.lines[1];
66
- }
67
-
68
- if (token.type === 'heading_open') {
69
- tokens[i].lvl = tokens[i - 1].hLevel;
70
- tokens[i].i = num++;
71
- arr.push(tokens[i]);
72
- }
73
- }
74
-
75
- var result = [];
76
- res.json = [];
77
-
78
- // exclude headings that come before the actual
79
- // table of contents.
80
- var alen = arr.length, j = 0;
81
- while (alen--) {
82
- var tok = arr[j++];
83
-
84
- if (tok.lines && (tok.lines[0] > tocstart)) {
85
- var val = tok.content;
86
- if (tok.children && tok.children[0].type === 'link_open') {
87
- if (tok.children[1].type === 'text') {
88
- val = tok.children[1].content;
89
- }
90
- }
91
-
92
- if (!seen.hasOwnProperty(val)) {
93
- seen[val] = 0;
94
- } else {
95
- seen[val]++;
96
- }
97
-
98
- tok.seen = opts.num = seen[val];
99
- tok.slug = utils.slugify(val, opts);
100
- res.json.push(utils.pick(tok, ['content', 'slug', 'lvl', 'i', 'seen']));
101
- if (opts.linkify) tok = linkify(tok, opts);
102
- result.push(tok);
103
- }
104
- }
105
-
106
- opts.highest = highest(result);
107
- res.highest = opts.highest;
108
- res.tokens = tokens;
109
-
110
- if (stripFirst) result = result.slice(1);
111
- res.content = bullets(result, opts);
112
- res.content += (opts.append || '');
113
- return res;
114
- };
115
- };
116
- }
117
-
118
- /**
119
- * Render markdown list bullets
120
- *
121
- * @param {Array} `arr` Array of listitem objects
122
- * @param {Object} `opts`
123
- * @return {String}
124
- */
125
-
126
- function bullets(arr, options) {
127
- var opts = utils.merge({indent: ' '}, options);
128
- opts.chars = opts.chars || opts.bullets || ['-', '*', '+'];
129
- var unindent = 0;
130
-
131
- var listitem = utils.li(opts);
132
- var fn = typeof opts.filter === 'function'
133
- ? opts.filter
134
- : null;
135
-
136
- // Keep the first h1? This is `true` by default
137
- if (opts && opts.firsth1 === false) {
138
- unindent = 1;
139
- }
140
-
141
- var len = arr.length;
142
- var res = [];
143
- var i = 0;
144
-
145
- while (i < len) {
146
- var ele = arr[i++];
147
- ele.lvl -= unindent;
148
- if (fn && !fn(ele.content, ele, arr)) {
149
- continue;
150
- }
151
-
152
- if (ele.lvl > opts.maxdepth) {
153
- continue;
154
- }
155
-
156
- var lvl = ele.lvl - opts.highest;
157
- res.push(listitem(lvl, ele.content, opts));
158
- }
159
- return res.join('\n');
160
- }
161
-
162
- /**
163
- * Get the highest heading level in the array, so
164
- * we can un-indent the proper number of levels.
165
- *
166
- * @param {Array} `arr` Array of tokens
167
- * @return {Number} Highest level
168
- */
169
-
170
- function highest(arr) {
171
- var res = arr.slice().sort(function(a, b) {
172
- return a.lvl - b.lvl;
173
- });
174
- if (res && res.length) {
175
- return res[0].lvl;
176
- }
177
- return 0;
178
- }
179
-
180
- /**
181
- * Turn headings into anchors
182
- */
183
-
184
- function linkify(tok, options) {
185
- var opts = utils.merge({}, options);
186
- if (tok && tok.content) {
187
- opts.num = tok.seen;
188
- var text = titleize(tok.content, opts);
189
- var slug = utils.slugify(tok.content, opts);
190
- slug = querystring.escape(slug);
191
- if (opts && typeof opts.linkify === 'function') {
192
- return opts.linkify(tok, text, slug, opts);
193
- }
194
- tok.content = utils.mdlink(text, '#' + slug);
195
- }
196
- return tok;
197
- }
198
-
199
- /**
200
- * Titleize the title part of a markdown link.
201
- *
202
- * @name options.titleize
203
- * @param {String} `str` The string to titleize
204
- * @param {Object} `opts` Pass a custom titleize function on `titleize`
205
- * @return {String}
206
- * @api public
207
- */
208
-
209
- function titleize(str, opts) {
210
- if (opts && opts.strip) { return strip(str, opts); }
211
- if (opts && opts.titleize === false) return str;
212
- if (opts && typeof opts.titleize === 'function') {
213
- return opts.titleize(str, opts);
214
- }
215
- str = utils.getTitle(str);
216
- str = str.split(/<\/?[^>]+>/).join('');
217
- str = str.split(/[ \t]+/).join(' ');
218
- return str.trim();
219
- }
220
-
221
- /**
222
- * Optionally strip specified words from heading text (not url)
223
- *
224
- * @name options.strip
225
- * @param {String} `str`
226
- * @param {String} `opts`
227
- * @return {String}
228
- */
229
-
230
- function strip(str, opts) {
231
- opts = opts || {};
232
- if (!opts.strip) return str;
233
- if (typeof opts.strip === 'function') {
234
- return opts.strip(str, opts);
235
- }
236
- if (Array.isArray(opts.strip) && opts.strip.length) {
237
- var res = opts.strip.join('|');
238
- var re = new RegExp(res, 'g');
239
- str = str.trim().replace(re, '');
240
- return str.replace(/^-|-$/g, '');
241
- }
242
- return str;
243
- }
244
-
245
- /**
246
- * Expose utils
247
- */
248
-
249
- toc.utils = utils;
250
- toc.bullets = bullets;
251
- toc.linkify = linkify;
252
- toc.slugify = utils.slugify;
253
- toc.titleize = titleize;
254
- toc.plugin = generate;
255
- toc.strip = strip;
package/lib/insert.js DELETED
@@ -1,67 +0,0 @@
1
- 'use strict';
2
-
3
- var toc = require('..');
4
- var utils = require('./utils');
5
-
6
- /**
7
- * The basic idea:
8
- *
9
- * 1. when front-matter exists, we need to avoid turning its properties into headings.
10
- * 2. We need to detect toc markers on the page. For now it's a simple HTML code comment
11
- * to ensure the markdown is compatible with any parser.
12
- *
13
- * @param {String} `str` Pass a string of markdown
14
- * @param {Object} `options` Pass options to toc generation
15
- * @return {String} Get the same string back with a TOC inserted
16
- */
17
-
18
- module.exports = function insert(str, options) {
19
- options = options || {};
20
-
21
- var regex = options.regex || /(?:<!-- toc(?:\s*stop)? -->)/g;
22
- var open = typeof options.open === 'string' ? options.open : '<!-- toc -->\n\n';
23
- var close = typeof options.close === 'string' ? options.close : '<!-- tocstop -->';
24
- var obj;
25
-
26
- var newlines = '';
27
- var m = /\n+$/.exec(str);
28
- if (m) newlines = m[0];
29
-
30
- // does the file have front-matter?
31
- if (/^---/.test(str)) {
32
- // extract it temporarily so the syntax
33
- // doesn't get mistaken for a heading
34
- obj = utils.matter(str);
35
- str = obj.content;
36
- }
37
-
38
- var sections = split(str, regex);
39
- if (sections.length > 3) {
40
- throw new Error('markdown-toc only supports one Table of Contents per file.');
41
- }
42
-
43
- var last = sections[sections.length - 1];
44
- if (sections.length === 3) {
45
- sections.splice(1, 1, open + (options.toc || toc(last, options).content));
46
- sections.splice(2, 0, close);
47
- }
48
-
49
- if (sections.length === 2) {
50
- sections.splice(1, 0, open + toc(last, options).content + '\n\n' + close);
51
- }
52
-
53
- var resultString = sections.join('\n\n') + newlines;
54
- // if front-matter was found, put it back now
55
- if (obj) {
56
- return utils.matter.stringify(resultString, obj.data);
57
- }
58
- return resultString;
59
- };
60
-
61
- function split(str, re) {
62
- return str.split(re).map(trim);
63
- }
64
-
65
- function trim(str) {
66
- return str.trim();
67
- }
package/lib/utils.js DELETED
@@ -1,86 +0,0 @@
1
- 'use strict';
2
-
3
- /**
4
- * Module dependencies
5
- */
6
-
7
- var diacritics = require('diacritics-map');
8
- var utils = require('lazy-cache')(require);
9
- var fn = require;
10
- require = utils;
11
-
12
- /**
13
- * Lazily required module dependencies
14
- */
15
-
16
- require('concat-stream', 'concat');
17
- require('gray-matter', 'matter');
18
- require('list-item', 'li');
19
- require('markdown-link', 'mdlink');
20
- require('minimist');
21
- require('mixin-deep', 'merge');
22
- require('object.pick', 'pick');
23
- require('remarkable', 'Remarkable');
24
- require('repeat-string', 'repeat');
25
- require('strip-color');
26
- require = fn;
27
-
28
- /**
29
- * Get the "title" from a markdown link
30
- */
31
-
32
- utils.getTitle = function(str) {
33
- if (/^\[[^\]]+\]\(/.test(str)) {
34
- var m = /^\[([^\]]+)\]/.exec(str);
35
- if (m) return m[1];
36
- }
37
- return str;
38
- };
39
-
40
- /**
41
- * Slugify the url part of a markdown link.
42
- *
43
- * @name options.slugify
44
- * @param {String} `str` The string to slugify
45
- * @param {Object} `options` Pass a custom slugify function on `options.slugify`
46
- * @return {String}
47
- * @api public
48
- */
49
-
50
- utils.slugify = function(str, options) {
51
- options = options || {};
52
- if (options.slugify === false) return str;
53
- if (typeof options.slugify === 'function') {
54
- return options.slugify(str, options);
55
- }
56
-
57
- str = utils.getTitle(str);
58
- str = utils.stripColor(str);
59
- str = str.toLowerCase();
60
-
61
- // `.split()` is often (but not always) faster than `.replace()`
62
- str = str.split(' ').join('-');
63
- str = str.split(/\t/).join('--');
64
- if (options.stripHeadingTags !== false) {
65
- str = str.split(/<\/?[^>]+>/).join('');
66
- }
67
- str = str.split(/[|$&`~=\\\/@+*!?({[\]})<>=.,;:'"^]/).join('');
68
- str = str.split(/[。?!,、;:“”【】()〔〕[]﹃﹄“ ”‘’﹁﹂—…-~《》〈〉「」]/).join('');
69
- str = replaceDiacritics(str);
70
- if (options.num) {
71
- str += '-' + options.num;
72
- }
73
- return str;
74
- };
75
-
76
- function replaceDiacritics(str) {
77
- return str.replace(/[À-ž]/g, function(ch) {
78
- return diacritics[ch] || ch;
79
- });
80
- }
81
-
82
- /**
83
- * Expose `utils` modules
84
- */
85
-
86
- module.exports = utils;