@@ -158,7 +158,7 @@ const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
158158
159159export const CHAT_MARKDOWN_REMARK_PLUGINS : PluggableList = [
160160 remarkGfm ,
161- remarkKeepWindowsPathDestinations ,
161+ remarkKeepWindowsPaths ,
162162 remarkGithubAlerts ,
163163 remarkNormalizeListItemIndentation ,
164164 remarkCodexDirectives ,
@@ -168,7 +168,7 @@ export const CHAT_MARKDOWN_REMARK_PLUGINS: PluggableList = [
168168
169169export const CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS : PluggableList = [
170170 remarkGfm ,
171- remarkKeepWindowsPathDestinations ,
171+ remarkKeepWindowsPaths ,
172172 remarkGithubAlerts ,
173173 remarkNormalizeListItemIndentation ,
174174 remarkCodexDirectives ,
@@ -255,17 +255,37 @@ function keepWindowsPathDestination(this: DestinationCompileContext, token: unkn
255255 node . url = isWindowsAbsolutePath ( authored ) && ! authored . includes ( "&" ) ? authored : decoded ;
256256}
257257
258+ // A drive path that has not reached whitespace yet: `C:`, `C:\Users\me`, `C:/work`.
259+ const UNFINISHED_DRIVE_PATH = / (?: ^ | \W ) [ A - Z a - z ] : (?: [ \\ / ] \S * ) ? $ / ;
260+
261+ interface EscapeCompileContext {
262+ readonly stack : ReadonlyArray < {
263+ readonly type : string ;
264+ children ?: Array < { type : string ; value ?: string } > ;
265+ } > ;
266+ }
267+
268+ function keepWindowsPathEscape ( this : EscapeCompileContext ) {
269+ const text = this . stack . at ( - 1 ) ?. children ?. at ( - 1 ) ;
270+ if ( text ?. type !== "text" || text . value === undefined ) return ;
271+ const before = text . value . slice ( 0 , - 1 ) ;
272+ if ( UNFINISHED_DRIVE_PATH . test ( before ) ) text . value = `${ before } \\${ text . value . slice ( - 1 ) } ` ;
273+ }
274+
258275/**
259- * CommonMark reads the `\.` in `C:\me\.t3\shot.png` as an escape, even in a link
260- * destination. Every backslash in a Windows path is a separator, so link, image, and
261- * definition destinations that are Windows paths keep the text as written.
276+ * CommonMark reads the `\.` in `C:\me\.t3\shot.png` as an escape. Every backslash in a
277+ * Windows path is a separator, so link, image, and definition destinations that are
278+ * Windows paths keep the text as written, and so do escapes inside drive paths in prose.
279+ * Prose paths end at whitespace: past that, nothing tells a path from a sentence, and
280+ * `C:\x and \*this\*` must still read `*this*`.
262281 */
263- function remarkKeepWindowsPathDestinations ( this : Processor ) {
282+ export function remarkKeepWindowsPaths ( this : Processor ) {
264283 const data = this . data ( ) ;
265284 ( data . fromMarkdownExtensions ??= [ ] ) . push ( {
266285 exit : {
267286 resourceDestinationString : keepWindowsPathDestination ,
268287 definitionDestinationString : keepWindowsPathDestination ,
288+ characterEscape : keepWindowsPathEscape ,
269289 } ,
270290 } ) ;
271291}
0 commit comments