@@ -76,6 +76,43 @@ function indentCodeCompensation(raw: string, text: string, rules: Rules) {
7676 . join ( '\n' ) ;
7777}
7878
79+ function isLabelEndInsideToken ( src : string , label : string , labelStart : number , rules : Rules ) {
80+ if ( ! label . includes ( '<' ) ) {
81+ return false ;
82+ }
83+
84+ for ( let i = 0 ; i < label . length ; i ++ ) {
85+ if ( label [ i ] === '\\' ) {
86+ i ++ ;
87+ continue ;
88+ }
89+
90+ if ( label [ i ] === '`' ) {
91+ const code = rules . inline . code . exec ( label . slice ( i ) ) ;
92+ if ( code ) {
93+ i += code [ 0 ] . length - 1 ;
94+ continue ;
95+ }
96+ }
97+
98+ if ( label [ i ] !== '<' ) {
99+ continue ;
100+ }
101+
102+ const tokenSrc = src . slice ( labelStart + i ) ;
103+ const token = rules . inline . tag . exec ( tokenSrc ) || rules . inline . autolink . exec ( tokenSrc ) ;
104+ if ( ! token ) {
105+ continue ;
106+ }
107+
108+ if ( token [ 0 ] . length > label . length - i ) {
109+ return true ;
110+ }
111+ i += token [ 0 ] . length - 1 ;
112+ }
113+ return false ;
114+ }
115+
79116/**
80117 * Tokenizer
81118 */
@@ -684,6 +721,11 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
684721 link ( src : string ) : Tokens . Link | Tokens . Image | undefined {
685722 const cap = this . rules . inline . link . exec ( src ) ;
686723 if ( cap ) {
724+ const labelStart = cap [ 0 ] . charAt ( 0 ) === '!' ? 2 : 1 ;
725+ if ( ! this . options . pedantic && isLabelEndInsideToken ( src , cap [ 1 ] , labelStart , this . rules ) ) {
726+ return ;
727+ }
728+
687729 const trimmedUrl = cap [ 2 ] . trim ( ) ;
688730 if ( ! this . options . pedantic && this . rules . other . startAngleBracket . test ( trimmedUrl ) ) {
689731 // commonmark requires matching angle brackets
@@ -746,6 +788,11 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
746788 let cap ;
747789 if ( ( cap = this . rules . inline . reflink . exec ( src ) )
748790 || ( cap = this . rules . inline . nolink . exec ( src ) ) ) {
791+ const labelStart = cap [ 0 ] . charAt ( 0 ) === '!' ? 2 : 1 ;
792+ if ( ! this . options . pedantic && isLabelEndInsideToken ( src , cap [ 1 ] , labelStart , this . rules ) ) {
793+ return ;
794+ }
795+
749796 const linkString = ( cap [ 2 ] || cap [ 1 ] ) . replace ( this . rules . other . multipleSpaceGlobal , ' ' ) ;
750797 const link = links [ normalizeLabel ( linkString ) ] ;
751798 if ( ! link ) {
0 commit comments