@@ -97,30 +97,68 @@ const model = {
9797 . split ( "/" ) ;
9898 if ( ! owner || ! repo ) return html ;
9999
100- const repoBlobBase = `https://github.com/${ owner } /${ repo } /blob/${ branch } ` ;
100+ const repoWebBase = `https://github.com/${ owner } /${ repo } ` ;
101+ const repoBlobBase = `${ repoWebBase } /blob/${ branch } ` ;
102+ const repoRawBase = `https://raw.githubusercontent.com/${ owner } /${ repo } /${ branch } ` ;
101103 const doc = new DOMParser ( ) . parseFromString ( html , "text/html" ) ;
104+ const githubRepoRoutePrefixes = new Set ( [
105+ "actions" ,
106+ "blob" ,
107+ "branches" ,
108+ "commit" ,
109+ "commits" ,
110+ "compare" ,
111+ "discussions" ,
112+ "issues" ,
113+ "labels" ,
114+ "milestones" ,
115+ "packages" ,
116+ "projects" ,
117+ "pulls" ,
118+ "raw" ,
119+ "releases" ,
120+ "security" ,
121+ "tags" ,
122+ "tree" ,
123+ "wiki" ,
124+ ] ) ;
125+ const shouldSkipRebase = ( value ) =>
126+ ! value ||
127+ value . startsWith ( "#" ) ||
128+ value . startsWith ( "//" ) ||
129+ / ^ [ a - z A - Z ] [ a - z A - Z \d + . - ] * : / . test ( value ) ;
130+
131+ const resolveRepoPath = ( value ) => {
132+ if ( shouldSkipRebase ( value ) ) return null ;
133+ try {
134+ const resolved = new URL ( value , "https://repo-root.invalid/" ) ;
135+ return `${ resolved . pathname . replace ( / ^ \/ + / , "" ) } ${ resolved . search } ${ resolved . hash } ` ;
136+ } catch {
137+ return null ;
138+ }
139+ } ;
140+ const isRepoRoutePath = ( repoPath ) => {
141+ const pathOnly = repoPath
142+ . split ( / [ ? # ] / , 1 ) [ 0 ]
143+ . replace ( / ^ \/ + | \/ + $ / g, "" ) ;
144+ if ( ! pathOnly ) return false ;
145+ const firstSegment = pathOnly . split ( "/" ) [ 0 ] . toLowerCase ( ) ;
146+ return githubRepoRoutePrefixes . has ( firstSegment ) ;
147+ } ;
102148
103149 doc . querySelectorAll ( "a[href]" ) . forEach ( ( anchor ) => {
104150 const href = ( anchor . getAttribute ( "href" ) || "" ) . trim ( ) ;
105- if (
106- ! href ||
107- href . startsWith ( "#" ) ||
108- href . startsWith ( "//" ) ||
109- / ^ [ a - z A - Z ] [ a - z A - Z \d + . - ] * : / . test ( href )
110- ) {
111- return ;
112- }
151+ const repoPath = resolveRepoPath ( href ) ;
152+ if ( ! repoPath ) return ;
153+ const base = isRepoRoutePath ( repoPath ) ? repoWebBase : repoBlobBase ;
154+ anchor . setAttribute ( "href" , `${ base } /${ repoPath } ` ) ;
155+ } ) ;
113156
114- try {
115- const resolved = new URL ( href , "https://repo-root.invalid/" ) ;
116- const repoPath = resolved . pathname . replace ( / ^ \/ + / , "" ) ;
117- anchor . setAttribute (
118- "href" ,
119- `${ repoBlobBase } /${ repoPath } ${ resolved . search } ${ resolved . hash } ` ,
120- ) ;
121- } catch {
122- // Leave malformed links unchanged.
123- }
157+ doc . querySelectorAll ( "img[src]" ) . forEach ( ( image ) => {
158+ const src = ( image . getAttribute ( "src" ) || "" ) . trim ( ) ;
159+ const repoPath = resolveRepoPath ( src ) ;
160+ if ( ! repoPath ) return ;
161+ image . setAttribute ( "src" , `${ repoRawBase } /${ repoPath } ` ) ;
124162 } ) ;
125163
126164 return doc . body . innerHTML ;
0 commit comments