@@ -15,8 +15,14 @@ export const ATTACHMENT_PATTERNS = {
1515 legacy : / h t t p s : \/ \/ u s e r - i m a g e s \. g i t h u b u s e r c o n t e n t \. c o m \/ [ ^ \s ) ] + / g
1616} as const ;
1717
18- async function downloadFile ( url : string ) : Promise < string > {
19- const response = await fetch ( url ) ;
18+ async function downloadFile ( url : string , githubToken ?: string ) : Promise < string > {
19+ // Add authentication header for GitHub URLs
20+ const headers : Record < string , string > = { } ;
21+ if ( githubToken && ( url . includes ( 'github.com' ) || url . includes ( 'githubusercontent.com' ) ) ) {
22+ headers [ 'Authorization' ] = `token ${ githubToken } ` ;
23+ }
24+
25+ const response = await fetch ( url , { headers } ) ;
2026 if ( ! response . ok ) {
2127 throw new Error ( `Failed to download ${ url } : ${ response . status } ${ response . statusText } ` ) ;
2228 }
@@ -54,16 +60,19 @@ async function downloadFile(url: string): Promise<string> {
5460 * - Markdown images: 
5561 * - Markdown files: (https://github.com/user-attachments/files/...)
5662 * - Legacy images: https://user-images.githubusercontent.com/...
63+ *
64+ * @param text - Text containing attachment URLs
65+ * @param githubToken - GitHub token for authenticating downloads from GitHub
5766 */
58- export async function downloadAttachmentsAndRewriteText ( text : string ) : Promise < string > {
67+ export async function downloadAttachmentsAndRewriteText ( text : string , githubToken ?: string ) : Promise < string > {
5968 let updatedText = text ;
6069
6170 // Handle HTML image tags with user-attachments URLs
6271 const imgMatches = [ ...text . matchAll ( ATTACHMENT_PATTERNS . imgTag ) ] ;
6372 for ( const match of imgMatches ) {
6473 const url = match [ 1 ] ;
6574 try {
66- const localPath = await downloadFile ( url ) ;
75+ const localPath = await downloadFile ( url , githubToken ) ;
6776 updatedText = updatedText . replace ( match [ 0 ] , `src="${ localPath } "` ) ;
6877 } catch ( error ) {
6978 console . error ( `Failed to download image: ${ url } ` , error ) ;
@@ -75,7 +84,7 @@ export async function downloadAttachmentsAndRewriteText(text: string): Promise<s
7584 for ( const match of mdImgMatches ) {
7685 const url = match [ 1 ] ;
7786 try {
78- const localPath = await downloadFile ( url ) ;
87+ const localPath = await downloadFile ( url , githubToken ) ;
7988 updatedText = updatedText . replace ( match [ 1 ] , localPath ) ;
8089 } catch ( error ) {
8190 console . error ( `Failed to download markdown image: ${ url } ` , error ) ;
@@ -87,7 +96,7 @@ export async function downloadAttachmentsAndRewriteText(text: string): Promise<s
8796 for ( const match of fileMatches ) {
8897 const url = match [ 1 ] ;
8998 try {
90- const localPath = await downloadFile ( url ) ;
99+ const localPath = await downloadFile ( url , githubToken ) ;
91100 updatedText = updatedText . replace ( match [ 0 ] , `(${ localPath } )` ) ;
92101 } catch ( error ) {
93102 console . error ( `Failed to download file: ${ url } ` , error ) ;
@@ -99,7 +108,7 @@ export async function downloadAttachmentsAndRewriteText(text: string): Promise<s
99108 for ( const match of legacyMatches ) {
100109 const url = match [ 0 ] ;
101110 try {
102- const localPath = await downloadFile ( url ) ;
111+ const localPath = await downloadFile ( url , githubToken ) ;
103112 updatedText = updatedText . replace ( url , localPath ) ;
104113 } catch ( error ) {
105114 console . error ( `Failed to download legacy image: ${ url } ` , error ) ;
0 commit comments