@@ -99,17 +99,27 @@ export const externalLinkRegex = /^https?:\/\//i
9999
100100export const arrowRegex = new RegExp ( / ( - { 1 , 2 } > | = { 1 , 2 } > | < - { 1 , 2 } | < = { 1 , 2 } ) / , "g" )
101101
102- // (\|[^\|\[\n]*)? -> optional check if wikilink is inside a table cell
103- // !? -> optional embedding
104- // \[\[ -> open brace
105- // ([^\[\]\|\#]+) -> one or more non-special characters ([,],|, or #) (name)
106- // (#[^\[\]\|\#]+)? -> # then one or more non-special characters (heading link)
107- // (\|[^\[\]\#]+)? -> \| then one or more non-special characters (alias)
108- // ([^\|\n]*\|)? -> optional check if wikilink is inside a table cell
102+ // !? -> optional embedding
103+ // \[\[ -> open brace
104+ // ([^\[\]\|\#]+) -> one or more non-special characters ([,],|, or #) (name)
105+ // (#[^\[\]\|\#]+)? -> # then one or more non-special characters (heading link)
106+ // (\\?\|[^\[\]\#]+)? -> optional escape \ then | then one or more non-special characters (alias)
109107export const wikilinkRegex = new RegExp (
110- / ( \| [ ^ \| \[ \n ] * ) ? ! ? \[ \[ ( [ ^ \[ \] \| \# \\ ] + ) ? ( # + [ ^ \[ \] \| \# \\ ] + ) ? ( \\ ? \| [ ^ \[ \] \# ] + ) ? \] \] ( [ ^ \| \n ] * \| ) ? / ,
108+ / ! ? \[ \[ ( [ ^ \[ \] \| \# \\ ] + ) ? ( # + [ ^ \[ \] \| \# \\ ] + ) ? ( \\ ? \| [ ^ \[ \] \# ] + ) ? \] \] / ,
111109 "g" ,
112110)
111+
112+ // ^\|([^\n])+\|\n(\|) -> matches the header row
113+ // ( ?:?-{3,}:? ?\|)+ -> matches the header row separator
114+ // (\|([^\n])+\|\n)+ -> matches the body rows
115+ export const tableRegex = new RegExp (
116+ / ^ \| ( [ ^ \n ] ) + \| \n ( \| ) ( ? : ? - { 3 , } : ? ? \| ) + \n ( \| ( [ ^ \n ] ) + \| \n ? ) + / ,
117+ "gm" ,
118+ )
119+
120+ // matches any wikilink, only used for escaping wikilinks inside tables
121+ export const tableWikilinkRegex = new RegExp ( / ( ! ? \[ \[ [ ^ \] ] * ?\] \] ) / , "g" )
122+
113123const highlightRegex = new RegExp ( / = = ( [ ^ = ] + ) = = / , "g" )
114124const commentRegex = new RegExp ( / % % [ \s \S ] * ?% % / , "g" )
115125// from https://github.com/escwxyz/remark-obsidian-callout/blob/main/src/index.ts
@@ -171,27 +181,34 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options>
171181 src = src . toString ( )
172182 }
173183
184+ // replace all wikilinks inside a table first
185+ src = src . replace ( tableRegex , ( value ) => {
186+ // escape all aliases and headers in wikilinks inside a table
187+ return value . replace ( tableWikilinkRegex , ( value , ...capture ) => {
188+ const [ raw ] : ( string | undefined ) [ ] = capture
189+ let escaped = raw ?? ""
190+ escaped = escaped . replace ( "#" , "\\#" )
191+ escaped = escaped . replace ( "|" , "\\|" )
192+
193+ return escaped
194+ } )
195+ } )
196+
197+ // replace all other wikilinks
174198 src = src . replace ( wikilinkRegex , ( value , ...capture ) => {
175- const [ rawTablePre , rawFp , rawHeader , rawAlias , rawTablePost ] : ( string | undefined ) [ ] =
176- capture
199+ const [ rawFp , rawHeader , rawAlias ] : ( string | undefined ) [ ] = capture
177200
178201 const fp = rawFp ?? ""
179202 const anchor = rawHeader ?. trim ( ) . replace ( / ^ # + / , "" )
180203 const blockRef = Boolean ( anchor ?. startsWith ( "^" ) ) ? "^" : ""
181204 const displayAnchor = anchor ? `#${ blockRef } ${ slugAnchor ( anchor ) } ` : ""
182- let displayAlias = rawAlias ?? rawHeader ?. replace ( "#" , "|" ) ?? ""
205+ const displayAlias = rawAlias ?? rawHeader ?. replace ( "#" , "|" ) ?? ""
183206 const embedDisplay = value . startsWith ( "!" ) ? "!" : ""
184207
185208 if ( rawFp ?. match ( externalLinkRegex ) ) {
186209 return `${ embedDisplay } [${ displayAlias . replace ( / ^ \| / , "" ) } ](${ rawFp } )`
187210 }
188211
189- // transform `[[note#^block_ref|^block_ref]]` to `[[note#^block_ref\|^block_ref]]`,
190- // when the wikilink with alias is inside a table.
191- if ( displayAlias && displayAlias . startsWith ( "|" ) && rawTablePre && rawTablePost ) {
192- displayAlias = `\\${ displayAlias } `
193- }
194-
195212 return `${ embedDisplay } [[${ fp } ${ displayAnchor } ${ displayAlias } ]]`
196213 } )
197214 }
@@ -211,7 +228,7 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options>
211228 replacements . push ( [
212229 wikilinkRegex ,
213230 ( value : string , ...capture : string [ ] ) => {
214- let [ _rawTablePre , rawFp , rawHeader , rawAlias , _rawTablePost ] = capture
231+ let [ rawFp , rawHeader , rawAlias ] = capture
215232 const fp = rawFp ?. trim ( ) ?? ""
216233 const anchor = rawHeader ?. trim ( ) ?? ""
217234 const alias = rawAlias ?. slice ( 1 ) . trim ( )
0 commit comments