Skip to content

Commit

Permalink
fix: extract un-escaping of text/code nodes
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Sukharev <antreesy.web@gmail.com>

[skip ci]
  • Loading branch information
Antreesy authored and backportbot[bot] committed Feb 7, 2025
1 parent 7033776 commit d75459d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/components/NcRichText/NcRichText.vue
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ import NcCheckboxRadioSwitch from '../NcCheckboxRadioSwitch/NcCheckboxRadioSwitc
import NcLoadingIcon from '../NcLoadingIcon/NcLoadingIcon.vue'
import { getRoute, remarkAutolink } from './autolink.ts'
import { remarkPlaceholder, prepareTextNode } from './placeholder.js'
import { remarkUnescape } from './remarkUnescape.js'
import GenRandomId from '../../utils/GenRandomId.js'

import { unified } from 'unified'
Expand Down Expand Up @@ -456,6 +457,7 @@ export default {
useMarkdown: this.useMarkdown,
useExtendedMarkdown: this.useExtendedMarkdown,
})
.use(remarkUnescape)
.use(this.useExtendedMarkdown ? remarkGfm.value : undefined)
.use(breaks)
.use(remark2rehype, {
Expand All @@ -480,9 +482,7 @@ export default {
})
.processSync(this.text
// escape special symbol "<" to not treat text as HTML
.replace(/</gmi, '&lt;')
// unescape special symbol ">" to parse blockquotes
.replace(/&gt;/gmi, '>'),
.replace(/<[^>]+>/g, (match) => match.replace(/</g, '&lt;')),
)
.result

Expand Down
19 changes: 19 additions & 0 deletions src/components/NcRichText/remarkUnescape.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { visit, SKIP } from 'unist-util-visit'

export const remarkUnescape = function() {
return function(tree) {
visit(tree, (node) => node.type === 'text' || node.type === 'code',
(node, index, parent) => {
parent.children.splice(index, 1, {
...node,
value: node.value.replace(/&lt;/gmi, '<').replace(/&gt;/gmi, '>'),
})
return [SKIP, index + 1]
})
}
}

0 comments on commit d75459d

Please sign in to comment.