-
Notifications
You must be signed in to change notification settings - Fork 115
feat(editor): Add support for collapsible sections #6251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /** | ||
| * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| import { initUserAndFiles, randUser } from '../../utils/index.js' | ||
|
|
||
| const user = randUser() | ||
| const fileName = 'empty.md' | ||
|
|
||
| describe('Details plugin', () => { | ||
| before(() => { | ||
| initUserAndFiles(user) | ||
| }) | ||
|
|
||
| beforeEach(() => { | ||
| cy.login(user) | ||
|
|
||
| cy.isolateTest({ | ||
| sourceFile: fileName, | ||
| }) | ||
|
|
||
| return cy.openFile(fileName, { force: true }) | ||
| }) | ||
|
|
||
| it('inserts and removes details', () => { | ||
| cy.getContent() | ||
| .type('content{selectAll}') | ||
|
|
||
| cy.getMenuEntry('details').click() | ||
|
|
||
| cy.getContent() | ||
| .find('div[data-text-el="details"]') | ||
| .should('exist') | ||
|
|
||
| cy.getContent() | ||
| .type('summary') | ||
|
|
||
| cy.getContent() | ||
| .find('div[data-text-el="details"]') | ||
| .find('summary') | ||
| .should('contain', 'summary') | ||
|
|
||
| cy.getContent() | ||
| .find('div[data-text-el="details"]') | ||
| .find('.details-content') | ||
| .should('contain', 'content') | ||
|
|
||
| cy.getMenuEntry('details').click() | ||
|
|
||
| cy.getContent() | ||
| .find('div[data-text-el="details"]') | ||
| .should('not.exist') | ||
|
|
||
| cy.getContent() | ||
| .should('contain', 'content') | ||
| }) | ||
| }) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| /** | ||
| * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| import type MarkdownIt from 'markdown-it' | ||
| import type StateBlock from 'markdown-it/lib/rules_block/state_block' | ||
| import type Token from 'markdown-it/lib/token' | ||
|
|
||
| const DETAILS_START_REGEX = /^<details>\s*$/ | ||
| const DETAILS_END_REGEX = /^<\/details>\s*$/ | ||
| const SUMMARY_REGEX = /(?<=^<summary>).*(?=<\/summary>\s*$)/ | ||
|
|
||
| function parseDetails(state: StateBlock, startLine: number, endLine: number, silent: boolean) { | ||
| // let autoClosedBlock = false | ||
| 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)) { | ||
| return false | ||
| } | ||
|
|
||
| // Since start is found, we can report success here in validation mode | ||
| if (silent) { | ||
| return true | ||
| } | ||
|
|
||
| let detailsFound = false | ||
| let detailsSummary = null | ||
| let nestedCount = 0 | ||
| let nextLine = startLine | ||
| for (;;) { | ||
| nextLine++ | ||
| if (nextLine >= endLine) { | ||
| break | ||
| } | ||
|
|
||
| start = state.bMarks[nextLine] + state.tShift[nextLine] | ||
| max = state.eMarks[nextLine] | ||
|
|
||
| // Details summary | ||
| const m = state.src.slice(start, max).match(SUMMARY_REGEX) | ||
| if (m && detailsSummary === null) { | ||
| // Only set `detailsSummary` the first time | ||
| // Ignore future summary tags (in nested/broken details) | ||
| detailsSummary = m[0].trim() | ||
| continue | ||
| } | ||
|
|
||
| // Nested details | ||
| if (state.src.slice(start, max).match(DETAILS_START_REGEX)) { | ||
| nestedCount++ | ||
| } | ||
|
|
||
| // Details block end | ||
| if (!state.src.slice(start, max).match(DETAILS_END_REGEX)) { | ||
| continue | ||
| } | ||
|
|
||
| // Regard nested details blocks | ||
| if (nestedCount > 0) { | ||
| nestedCount-- | ||
| } else { | ||
| detailsFound = true | ||
| break | ||
| } | ||
| } | ||
|
|
||
| if (!detailsFound || detailsSummary === null) { | ||
| return false | ||
| } | ||
|
|
||
| const oldParent = state.parentType | ||
| const oldLineMax = state.lineMax | ||
| state.parentType = 'reference' | ||
|
|
||
| // This will prevent lazy continuations from ever going past our end marker | ||
| state.lineMax = nextLine; | ||
|
|
||
| // Push tokens to the state | ||
|
|
||
| let token = state.push('details_open', 'details', 1) | ||
| token.block = true | ||
| token.info = detailsSummary | ||
| token.map = [ startLine, nextLine ] | ||
|
|
||
| token = state.push('details_summary', 'summary', 1) | ||
| token.block = false | ||
|
|
||
| // Parse and push summary to preserve markup | ||
| let tokens: Token[] = [] | ||
| state.md.inline.parse(detailsSummary, state.md, state.env, tokens) | ||
| for (const t of tokens) { | ||
| token = state.push(t.type, t.tag, t.nesting) | ||
| token.block = t.block | ||
| token.markup = t.markup | ||
| token.content = t.content | ||
| } | ||
|
|
||
| token = state.push('details_summary', 'summary', -1) | ||
|
|
||
| state.md.block.tokenize(state, startLine + 2, nextLine); | ||
|
|
||
| token = state.push('details_close', 'details', -1) | ||
| token.block = true | ||
|
|
||
| state.parentType = oldParent | ||
| state.lineMax = oldLineMax | ||
| state.line = nextLine + 1 | ||
|
|
||
| return true | ||
| } | ||
|
|
||
| /** | ||
| * @param {object} md Markdown object | ||
| */ | ||
| export default function details(md: MarkdownIt) { | ||
| md.block.ruler.before('fence', 'details', parseDetails, { | ||
| alt: [ 'paragraph', 'reference', 'blockquote', 'list' ], | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jancborchardt Keeping #2836 into consideration should we take action for this one right now and instead of adding this as yet another menu bar entry try to group them in the callout menu as listed in the issue?
In the end the details is just another "block style"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though one argument against would be that you could still have a callout nested in a details block which is then not very obvious from the active indication in the menu bar
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, good point, makes sense to directly group it properly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, but then we probably should move block quote and code blocks into this submenu as well straight away, right? What icon should be used for the "block style" submenu?