fix: title in containers not working with markdown content (#765) (#768)

fix #765
pull/779/head
Divyansh Singh 2 years ago committed by GitHub
parent dc29dba3fb
commit c5c3c64851
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,13 +1,14 @@
import MarkdownIt from 'markdown-it' import MarkdownIt from 'markdown-it'
import { RenderRule } from 'markdown-it/lib/renderer'
import Token from 'markdown-it/lib/token' import Token from 'markdown-it/lib/token'
import container from 'markdown-it-container' import container from 'markdown-it-container'
export const containerPlugin = (md: MarkdownIt) => { export const containerPlugin = (md: MarkdownIt) => {
md.use(...createContainer('tip', 'TIP')) md.use(...createContainer('tip', 'TIP', md))
.use(...createContainer('info', 'INFO')) .use(...createContainer('info', 'INFO', md))
.use(...createContainer('warning', 'WARNING')) .use(...createContainer('warning', 'WARNING', md))
.use(...createContainer('danger', 'DANGER')) .use(...createContainer('danger', 'DANGER', md))
.use(...createContainer('details', 'Details')) .use(...createContainer('details', 'Details', md))
// explicitly escape Vue syntax // explicitly escape Vue syntax
.use(container, 'v-pre', { .use(container, 'v-pre', {
render: (tokens: Token[], idx: number) => render: (tokens: Token[], idx: number) =>
@ -15,15 +16,13 @@ export const containerPlugin = (md: MarkdownIt) => {
}) })
} }
type ContainerArgs = [ type ContainerArgs = [typeof container, string, { render: RenderRule }]
typeof container,
string,
{
render(tokens: Token[], idx: number): string
}
]
function createContainer(klass: string, defaultTitle: string): ContainerArgs { function createContainer(
klass: string,
defaultTitle: string,
md: MarkdownIt
): ContainerArgs {
return [ return [
container, container,
klass, klass,
@ -32,14 +31,11 @@ function createContainer(klass: string, defaultTitle: string): ContainerArgs {
const token = tokens[idx] const token = tokens[idx]
const info = token.info.trim().slice(klass.length).trim() const info = token.info.trim().slice(klass.length).trim()
if (token.nesting === 1) { if (token.nesting === 1) {
const title = md.renderInline(info || defaultTitle)
if (klass === 'details') { if (klass === 'details') {
return `<details class="${klass} custom-block">${ return `<details class="${klass} custom-block"><summary>${title}</summary>\n`
info ? `<summary>${info}</summary>` : `<summary>Details</summary>`
}\n`
} }
return `<div class="${klass} custom-block"><p class="custom-block-title">${ return `<div class="${klass} custom-block"><p class="custom-block-title">${title}</p>\n`
info || defaultTitle
}</p>\n`
} else { } else {
return klass === 'details' ? `</details>\n` : `</div>\n` return klass === 'details' ? `</details>\n` : `</div>\n`
} }

Loading…
Cancel
Save