33 * SPDX-License-Identifier: AGPL-3.0-or-later
44 */
55
6+ import type { Node , Parent } from 'unist'
67import type { Router } from 'vue-router'
78
89import { getBaseUrl , getRootUrl } from '@nextcloud/router'
910import { u } from 'unist-builder'
10- import { SKIP , visit } from 'unist-util-visit'
11+ import { SKIP , visitParents } from 'unist-util-visit-parents '
1112import { defineComponent , h } from 'vue'
1213import { logger } from '../../utils/logger.ts'
1314import { URL_PATTERN_AUTOLINK } from './helpers.js'
@@ -45,13 +46,19 @@ export function remarkAutolink({ autolink, useMarkdown, useExtendedMarkdown }) {
4546 return
4647 }
4748
48- visit ( tree , ( node ) => node . type === 'text' , ( node , index , parent ) => {
49- let parsed = parseUrl ( node . value )
50- if ( typeof parsed === 'string' ) {
51- parsed = [ u ( 'text' , parsed ) ]
52- } else {
53- parsed = parsed
54- . map ( ( n ) => {
49+ visitParents ( tree , ( node ) => node . type === 'text' , ( node , ancestors : Parent [ ] ) => {
50+ // Do not autolink text already inside a link node
51+ if ( ancestors . some ( ( ancestor ) => ancestor . type === 'link' || ancestor . type === 'linkReference' ) ) {
52+ return
53+ }
54+
55+ const parent = ancestors . at ( - 1 )
56+ const index = parent ! . children . indexOf ( node ) ?? 0
57+
58+ const parsed = parseUrl ( node . value )
59+ const parsedNodes : Node [ ] = ( typeof parsed === 'string' )
60+ ? [ u ( 'text' , parsed ) ]
61+ : parsed . map ( ( n ) => {
5562 if ( typeof n === 'string' ) {
5663 return u ( 'text' , n )
5764 }
@@ -60,12 +67,11 @@ export function remarkAutolink({ autolink, useMarkdown, useExtendedMarkdown }) {
6067 url : n . props . href ,
6168 } , [ u ( 'text' , n . props . href ) ] )
6269 } )
63- . filter ( ( x ) => x )
64- . flat ( )
65- }
70+ . filter ( ( x ) => x )
71+ . flat ( )
6672
67- parent . children . splice ( index , 1 , ...parsed )
68- return [ SKIP , ( index ?? 0 ) + parsed . length ]
73+ parent ! . children . splice ( index , 1 , ...parsedNodes )
74+ return [ SKIP , index + parsedNodes . length ]
6975 } )
7076 }
7177}
0 commit comments