@@ -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
0 commit comments