Skip to content

Commit f43f092

Browse files
committed
fix(NcRichText): migrate from 'remark-stringify' library with 'mdast-util-to-string'
- 'remark-stringify' returns serialized markdown, escaping its syntax, e.g. underscores. NcReferenceList needs only text content Signed-off-by: Maksim Sukharev <antreesy.web@gmail.com>
1 parent 6e41378 commit f43f092

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

src/components/NcRichText/NcRichText.vue

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,13 +306,13 @@ See [NcRichContenteditable](#/Components/NcRichContenteditable) documentation fo
306306
</docs>
307307

308308
<script>
309+
import { toString } from 'mdast-util-to-string'
309310
import rehypeExternalLinks from 'rehype-external-links'
310311
import rehype2react from 'rehype-react'
311312
import breaks from 'remark-breaks'
312313
import remarkGfm from 'remark-gfm'
313314
import remarkParse from 'remark-parse'
314315
import remark2rehype from 'remark-rehype'
315-
import remarkStringify from 'remark-stringify'
316316
import remarkUnlinkProtocols from 'remark-unlink-protocols'
317317
import { unified } from 'unified'
318318
import { ref } from 'vue'
@@ -627,10 +627,12 @@ export default {
627627
return text
628628
}
629629
630-
return unified()
630+
const processor = unified()
631+
processor.compiler = (tree) => toString(tree)
632+
633+
return processor
631634
.use(remarkParse)
632635
.use(remarkStripCode)
633-
.use(remarkStringify)
634636
.processSync(text)
635637
.value
636638
},

tests/unit/components/NcRichText/NcRichText.spec.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,20 @@
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
55

6-
import { describe, expect, it, test } from '@jest/globals'
6+
import { describe, expect, it, jest, test } from '@jest/globals'
7+
import axios from '@nextcloud/axios'
78
import { mount } from '@vue/test-utils'
89
import { nextTick } from 'vue'
10+
import NcReferenceList from '../../../../src/components/NcRichText/NcReferenceList.vue'
911
import NcRichText from '../../../../src/components/NcRichText/NcRichText.vue'
1012

13+
jest.mock('@nextcloud/axios', () => ({
14+
__esModule: true,
15+
default: {
16+
get: jest.fn().mockResolvedValue({ data: { ocs: { data: { references: {} } } } }),
17+
},
18+
}))
19+
1120
describe('NcRichText', () => {
1221
it('renders a message and responds correctly to props changes', async () => {
1322
const wrapper = mount(NcRichText, {
@@ -224,4 +233,18 @@ describe('NcRichText', () => {
224233
await nextTick()
225234
expect(wrapper.find('code').classes()).toEqual(['hljs', 'language-js'])
226235
})
236+
237+
it('strips URL links from text source if wrapped in inline/code blocks Markdown', () => {
238+
const testUrl = 'https://example.com/a_b'
239+
const wrapper = mount(NcRichText, {
240+
propsData: {
241+
text: 'Inline `https://example.com/inline_b`\n```\nStripped text https://example.com/fenced_b\n```\n\nPlain ' + testUrl,
242+
useMarkdown: true,
243+
referenceLimit: 1,
244+
},
245+
})
246+
247+
expect(wrapper.findComponent(NcReferenceList).props('text').trim()).toBe('Inline Plain https://example.com/a_b')
248+
expect(axios.get).toHaveBeenCalledWith(expect.stringContaining(encodeURIComponent(testUrl)))
249+
})
227250
})

0 commit comments

Comments
 (0)