@@ -168,19 +168,74 @@ const inline = {
168168 link : / ^ ! ? \[ ( l a b e l ) \] \( \s * ( h r e f ) (?: \s + ( t i t l e ) ) ? \s * \) / ,
169169 reflink : / ^ ! ? \[ ( l a b e l ) \] \[ (? ! \s * \] ) ( (?: \\ [ \[ \] ] ? | [ ^ \[ \] \\ ] ) + ) \] / ,
170170 nolink : / ^ ! ? \[ (? ! \s * \] ) ( (?: \[ [ ^ \[ \] ] * \] | \\ [ \[ \] ] | [ ^ \[ \] ] ) * ) \] (?: \[ \] ) ? / ,
171- strong : / ^ _ _ ( [ ^ \s _ ] ) _ _ (? ! _ ) | ^ \* \* ( [ ^ \s * ] ) \* \* (? ! \* ) | ^ _ _ ( [ ^ \s ] [ \s \S ] * ?[ ^ \s ] ) _ _ (? ! _ ) | ^ \* \* ( [ ^ \s ] [ \s \S ] * ?[ ^ \s ] ) \* \* (? ! \* ) / ,
172- em : / ^ _ ( [ ^ \s _ ] ) _ (? ! _ ) | ^ _ ( [ ^ \s _ < ] [ \s \S ] * ?[ ^ \s _ ] ) _ (? ! _ | [ ^ \s , p u n c t u a t i o n ] ) | ^ _ ( [ ^ \s _ < ] [ \s \S ] * ?[ ^ \s ] ) _ (? ! _ | [ ^ \s , p u n c t u a t i o n ] ) | ^ \* ( [ ^ \s * < \[ ] ) \* (? ! \* ) | ^ \* ( [ ^ \s < " ] [ \s \S ] * ?[ ^ \s \[ \* ] ) \* (? ! [ \] ` p u n c t u a t i o n ] ) | ^ \* ( [ ^ \s * " < \[ ] [ \s \S ] * [ ^ \s ] ) \* (? ! \* ) / ,
171+ reflinkSearch : 'reflink|nolink(?!\\()' ,
172+ strong : {
173+ start : / ^ (?: ( \* \* (? = [ * p u n c t u a t i o n ] ) ) | \* \* ) (? ! [ \s ] ) | _ _ / , // (1) returns if starts w/ punctuation
174+ middle : / ^ \* \* (?: (?: (? ! o v e r l a p S k i p ) (?: [ ^ * ] | \\ \* ) | o v e r l a p S k i p ) | \* (?: (? ! o v e r l a p S k i p ) (?: [ ^ * ] | \\ \* ) | o v e r l a p S k i p ) * ?\* ) + ?\* \* $ | ^ _ _ (? ! [ \s ] ) ( (?: (?: (? ! o v e r l a p S k i p ) (?: [ ^ _ ] | \\ _ ) | o v e r l a p S k i p ) | _ (?: (? ! o v e r l a p S k i p ) (?: [ ^ _ ] | \\ _ ) | o v e r l a p S k i p ) * ?_ ) + ?) _ _ $ / ,
175+ endAst : / [ ^ p u n c t u a t i o n \s ] \* \* (? ! \* ) | [ p u n c t u a t i o n ] \* \* (? ! \* ) (?: (? = [ p u n c t u a t i o n \s ] | $ ) ) / , // last char can't be punct, or final * must also be followed by punct (or endline)
176+ endUnd : / [ ^ \s ] _ _ (? ! _ ) (?: (? = [ p u n c t u a t i o n \s ] ) | $ ) / // last char can't be a space, and final _ must preceed punct or \s (or endline)
177+ } ,
178+ em : {
179+ start : / ^ (?: ( \* (? = [ p u n c t u a t i o n ] ) ) | \* ) (? ! [ * \s ] ) | _ / , // (1) returns if starts w/ punctuation
180+ middle : / ^ \* (?: (?: (? ! o v e r l a p S k i p ) (?: [ ^ * ] | \\ \* ) | o v e r l a p S k i p ) | \* (?: (? ! o v e r l a p S k i p ) (?: [ ^ * ] | \\ \* ) | o v e r l a p S k i p ) * ?\* ) + ?\* $ | ^ _ (? ! [ _ \s ] ) (?: (?: (? ! o v e r l a p S k i p ) (?: [ ^ _ ] | \\ _ ) | o v e r l a p S k i p ) | _ (?: (? ! o v e r l a p S k i p ) (?: [ ^ _ ] | \\ _ ) | o v e r l a p S k i p ) * ?_ ) + ?_ $ / ,
181+ endAst : / [ ^ p u n c t u a t i o n \s ] \* (? ! \* ) | [ p u n c t u a t i o n ] \* (? ! \* ) (?: (? = [ p u n c t u a t i o n \s ] | $ ) ) / , // last char can't be punct, or final * must also be followed by punct (or endline)
182+ endUnd : / [ ^ \s ] _ (? ! _ ) (?: (? = [ p u n c t u a t i o n \s ] ) | $ ) / // last char can't be a space, and final _ must preceed punct or \s (or endline)
183+ } ,
173184 code : / ^ ( ` + ) ( [ ^ ` ] | [ ^ ` ] [ \s \S ] * ?[ ^ ` ] ) \1(? ! ` ) / ,
174185 br : / ^ ( { 2 , } | \\ ) \n (? ! \s * $ ) / ,
175186 del : noopTest ,
176- text : / ^ ( ` + | [ ^ ` ] ) (?: [ \s \S ] * ?(?: (? = [ \\ < ! \[ ` * ] | \b _ | $ ) | [ ^ ] (? = { 2 , } \n ) ) | (? = { 2 , } \n ) ) /
187+ text : / ^ ( ` + | [ ^ ` ] ) (?: [ \s \S ] * ?(?: (? = [ \\ < ! \[ ` * ] | \b _ | $ ) | [ ^ ] (? = { 2 , } \n ) ) | (? = { 2 , } \n ) ) / ,
188+ punctuation : / ^ ( [ \s * p u n c t u a t i o n ] ) /
177189} ;
178190
179191// list of punctuation marks from common mark spec
180- // without ` and ] to workaround Rule 17 (inline code blocks/links)
181- // without , to work around example 393
182- inline . _punctuation = '!"#$%&\'()*+\\-./:;<=>?@\\[^_{|}~' ;
183- inline . em = edit ( inline . em ) . replace ( / p u n c t u a t i o n / g, inline . _punctuation ) . getRegex ( ) ;
192+ // without * and _ to workaround cases with double emphasis
193+ inline . _punctuation = '!"#$%&\'()+\\-.,/:;<=>?@\\[\\]`^{|}~' ;
194+ inline . punctuation = edit ( inline . punctuation ) . replace ( / p u n c t u a t i o n / g, inline . _punctuation ) . getRegex ( ) ;
195+
196+ // sequences em should skip over [title](link), `code`, <html>
197+ inline . _blockSkip = '\\[[^\\]]*?\\]\\([^\\)]*?\\)|`[^`]*?`|<[^>]*?>' ;
198+ inline . _overlapSkip = '__[^_]*?__|\\*\\*\\[^\\*\\]*?\\*\\*' ;
199+
200+ inline . em . start = edit ( inline . em . start )
201+ . replace ( / p u n c t u a t i o n / g, inline . _punctuation )
202+ . getRegex ( ) ;
203+
204+ inline . em . middle = edit ( inline . em . middle )
205+ . replace ( / p u n c t u a t i o n / g, inline . _punctuation )
206+ . replace ( / o v e r l a p S k i p / g, inline . _overlapSkip )
207+ . getRegex ( ) ;
208+
209+ inline . em . endAst = edit ( inline . em . endAst , 'g' )
210+ . replace ( / p u n c t u a t i o n / g, inline . _punctuation )
211+ . getRegex ( ) ;
212+
213+ inline . em . endUnd = edit ( inline . em . endUnd , 'g' )
214+ . replace ( / p u n c t u a t i o n / g, inline . _punctuation )
215+ . getRegex ( ) ;
216+
217+ inline . strong . start = edit ( inline . strong . start )
218+ . replace ( / p u n c t u a t i o n / g, inline . _punctuation )
219+ . getRegex ( ) ;
220+
221+ inline . strong . middle = edit ( inline . strong . middle )
222+ . replace ( / p u n c t u a t i o n / g, inline . _punctuation )
223+ . replace ( / b l o c k S k i p / g, inline . _blockSkip )
224+ . getRegex ( ) ;
225+
226+ inline . strong . endAst = edit ( inline . strong . endAst , 'g' )
227+ . replace ( / p u n c t u a t i o n / g, inline . _punctuation )
228+ . getRegex ( ) ;
229+
230+ inline . strong . endUnd = edit ( inline . strong . endUnd , 'g' )
231+ . replace ( / p u n c t u a t i o n / g, inline . _punctuation )
232+ . getRegex ( ) ;
233+
234+ inline . blockSkip = edit ( inline . _blockSkip , 'g' )
235+ . getRegex ( ) ;
236+
237+ inline . overlapSkip = edit ( inline . _overlapSkip , 'g' )
238+ . getRegex ( ) ;
184239
185240inline . _escapes = / \\ ( [ ! " # $ % & ' ( ) * + , \- . / : ; < = > ? @ \[ \] \\ ^ _ ` { | } ~ ] ) / g;
186241
@@ -212,6 +267,11 @@ inline.reflink = edit(inline.reflink)
212267 . replace ( 'label' , inline . _label )
213268 . getRegex ( ) ;
214269
270+ inline . reflinkSearch = edit ( inline . reflinkSearch , 'g' )
271+ . replace ( 'reflink' , inline . reflink )
272+ . replace ( 'nolink' , inline . nolink )
273+ . getRegex ( ) ;
274+
215275/**
216276 * Normal Inline Grammar
217277 */
@@ -223,8 +283,18 @@ inline.normal = merge({}, inline);
223283 */
224284
225285inline . pedantic = merge ( { } , inline . normal , {
226- strong : / ^ _ _ (? = \S ) ( [ \s \S ] * ?\S ) _ _ (? ! _ ) | ^ \* \* (? = \S ) ( [ \s \S ] * ?\S ) \* \* (? ! \* ) / ,
227- em : / ^ _ (? = \S ) ( [ \s \S ] * ?\S ) _ (? ! _ ) | ^ \* (? = \S ) ( [ \s \S ] * ?\S ) \* (? ! \* ) / ,
286+ strong : {
287+ start : / ^ _ _ | \* \* / ,
288+ middle : / ^ _ _ (? = \S ) ( [ \s \S ] * ?\S ) _ _ (? ! _ ) | ^ \* \* (? = \S ) ( [ \s \S ] * ?\S ) \* \* (? ! \* ) / ,
289+ endAst : / \* \* (? ! \* ) / g,
290+ endUnd : / _ _ (? ! _ ) / g
291+ } ,
292+ em : {
293+ start : / ^ _ | \* / ,
294+ middle : / ^ ( ) \* (? = \S ) ( [ \s \S ] * ?\S ) \* (? ! \* ) | ^ _ (? = \S ) ( [ \s \S ] * ?\S ) _ (? ! _ ) / ,
295+ endAst : / \* (? ! \* ) / g,
296+ endUnd : / _ (? ! _ ) / g
297+ } ,
228298 link : edit ( / ^ ! ? \[ ( l a b e l ) \] \( ( .* ?) \) / )
229299 . replace ( 'label' , inline . _label )
230300 . getRegex ( ) ,
0 commit comments