|
| 1 | +/** |
| 2 | + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import type MarkdownIt from 'markdown-it' |
| 7 | +import type StateBlock from 'markdown-it/lib/rules_block/state_block' |
| 8 | +import type Token from 'markdown-it/lib/token' |
| 9 | + |
| 10 | +const DETAILS_START_REGEX = /^<details>\s*$/ |
| 11 | +const DETAILS_END_REGEX = /^<\/details>\s*$/ |
| 12 | +const SUMMARY_REGEX = /(?<=^<summary>).*(?=<\/summary>\s*$)/ |
| 13 | + |
| 14 | +function parseDetails(state: StateBlock, startLine: number, endLine: number, silent: boolean) { |
| 15 | + // let autoClosedBlock = false |
| 16 | + let start = state.bMarks[startLine] + state.tShift[startLine] |
| 17 | + let max = state.eMarks[startLine] |
| 18 | + |
| 19 | + // Details block start |
| 20 | + if (!state.src.slice(start, max).match(DETAILS_START_REGEX)) { |
| 21 | + return false |
| 22 | + } |
| 23 | + |
| 24 | + // Since start is found, we can report success here in validation mode |
| 25 | + if (silent) { |
| 26 | + return true |
| 27 | + } |
| 28 | + |
| 29 | + let detailsFound = false |
| 30 | + let detailsSummary = null |
| 31 | + let nestedCount = 0 |
| 32 | + let nextLine = startLine |
| 33 | + for (;;) { |
| 34 | + nextLine++ |
| 35 | + if (nextLine >= endLine) { |
| 36 | + break |
| 37 | + } |
| 38 | + |
| 39 | + start = state.bMarks[nextLine] + state.tShift[nextLine] |
| 40 | + max = state.eMarks[nextLine] |
| 41 | + |
| 42 | + // Details summary |
| 43 | + const m = state.src.slice(start, max).match(SUMMARY_REGEX) |
| 44 | + if (m && detailsSummary === null) { |
| 45 | + // Only set `detailsSummary` the first time |
| 46 | + // Ignore future summary tags (in nested/broken details) |
| 47 | + detailsSummary = m[0].trim() |
| 48 | + continue |
| 49 | + } |
| 50 | + |
| 51 | + // Nested details |
| 52 | + if (state.src.slice(start, max).match(DETAILS_START_REGEX)) { |
| 53 | + nestedCount++ |
| 54 | + } |
| 55 | + |
| 56 | + // Details block end |
| 57 | + if (!state.src.slice(start, max).match(DETAILS_END_REGEX)) { |
| 58 | + continue |
| 59 | + } |
| 60 | + |
| 61 | + // Regard nested details blocks |
| 62 | + if (nestedCount > 0) { |
| 63 | + nestedCount-- |
| 64 | + } else { |
| 65 | + detailsFound = true |
| 66 | + break |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if (!detailsFound || detailsSummary === null) { |
| 71 | + return false |
| 72 | + } |
| 73 | + |
| 74 | + const oldParent = state.parentType |
| 75 | + const oldLineMax = state.lineMax |
| 76 | + state.parentType = 'reference' |
| 77 | + |
| 78 | + // This will prevent lazy continuations from ever going past our end marker |
| 79 | + state.lineMax = nextLine; |
| 80 | + |
| 81 | + // Push tokens to the state |
| 82 | + |
| 83 | + let token = state.push('details_open', 'details', 1) |
| 84 | + token.block = true |
| 85 | + token.info = detailsSummary |
| 86 | + token.map = [ startLine, nextLine ] |
| 87 | + |
| 88 | + token = state.push('details_summary', 'summary', 1) |
| 89 | + token.block = false |
| 90 | + |
| 91 | + // Parse and push summary to preserve markup |
| 92 | + let tokens: Token[] = [] |
| 93 | + state.md.inline.parse(detailsSummary, state.md, state.env, tokens) |
| 94 | + for (const t of tokens) { |
| 95 | + token = state.push(t.type, t.tag, t.nesting) |
| 96 | + token.block = t.block |
| 97 | + token.markup = t.markup |
| 98 | + token.content = t.content |
| 99 | + } |
| 100 | + |
| 101 | + token = state.push('details_summary', 'summary', -1) |
| 102 | + |
| 103 | + state.md.block.tokenize(state, startLine + 2, nextLine); |
| 104 | + |
| 105 | + token = state.push('details_close', 'details', -1) |
| 106 | + token.block = true |
| 107 | + |
| 108 | + state.parentType = oldParent |
| 109 | + state.lineMax = oldLineMax |
| 110 | + state.line = nextLine + 1 |
| 111 | + |
| 112 | + return true |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * @param {object} md Markdown object |
| 117 | + */ |
| 118 | +export default function details(md: MarkdownIt) { |
| 119 | + md.block.ruler.before('fence', 'details', parseDetails, { |
| 120 | + alt: [ 'paragraph', 'reference', 'blockquote', 'list' ], |
| 121 | + }) |
| 122 | +} |
0 commit comments