Skip to content

Commit 8e14d63

Browse files
authored
Merge pull request #7202 from nextcloud/fix/details_newline
fix(details): Accept details open tag and summary tag in same line
2 parents e8eef56 + 1097b70 commit 8e14d63

2 files changed

Lines changed: 17 additions & 8 deletions

File tree

src/markdownit/details.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import type StateBlock from 'markdown-it/lib/rules_block/state_block.mjs'
88
import type Token from 'markdown-it/lib/token.mjs'
99

1010
const DETAILS_START_REGEX = /^<details>\s*$/
11+
const DETAILS_AND_SUMMARY_START_REGEX =
12+
/(?<=^<details>\s*<summary>).*(?=<\/summary>\s*$)/
1113
const DETAILS_END_REGEX = /^<\/details>\s*$/
1214
const SUMMARY_REGEX = /(?<=^<summary>).*(?=<\/summary>\s*$)/
1315

@@ -28,8 +30,17 @@ function parseDetails(
2830
let start = state.bMarks[startLine] + state.tShift[startLine]
2931
let max = state.eMarks[startLine]
3032

31-
// Details block start
32-
if (!state.src.slice(start, max).match(DETAILS_START_REGEX)) {
33+
let detailsFound = false
34+
let detailsSummary = null
35+
let startLineCount = 2
36+
37+
const m = state.src.slice(start, max).match(DETAILS_AND_SUMMARY_START_REGEX)
38+
if (m) {
39+
// Details block start and summary in same line
40+
detailsSummary = m[0].trim()
41+
startLineCount = 1
42+
} else if (!state.src.slice(start, max).match(DETAILS_START_REGEX)) {
43+
// Details block start in separate line
3344
return false
3445
}
3546

@@ -38,8 +49,6 @@ function parseDetails(
3849
return true
3950
}
4051

41-
let detailsFound = false
42-
let detailsSummary = null
4352
let nestedCount = 0
4453
let nextLine = startLine
4554
for (;;) {
@@ -112,7 +121,7 @@ function parseDetails(
112121

113122
token = state.push('details_summary', 'summary', -1)
114123

115-
state.md.block.tokenize(state, startLine + 2, nextLine)
124+
state.md.block.tokenize(state, startLine + startLineCount, nextLine)
116125

117126
token = state.push('details_close', 'details', -1)
118127
token.block = true

src/tests/markdownit/details.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('Details extension', () => {
2626
)
2727
})
2828
it('renders with spaces', () => {
29-
const rendered = markdownit.render(' <details> \n <summary>summary </summary> \n content \n </details>')
29+
const rendered = markdownit.render(' <details> \n <summary> summary </summary> \n content \n </details> ')
3030
expect(stripIndent(rendered)).toBe(
3131
'<details><summary>summary</summary><p>content</p></details>',
3232
)
@@ -55,10 +55,10 @@ describe('Details extension', () => {
5555
'<details><summary>summary</summary><details><summary>nested summary</summary><p>nested content</p></details><p>content</p></details>',
5656
)
5757
})
58-
it('does not render with missing linebreak after details open', () => {
58+
it('renders without linebreak after details open', () => {
5959
const rendered = markdownit.render('<details><summary>summary</summary>\ncontent\n</details>')
6060
expect(stripIndent(rendered)).toBe(
61-
'<p>&lt;details&gt;&lt;summary&gt;summary&lt;/summary&gt;content&lt;/details&gt;</p>',
61+
'<details><summary>summary</summary><p>content</p></details>',
6262
)
6363
})
6464
it('does not render with missing linebreak after summary', () => {

0 commit comments

Comments
 (0)