diff --git a/src/markdownit/details.ts b/src/markdownit/details.ts index 9c5ac1985bc..cc852ed82ce 100644 --- a/src/markdownit/details.ts +++ b/src/markdownit/details.ts @@ -8,6 +8,8 @@ import type StateBlock from 'markdown-it/lib/rules_block/state_block.mjs' import type Token from 'markdown-it/lib/token.mjs' const DETAILS_START_REGEX = /^
\s*$/ +const DETAILS_AND_SUMMARY_START_REGEX = + /(?<=^
\s*).*(?=<\/summary>\s*$)/ const DETAILS_END_REGEX = /^<\/details>\s*$/ const SUMMARY_REGEX = /(?<=^).*(?=<\/summary>\s*$)/ @@ -28,8 +30,17 @@ function parseDetails( let start = state.bMarks[startLine] + state.tShift[startLine] let max = state.eMarks[startLine] - // Details block start - if (!state.src.slice(start, max).match(DETAILS_START_REGEX)) { + let detailsFound = false + let detailsSummary = null + let startLineCount = 2 + + const m = state.src.slice(start, max).match(DETAILS_AND_SUMMARY_START_REGEX) + if (m) { + // Details block start and summary in same line + detailsSummary = m[0].trim() + startLineCount = 1 + } else if (!state.src.slice(start, max).match(DETAILS_START_REGEX)) { + // Details block start in separate line return false } @@ -38,8 +49,6 @@ function parseDetails( return true } - let detailsFound = false - let detailsSummary = null let nestedCount = 0 let nextLine = startLine for (;;) { @@ -112,7 +121,7 @@ function parseDetails( token = state.push('details_summary', 'summary', -1) - state.md.block.tokenize(state, startLine + 2, nextLine) + state.md.block.tokenize(state, startLine + startLineCount, nextLine) token = state.push('details_close', 'details', -1) token.block = true diff --git a/src/tests/markdownit/details.spec.js b/src/tests/markdownit/details.spec.js index e5755f7efec..1925f9025b5 100644 --- a/src/tests/markdownit/details.spec.js +++ b/src/tests/markdownit/details.spec.js @@ -26,7 +26,7 @@ describe('Details extension', () => { ) }) it('renders with spaces', () => { - const rendered = markdownit.render('
\n summary \n content \n
') + const rendered = markdownit.render('
\n summary \n content \n
') expect(stripIndent(rendered)).toBe( '
summary

content

', ) @@ -55,10 +55,10 @@ describe('Details extension', () => { '
summary
nested summary

nested content

content

', ) }) - it('does not render with missing linebreak after details open', () => { + it('renders without linebreak after details open', () => { const rendered = markdownit.render('
summary\ncontent\n
') expect(stripIndent(rendered)).toBe( - '

<details><summary>summary</summary>content</details>

', + '
summary

content

', ) }) it('does not render with missing linebreak after summary', () => {