mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-14 15:14:56 -04:00
added a spoiler container (#947)
changed toolbar to insert the new spoiler container changed tests accordingly References: https://github.com/markdown-it/markdown-it-container Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
parent
9330adf564
commit
7be64bc582
9 changed files with 60 additions and 18 deletions
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import MarkdownIt from 'markdown-it'
|
||||
import { escapeHtml } from 'markdown-it/lib/common/utils'
|
||||
import markdownItContainer from 'markdown-it-container'
|
||||
import Token from 'markdown-it/lib/token'
|
||||
import { MarkdownItPlugin } from '../replace-components/ComponentReplacer'
|
||||
import { RenderContainerReturn } from './alert-container'
|
||||
|
||||
export const spoilerRegEx = /^spoiler\s+(.*)$/;
|
||||
|
||||
const createSpoilerContainer = (): RenderContainerReturn => {
|
||||
return (tokens: Token[], index: number) => {
|
||||
const matches = spoilerRegEx.exec(tokens[index].info.trim())
|
||||
|
||||
if (tokens[index].nesting === 1 && matches && matches[1]) {
|
||||
// opening tag
|
||||
return `<details><summary>${escapeHtml(matches[1])}</summary>`
|
||||
|
||||
} else {
|
||||
// closing tag
|
||||
return '</details>\n'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const spoilerContainer: MarkdownItPlugin = (markdownIt: MarkdownIt) => {
|
||||
markdownItContainer(markdownIt, 'spoiler', {
|
||||
validate: (params: string) => spoilerRegEx.test(params),
|
||||
render: createSpoilerContainer()
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue