Skip to content

Commit 924868b

Browse files
committed
perf: support collapsed option #8
1 parent 917036d commit 924868b

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

lib/index.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ const generateContent = (configs, content = '') => {
4949
'copyright': 'Copyright © aleen42',
5050
'minHeaderCount': '1',
5151
'minHeaderDeep': '1',
52+
'collapsed': false,
5253
}, configs || {});
5354

54-
const renderContent = pageTreeview.initHeaders(content, options.minHeaderCount, options.minHeaderDeep);
55+
const renderContent = pageTreeview.initHeaders(content, options);
5556

5657
/** check whether the option copyright is empty */
5758
const copyRight = options.copyright ? (`
@@ -133,12 +134,11 @@ const pageTreeview = module.exports = {
133134

134135
/**
135136
* [initHeaders: get headers of a page]
136-
* @param {string} content [description]
137-
* @param {string} count [description]
138-
* @param {string} deep [description]
139-
* @return {string} [description]
137+
* @param {string} content
138+
* @param {object} options
139+
* @return {string}
140140
*/
141-
initHeaders: (content, count, deep) => {
141+
initHeaders: (content, {minHeaderCount: count, minHeaderDeep: deep, collapsed}) => {
142142
const minHeaderCount = _.isNUM(parseInt(count)) ? parseInt(count) : 1;
143143
const minHeaderDeep = _.isNUM(parseInt(deep)) ? parseInt(deep) : 1;
144144

@@ -238,24 +238,35 @@ const pageTreeview = module.exports = {
238238
/** it's a parent node and replace it */
239239
/** the length of "<div" is 4 */
240240
const separatorIndex = '<i'.length;
241+
const state = collapsed ? 'hidden' : 'opened';
241242
generatedHTML = generatedHTML.substring(0, node.index + separatorIndex)
242-
+ ' class="level__parent level__item level__parent--opened" state="opened" onclick="'
243-
+ 'var curState = this.getAttribute(\'state\');'
244-
+ 'var nextState = curState === \'opened\' ? \'hidden\' : \'opened\';'
245-
+ 'this.setAttribute(\'state\', nextState);'
243+
+ ` class="level__parent level__item level__parent--${state}" state="${state}" onclick="`
244+
+ `var curState = this.getAttribute('state');`
245+
+ `var nextState = curState === 'opened' ? 'hidden' : 'opened';`
246+
+ `this.setAttribute('state', nextState);`
246247
+ 'this.className = this.className.split(curState).join(nextState);'
247248
+ ''
248249
+ 'var list = this.parentNode.nextElementSibling;'
249-
+ 'if (nextState === \'hidden\') {'
250-
+ ' list.style.display = \'none\';'
250+
+ `if (nextState === 'hidden') {`
251+
+ ` list.style.display = 'none';`
251252
+ '} else {'
252-
+ ' list.style.display = \'block\';'
253+
+ ` list.style.display = 'block';`
253254
+ '}'
254255
+ '"'
255256
+ generatedHTML.substring(node.index + separatorIndex);
256257
}
257258
}
258259

260+
if (collapsed) {
261+
const {parse: parseHtml} = require('node-html-parser');
262+
const ast = parseHtml(generatedHTML);
263+
ast.querySelectorAll('ul')
264+
.forEach((node => node.parentNode && node.parentNode.tagName === 'li'
265+
&& node.parentNode.childNodes[0].nodeType !== 3 // has hook for collapsing (not text node before)
266+
&& (node.rawAttrs += `style="display: none;"`))) // collapse submenu
267+
return ast.toString();
268+
}
269+
259270
return generatedHTML;
260271
},
261272

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"homepage": "https://github.com/aleen42/gitbook-treeview#readme",
3030
"dependencies": {
3131
"remarkable": "^1.6.2",
32-
"remove-markdown": "^0.1.0"
32+
"remove-markdown": "^0.1.0",
33+
"node-html-parser": "^1.2.20"
3334
},
3435
"devDependencies": {
3536
"chai": "^3.5.0",
@@ -51,6 +52,11 @@
5152
"type": "string",
5253
"default": "1",
5354
"description": "the minimum of the depth of headers"
55+
},
56+
"collapsed": {
57+
"type": "boolean",
58+
"default": false,
59+
"description": "whether collapse subheaders by default"
5460
}
5561
}
5662
}

test/index.js

Lines changed: 7 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)