@@ -5,6 +5,7 @@ import {Octokit} from 'octokit'
5
5
import { Buffer } from 'buffer/'
6
6
7
7
const IGNORE_LIST : string [ ] = [ 'README.md' , '.git' , '.gitignore' ]
8
+ const IMAGE_EXTENSIONS : string [ ] = [ '.png' , '.jpg' , '.jpeg' , '.gif' , '.svg' , '.webp' ]
8
9
9
10
/**
10
11
* Parse GitHub repository details from URL
@@ -34,14 +35,24 @@ function createOctokitClient(githubToken?: string): Octokit {
34
35
}
35
36
36
37
/**
37
- * Download individual markdown file
38
+ * Check if a file is an image based on its extension
39
+ * @param filename Filename to check
40
+ * @returns Boolean indicating if file is an image
41
+ */
42
+ function isImage ( filename : string ) : boolean {
43
+ const ext = path . extname ( filename ) . toLowerCase ( )
44
+ return IMAGE_EXTENSIONS . includes ( ext )
45
+ }
46
+
47
+ /**
48
+ * Download both markdowns or images
38
49
* @param octokit Octokit client
39
50
* @param owner Repository owner
40
51
* @param repo Repository name
41
52
* @param item File item details
42
53
* @param articlesDir Destination directory
43
54
*/
44
- async function downloadMarkdownFile (
55
+ async function downloadFile (
45
56
octokit : Octokit ,
46
57
owner : string ,
47
58
repo : string ,
@@ -56,11 +67,18 @@ async function downloadMarkdownFile(
56
67
} )
57
68
58
69
// Decode file content (GitHub API returns base64 encoded content)
59
- const fileContent = Buffer . from ( data . content , 'base64' ) . toString ( 'utf-8' )
60
-
70
+ const fileContent = Buffer . from ( data . content , 'base64' )
71
+
61
72
const localFilePath = path . join ( articlesDir , item . path )
62
73
await fs . mkdir ( path . dirname ( localFilePath ) , { recursive : true } )
63
- await fs . writeFile ( localFilePath , fileContent )
74
+
75
+ if ( item . name . endsWith ( '.md' ) ) {
76
+ // For markdown files, convert to utf-8 string before writing
77
+ await fs . writeFile ( localFilePath , fileContent . toString ( 'utf-8' ) )
78
+ } else {
79
+ // For binary files like images, write the buffer directly
80
+ await fs . writeFile ( localFilePath , fileContent )
81
+ }
64
82
65
83
console . log ( `Downloaded: ${ item . path } ` )
66
84
} catch ( error ) {
@@ -101,8 +119,11 @@ async function processRepoContents(
101
119
102
120
if ( item . type === 'dir' ) {
103
121
await processRepoContents ( octokit , owner , repo , item . path , articlesDir )
104
- } else if ( item . type === 'file' && item . name . endsWith ( '.md' ) ) {
105
- await downloadMarkdownFile ( octokit , owner , repo , item , articlesDir )
122
+ } else if (
123
+ item . type === 'file' &&
124
+ ( item . name . endsWith ( '.md' ) || isImage ( item . name ) )
125
+ ) {
126
+ await downloadFile ( octokit , owner , repo , item , articlesDir )
106
127
}
107
128
}
108
129
} catch ( error ) {
@@ -112,7 +133,7 @@ async function processRepoContents(
112
133
}
113
134
114
135
/**
115
- * Pull articles from a GitHub repository
136
+ * Pull articles and images from a GitHub repository
116
137
* @param articlePath Destination path for articles
117
138
* @param config Configuration options
118
139
*/
@@ -124,7 +145,6 @@ async function pullArticles(
124
145
}
125
146
) : Promise < void > {
126
147
try {
127
- // Use environment variables if not provided
128
148
const githubToken = config ?. githubToken || process . env . GITHUB_TOKEN
129
149
const sourceUrl = config ?. sourceUrl || process . env . SOURCE
130
150
@@ -139,14 +159,13 @@ async function pullArticles(
139
159
const articlesDir = path . resolve ( process . cwd ( ) , articlePath )
140
160
await fs . mkdir ( articlesDir , { recursive : true } )
141
161
142
- // Start processing from root
143
162
await processRepoContents ( octokit , owner , repo , '' , articlesDir )
144
163
145
- console . log ( 'Article pull completed successfully' )
164
+ console . log ( 'Article and image pull completed successfully' )
146
165
} catch ( error ) {
147
- console . error ( 'Error pulling articles:' , error )
166
+ console . error ( 'Error pulling articles and images :' , error )
148
167
throw error
149
168
}
150
169
}
151
170
152
- export default pullArticles
171
+ export default pullArticles
0 commit comments