-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add collapsible heading plugin * Add docs * Remove default heading collapses * Updates based on review * Updates based on review * Default plugin to enabled and transform all headings * Update README.md
- Loading branch information
Showing
10 changed files
with
482 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* | ||
Copyright 2024 DigitalOcean | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
/** | ||
* @module rules/collapsible_heading | ||
*/ | ||
|
||
const safeObject = require('../util/safe_object'); | ||
|
||
/** | ||
* @typedef {Object} CollapsibleHeadingOptions | ||
* @property {number[]} [levels=[1,2,3,4,5,6]] List of headings to transform. | ||
* @property {boolean} [open=true] Whether to collapse the content by default. | ||
* @property {string} [className=collapsible] Class to use for collapsible sections. | ||
*/ | ||
|
||
/** | ||
* Wrap specific headings in detail tag and make the content collapsible | ||
* | ||
* If an array of heading tags is provided, all those tags and the related content will be wrapped in a details tag, with the heading as the summary. | ||
* | ||
* Nesting multiple collapsible sections is supported. | ||
* | ||
* @example | ||
* # H1 header | ||
* Test row | ||
* | ||
* <details class="collapsible"> | ||
* <summary> | ||
* <h1>H1 header</h1> | ||
* </summary> | ||
* <p>Test row</p> | ||
* </details> | ||
* | ||
* @type {import('markdown-it').PluginWithOptions<CollapsibleHeadingOptions>} | ||
*/ | ||
module.exports = (md, options) => { | ||
// Get the correct options | ||
const optsObj = safeObject(options); | ||
|
||
if (!Array.isArray(optsObj.levels) || !optsObj.levels.length) { | ||
optsObj.levels = [ 1, 2, 3, 4, 5, 6 ]; | ||
} | ||
|
||
/** | ||
* Rule for inserting details around headings. | ||
* | ||
* @type {import('markdown-it').RuleCore} | ||
* @private | ||
*/ | ||
const collapsibleHeading = state => { | ||
// Track levels of collapsible headings that're open | ||
const collapsed = []; | ||
state.tokens = state.tokens.reduce((newTokens, token) => { | ||
if (token.type === 'heading_open') { | ||
const headingLevel = Number(token.tag.replace('h', '')); | ||
// Close all open collapsible headings deeper than this | ||
while (collapsed[collapsed.length - 1] >= headingLevel) { | ||
// Close the previous detail element | ||
const closeDetail = new state.Token('details', 'details', -1); | ||
closeDetail.block = true; | ||
newTokens.push(closeDetail); | ||
|
||
// Remove the closed collapsible from the list. | ||
collapsed.pop(); | ||
} | ||
|
||
if (Array.isArray(optsObj.levels) && optsObj.levels.includes(headingLevel)) { | ||
// Create the outer details element | ||
const openDetails = new state.Token('details', 'details', 1); | ||
openDetails.block = true; | ||
openDetails.attrSet('class', md.utils.escapeHtml(typeof optsObj.className === 'string' ? optsObj.className : 'collapsible')); | ||
if (optsObj.open !== false) openDetails.attrSet('open', ''); | ||
newTokens.push(openDetails); | ||
|
||
// Create the summary element | ||
const openSummary = new state.Token('summary', 'summary', 1); | ||
openSummary.block = true; | ||
newTokens.push(openSummary); | ||
|
||
// Add the heading | ||
newTokens.push(token); | ||
|
||
// Track that we have an open collapsible heading | ||
collapsed.push(headingLevel); | ||
|
||
return newTokens; | ||
} | ||
} | ||
|
||
if (token.type === 'heading_close' | ||
&& Array.isArray(optsObj.levels) | ||
&& optsObj.levels.includes(Number(token.tag.replace('h', ''))) | ||
) { | ||
// Add the heading close | ||
newTokens.push(token); | ||
|
||
// Close the summary element | ||
const closeSummary = new state.Token('summary', 'summary', -1); | ||
closeSummary.block = true; | ||
newTokens.push(closeSummary); | ||
|
||
return newTokens; | ||
} | ||
|
||
// Add the current token | ||
newTokens.push(token); | ||
|
||
return newTokens; | ||
}, []); | ||
|
||
collapsed.forEach(() => { | ||
// Close all remaining previous detail elements | ||
const closeDetail = new state.Token('details', 'details', -1); | ||
closeDetail.block = true; | ||
state.tokens.push(closeDetail); | ||
}); | ||
}; | ||
|
||
md.core.ruler.push('collapsible_heading', collapsibleHeading); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
Copyright 2024 DigitalOcean | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const md = require('markdown-it')().use(require('./collapsible_heading')); | ||
|
||
it('injects collapsibles by default for all headings', () => { | ||
expect(md.render('# H1 header\nTest row\n\n## H2 header\nTest row\n\n### H3 header\nTest row\n\n#### H4 header\nTest row\n\n##### H5 header\nTest row\n\n###### H6 header\nTest row\n\n')).toBe(`<details class="collapsible" open=""> | ||
<summary> | ||
<h1>H1 header</h1> | ||
</summary> | ||
<p>Test row</p> | ||
<details class="collapsible" open=""> | ||
<summary> | ||
<h2>H2 header</h2> | ||
</summary> | ||
<p>Test row</p> | ||
<details class="collapsible" open=""> | ||
<summary> | ||
<h3>H3 header</h3> | ||
</summary> | ||
<p>Test row</p> | ||
<details class="collapsible" open=""> | ||
<summary> | ||
<h4>H4 header</h4> | ||
</summary> | ||
<p>Test row</p> | ||
<details class="collapsible" open=""> | ||
<summary> | ||
<h5>H5 header</h5> | ||
</summary> | ||
<p>Test row</p> | ||
<details class="collapsible" open=""> | ||
<summary> | ||
<h6>H6 header</h6> | ||
</summary> | ||
<p>Test row</p> | ||
</details> | ||
</details> | ||
</details> | ||
</details> | ||
</details> | ||
</details> | ||
`); | ||
}); | ||
|
||
const mdAllowed = require('markdown-it')({ }).use(require('./collapsible_heading'), { levels: [ 1 ] }); | ||
|
||
it('only wraps specified headings', () => { | ||
expect(mdAllowed.render('# H1 header\nTest row\n\n## H2 header\nTest row')).toBe(`<details class="collapsible" open=""> | ||
<summary> | ||
<h1>H1 header</h1> | ||
</summary> | ||
<p>Test row</p> | ||
<h2>H2 header</h2> | ||
<p>Test row</p> | ||
</details> | ||
`); | ||
}); | ||
|
||
const mdUsesClassName = require('markdown-it')({ }).use(require('./collapsible_heading'), { levels: [ 1 ], className: 'test' }); | ||
|
||
it('uses given classname', () => { | ||
expect(mdUsesClassName.render('# H1 header\nTest row\n\n## H2 header\nTest row')).toBe(`<details class="test" open=""> | ||
<summary> | ||
<h1>H1 header</h1> | ||
</summary> | ||
<p>Test row</p> | ||
<h2>H2 header</h2> | ||
<p>Test row</p> | ||
</details> | ||
`); | ||
}); | ||
|
||
it('handles same level breaks correctly', () => { | ||
expect(mdAllowed.render('# H1 header\nTest row\n\n# H1 header\nTest row')).toBe(`<details class="collapsible" open=""> | ||
<summary> | ||
<h1>H1 header</h1> | ||
</summary> | ||
<p>Test row</p> | ||
</details> | ||
<details class="collapsible" open=""> | ||
<summary> | ||
<h1>H1 header</h1> | ||
</summary> | ||
<p>Test row</p> | ||
</details> | ||
`); | ||
}); | ||
|
||
const mdAllowedTwo = require('markdown-it')({ }).use(require('./collapsible_heading'), { levels: [ 2 ] }); | ||
|
||
it('handles different level breaks correctly', () => { | ||
expect(mdAllowedTwo.render('## H2 header\nTest row\n\n# H1 header\nTest row')).toBe(`<details class="collapsible" open=""> | ||
<summary> | ||
<h2>H2 header</h2> | ||
</summary> | ||
<p>Test row</p> | ||
</details> | ||
<h1>H1 header</h1> | ||
<p>Test row</p> | ||
`); | ||
}); | ||
|
||
const mdClosed = require('markdown-it')({ }).use(require('./collapsible_heading'), { levels: [ 2 ], open: false }); | ||
|
||
it('renders the detail closed', () => { | ||
expect(mdClosed.render('## H2 header\nTest row\n\n# H1 header\nTest row')).toBe(`<details class="collapsible"> | ||
<summary> | ||
<h2>H2 header</h2> | ||
</summary> | ||
<p>Test row</p> | ||
</details> | ||
<h1>H1 header</h1> | ||
<p>Test row</p> | ||
`); | ||
}); | ||
|
||
const mdDeactivated = require('markdown-it')({ }); | ||
|
||
it('renders the heading without wrapping', () => { | ||
expect(mdDeactivated.render('# H1 header\nTest row')).toBe(`<h1>H1 header</h1> | ||
<p>Test row</p> | ||
`); | ||
}); |
Oops, something went wrong.