11import {
2+ fileBasename ,
3+ formatFilePathPosition ,
24 inlineCodeFilePathCandidate ,
3- isConventionalFilePosition ,
5+ normalizeMarkdownLinkDestination ,
6+ parseMarkdownFileLink ,
47} from "@t3tools/client-runtime/markdown-links" ;
58import { videoMimeType } from "@t3tools/shared/video" ;
69
710import type { MARKDOWN_FILE_ICON_SOURCES } from "./markdownFileIcons.generated" ;
811
9- const WINDOWS_DRIVE_PATH_PATTERN = / ^ [ A - Z a - z ] : [ \\ / ] / ;
10- const WINDOWS_UNC_PATH_PATTERN = / ^ \\ \\ / ;
11- const RELATIVE_PATH_PREFIX_PATTERN = / ^ ( ~ \/ | \. { 1 , 2 } \/ ) / ;
12- const RELATIVE_FILE_PATH_PATTERN =
13- / ^ (?: [ A - Z a - z 0 - 9 . _ - ] + (?: + [ A - Z a - z 0 - 9 . _ - ] + ) * \/ ) + [ A - Z a - z 0 - 9 . _ - ] + (?: + [ A - Z a - z 0 - 9 . _ - ] + ) * (?: : \d + ) { 0 , 2 } $ / ;
14- const RELATIVE_FILE_NAME_PATTERN =
15- / ^ [ A - Z a - z 0 - 9 . _ - ] + (?: + [ A - Z a - z 0 - 9 . _ - ] + ) * \. [ A - Z a - z 0 - 9 _ - ] + (?: : \d + ) { 0 , 2 } $ / ;
1612const POSITION_SUFFIX_PATTERN = / : \d + (?: : \d + ) ? $ / ;
17- const POSIX_FILE_ROOT_PREFIXES = [
18- "/Users/" ,
19- "/home/" ,
20- "/tmp/" ,
21- "/var/" ,
22- "/etc/" ,
23- "/opt/" ,
24- "/mnt/" ,
25- "/Volumes/" ,
26- "/private/" ,
27- "/root/" ,
28- ] as const ;
2913
3014export type MarkdownLinkPresentation =
3115 | {
@@ -246,112 +230,13 @@ const FILE_ICON_BY_EXTENSION: Readonly<Record<string, MarkdownFileIcon>> = {
246230 zsh : "bash" ,
247231} ;
248232
249- function safeDecode ( value : string ) : string {
250- try {
251- return decodeURIComponent ( value ) ;
252- } catch {
253- return value ;
254- }
255- }
256-
257- function normalizeDestination ( value : string ) : string {
258- const trimmed = value . trim ( ) ;
259- return trimmed . startsWith ( "<" ) && trimmed . endsWith ( ">" ) ? trimmed . slice ( 1 , - 1 ) : trimmed ;
260- }
261-
262233/** Native link and media APIs have no document scheme to inherit from protocol-relative URLs. */
263234export function normalizeNativeMarkdownUrl ( value : string ) : string {
264235 return value . startsWith ( "//" ) ? `https:${ value } ` : value ;
265236}
266237
267- function fileUrlTarget ( href : string ) : { readonly path : string ; readonly hash : string } | null {
268- try {
269- const parsed = new URL ( href ) ;
270- if ( parsed . protocol . toLowerCase ( ) !== "file:" ) {
271- return null ;
272- }
273- const uncHostname = parsed . hostname . toLowerCase ( ) === "localhost" ? "" : parsed . hostname ;
274- const rawPath = uncHostname
275- ? `\\\\${ uncHostname } ${ parsed . pathname . replaceAll ( "/" , "\\" ) } `
276- : parsed . pathname ;
277- const path = / ^ \/ [ A - Z a - z ] : [ \\ / ] / . test ( rawPath ) ? rawPath . slice ( 1 ) : rawPath ;
278- return { path, hash : parsed . hash } ;
279- } catch {
280- return null ;
281- }
282- }
283-
284- function stripSearchAndHash ( value : string ) : { readonly path : string ; readonly hash : string } {
285- const hashIndex = value . indexOf ( "#" ) ;
286- const pathWithSearch = hashIndex >= 0 ? value . slice ( 0 , hashIndex ) : value ;
287- const hash = hashIndex >= 0 ? value . slice ( hashIndex ) : "" ;
288- const queryIndex = pathWithSearch . indexOf ( "?" ) ;
289- return {
290- path : queryIndex >= 0 ? pathWithSearch . slice ( 0 , queryIndex ) : pathWithSearch ,
291- hash,
292- } ;
293- }
294-
295- function splitFilePosition (
296- path : string ,
297- hash : string ,
298- ) : { readonly path : string ; readonly line ?: number ; readonly column ?: number } {
299- const suffixMatch = path . match ( / : ( \d + ) (?: : ( \d + ) ) ? $ / ) ;
300- const hashMatch = suffixMatch ? null : hash . match ( / ^ # L ( \d + ) (?: C ( \d + ) ) ? $ / i) ;
301- const match = suffixMatch ?? hashMatch ;
302- if ( ! match ?. [ 1 ] ) {
303- return { path } ;
304- }
305-
306- const line = Number . parseInt ( match [ 1 ] , 10 ) ;
307- const column = match [ 2 ] ? Number . parseInt ( match [ 2 ] , 10 ) : undefined ;
308- const pathWithoutPosition = suffixMatch ? path . slice ( 0 , - suffixMatch [ 0 ] . length ) : path ;
309- return {
310- path : pathWithoutPosition ,
311- ...( line > 0 ? { line } : { } ) ,
312- ...( column !== undefined && column > 0 ? { column } : { } ) ,
313- } ;
314- }
315-
316- function looksLikePosixFilesystemPath ( path : string ) : boolean {
317- if ( ! path . startsWith ( "/" ) ) {
318- return false ;
319- }
320- if ( POSIX_FILE_ROOT_PREFIXES . some ( ( prefix ) => path . startsWith ( prefix ) ) ) {
321- return true ;
322- }
323- if ( POSITION_SUFFIX_PATTERN . test ( path ) ) {
324- return true ;
325- }
326- const basename = path . slice ( path . lastIndexOf ( "/" ) + 1 ) ;
327- return / \. [ A - Z a - z 0 - 9 _ - ] + $ / . test ( basename ) ;
328- }
329-
330- function looksLikeFilePath ( value : string ) : boolean {
331- if ( WINDOWS_DRIVE_PATH_PATTERN . test ( value ) || WINDOWS_UNC_PATH_PATTERN . test ( value ) ) {
332- return true ;
333- }
334- if ( RELATIVE_PATH_PREFIX_PATTERN . test ( value ) ) {
335- return true ;
336- }
337- if ( value . startsWith ( "/" ) ) {
338- return looksLikePosixFilesystemPath ( value ) ;
339- }
340- if ( FILE_ICON_BY_NAME [ value . replace ( POSITION_SUFFIX_PATTERN , "" ) . toLowerCase ( ) ] ) {
341- return true ;
342- }
343- if ( isConventionalFilePosition ( value ) ) return true ;
344- return RELATIVE_FILE_PATH_PATTERN . test ( value ) || RELATIVE_FILE_NAME_PATTERN . test ( value ) ;
345- }
346-
347- function fileLabel ( value : string ) : string {
348- const normalized = value . replaceAll ( "\\" , "/" ) ;
349- const basename = normalized . slice ( normalized . lastIndexOf ( "/" ) + 1 ) ;
350- return basename || normalized ;
351- }
352-
353238export function resolveMarkdownFileIcon ( value : string ) : MarkdownFileIcon {
354- const basename = fileLabel ( value ) . replace ( POSITION_SUFFIX_PATTERN , "" ) . toLowerCase ( ) ;
239+ const basename = fileBasename ( value ) . replace ( POSITION_SUFFIX_PATTERN , "" ) . toLowerCase ( ) ;
355240 if ( videoMimeType ( { name : basename , mimeType : "" } ) !== null ) return "video" ;
356241 const exactIcon = FILE_ICON_BY_NAME [ basename ] ;
357242 if ( exactIcon ) return exactIcon ;
@@ -367,7 +252,7 @@ export function resolveMarkdownFileIcon(value: string): MarkdownFileIcon {
367252}
368253
369254export function resolveMarkdownLinkPresentation ( href : string ) : MarkdownLinkPresentation {
370- const normalized = normalizeDestination ( href ) ;
255+ const normalized = normalizeMarkdownLinkDestination ( href ) ;
371256 try {
372257 const parsed = new URL ( normalizeNativeMarkdownUrl ( normalized ) ) ;
373258 if ( parsed . protocol === "http:" || parsed . protocol === "https:" ) {
@@ -381,31 +266,16 @@ export function resolveMarkdownLinkPresentation(href: string): MarkdownLinkPrese
381266 // Relative paths and non-URL link destinations are handled below.
382267 }
383268
384- const source = normalized . toLowerCase ( ) . startsWith ( "file:" )
385- ? fileUrlTarget ( normalized )
386- : stripSearchAndHash ( normalized ) ;
387- const decodedSource = source
388- ? { path : safeDecode ( source . path . trim ( ) ) , hash : safeDecode ( source . hash . trim ( ) ) }
389- : null ;
390- const fileTarget = decodedSource
391- ? splitFilePosition ( decodedSource . path , decodedSource . hash )
392- : null ;
393- const targetWithPosition = fileTarget
394- ? `${ fileTarget . path } ${
395- fileTarget . line
396- ? `:${ fileTarget . line } ${ fileTarget . column ? `:${ fileTarget . column } ` : "" } `
397- : ""
398- } `
399- : null ;
400- if ( fileTarget && targetWithPosition && looksLikeFilePath ( targetWithPosition ) ) {
269+ const target = parseMarkdownFileLink ( normalized ) ;
270+ if ( target ) {
401271 return {
402272 kind : "file" ,
403273 href : normalized ,
404- icon : resolveMarkdownFileIcon ( fileTarget . path ) ,
405- label : fileLabel ( targetWithPosition ) ,
406- path : fileTarget . path ,
407- ...( fileTarget . line ? { line : fileTarget . line } : { } ) ,
408- ...( fileTarget . column ? { column : fileTarget . column } : { } ) ,
274+ icon : resolveMarkdownFileIcon ( target . path ) ,
275+ label : fileBasename ( formatFilePathPosition ( target ) ) ,
276+ path : target . path ,
277+ ...( target . line ? { line : target . line } : { } ) ,
278+ ...( target . column ? { column : target . column } : { } ) ,
409279 } ;
410280 }
411281
0 commit comments