Merge pull request #1272 from hackmdio/bugfix/support-empty-spoiler

Support empty spoiler container syntax
This commit is contained in:
Max Wu 2019-09-08 01:10:16 +08:00 committed by GitHub
commit df2c0e23b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 7 deletions

View File

@ -1030,14 +1030,19 @@ md.use(markdownitContainer, 'warning', { render: renderContainer })
md.use(markdownitContainer, 'danger', { render: renderContainer })
md.use(markdownitContainer, 'spoiler', {
validate: function (params) {
return params.trim().match(/^spoiler\s+(.*)$/)
return params.trim().match(/^spoiler(\s+.*)?$/)
},
render: function (tokens, idx) {
var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/)
const m = tokens[idx].info.trim().match(/^spoiler(\s+.*)?$/)
if (tokens[idx].nesting === 1) {
// opening tag
return '<details><summary>' + md.utils.escapeHtml(m[1]) + '</summary>\n'
const summary = m[1] && m[1].trim()
if (summary) {
return `<details><summary>${md.utils.escapeHtml(summary)}</summary>\n`
} else {
return `<details>\n`
}
} else {
// closing tag
return '</details>\n'

View File

@ -112,17 +112,23 @@ md.use(markdownitContainer, 'warning', { render: renderContainer })
md.use(markdownitContainer, 'danger', { render: renderContainer })
md.use(markdownitContainer, 'spoiler', {
validate: function (params) {
return params.trim().match(/^spoiler\s+(.*)$/)
return params.trim().match(/^spoiler(\s+.*)?$/)
},
render: function (tokens, idx) {
var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/)
const m = tokens[idx].info.trim().match(/^spoiler(\s+.*)?$/)
if (tokens[idx].nesting === 1) {
// opening tag
const startline = tokens[idx].map[0] + 1
const endline = tokens[idx].map[1]
// opening tag
return `<details class="part raw" data-startline="${startline}" data-endline="${endline}"><summary>` + md.utils.escapeHtml(m[1]) + '</summary>\n'
const partClass = `class="part raw" data-startline="${startline}" data-endline="${endline}"`
const summary = m[1] && m[1].trim()
if (summary) {
return `<details ${partClass}><summary>${md.utils.escapeHtml(summary)}</summary>\n`
} else {
return `<details ${partClass}>\n`
}
} else {
// closing tag
return '</details>\n'