Skip to content

Commit 16f1b7a

Browse files
committed
fix(NcRichText)!: do not render non-resolved relative links
- still works: external links, internal absolute links, and internal relative links, that do have a router match for current app - internal relative links without match (also ?query and #anchor) only render an inner text - allow tel: and mail: to reduce breaking changes Signed-off-by: Maksim Sukharev <antreesy.web@gmail.com>
1 parent 77042fb commit 16f1b7a

2 files changed

Lines changed: 735 additions & 0 deletions

File tree

src/components/NcRichText/NcRichText.vue

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ import { RouterLink } from 'vue-router'
320320
import NcCheckboxRadioSwitch from '../NcCheckboxRadioSwitch/NcCheckboxRadioSwitch.vue'
321321
import NcReferenceList from './NcReferenceList.vue'
322322
import NcRichTextCopyButton from './NcRichTextCopyButton.vue'
323+
import NcRichTextExternalLink from './NcRichTextExternalLink.vue'
323324
import GenRandomId from '../../utils/GenRandomId.js'
324325
import { getRoute, remarkAutolink } from './autolink.js'
325326
import { prepareTextNode, remarkPlaceholder } from './placeholder.js'
@@ -556,6 +557,7 @@ export default {
556557
if (tag === 'a') {
557558
const route = getRoute(this.$router, attrs.attrs.href)
558559
if (route) {
560+
// Resolved link to this app; render RouterLink
559561
delete attrs.attrs.href
560562
delete attrs.attrs.target
561563
@@ -566,6 +568,18 @@ export default {
566568
},
567569
}, children)
568570
}
571+
572+
const isAllowedScheme = /^(https?:\/\/|tel:|mailto:)/.test(attrs.attrs.href)
573+
if (isAllowedScheme) {
574+
// External link; render normally, open in the new tab
575+
attrs.attrs.href = attrs.attrs.href.trim()
576+
return h(NcRichTextExternalLink, attrs, children)
577+
} else {
578+
// Unresolved relative link that does not belong to this app; render only children
579+
delete attrs.attrs.href
580+
delete attrs.attrs.target
581+
return h('span', attrs, children)
582+
}
569583
}
570584
571585
return h(tag, attrs, children)

0 commit comments

Comments
 (0)