Find range of a target heading and make it collapsible by <details>
and <summary>
like the below.
Open example
Tada! 🎉
<details><summary>Open example</summary>
Tada! :tada:
</details>
npm install remark-collapse
const processor = remark()
.use(collapse, {test: 'tango'})
const inputString = [
'# Heading1',
'',
'## tango',
'',
'target content',
'',
'## Another heading2',
''
].join('\n')
const resultString = processor.processSync(inputString).toString()
resultString
# Heading1
## tango
<details><summary>Open tango</summary>
target content
</details>
## Another heading2
To find heading range, Remark Collapse uses mdast-util-heading-range.
Required. Test to be passed to mdast-util-heading-range#heading
test
— Heading to look for. Whenstring
, wrapped innew RegExp('^(' + value + ')$', 'i')
; whenRegExp
, wrapped infunction (value) {expression.test(value)}
.
Summarizing function. By default, it uses text of the target heading and prepends Open
to it.
function defaultSummarizer (str) {
return 'Open ' + str
}
Example
function defuseTimeBomb (str) {
return 'Tick... Tock... Tick... Tock...⏱'
}
const processor = remark()
.use(collapse, {
test: 'tango',
summary: defuseTimeBomb
/* Also, you can provide a string
summary: 'Tick... Tock... Tick... Tock...⏱'
*/
})
const inputString = [
'# tango',
'',
'**KABOOM!!**',
''
].join()
const resultString = processor.processSync(inputString).toString()
resultString
# tango
<details><summary>Tick... Tock... Tick... Tock...⏱</summary>
**KABOOM!!**
</details>
Result
Tick... Tock... Tick... Tock...⏱
KABOOM!!
MIT © Junyoung Choi