Skip to content

Commit 3f3537b

Browse files
committed
images for markdown files
closes #10
1 parent ff9b17a commit 3f3537b

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ Creates a local backup of your GitHub data.
44

55
## In Scope
66

7-
- ✅ Repositories: cloned, public and private ones
7+
- ✅ Repositories: cloned, public and private ones, images from markdown files
88
- ✅ Releases: including images and assets
99
- ✅ Issues: including comments and images, open and closed ones
1010
- ✅ User: details and starred repositories
1111

1212
## In Progress
13-
- ⏳ Readme: Images and file attachments
1413
- ⏳ Releases: File attachments
1514
- ⏳ Issues: File attachments
15+
- ⏳ Markdown: File attachments
1616
- ⏳ Projects: classic per repo and new projects per user
1717

1818
## Usage

index.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import fs from 'fs-extra'
22
import shell from 'shelljs'
3-
import { dirname, basename, extname } from 'path'
3+
import { relative, dirname, basename, extname } from 'path'
44
import fetch from 'node-fetch'
55
import { extension } from 'mime-types'
6+
import { glob } from 'glob'
67

78
shell.config.fatal = true
89

@@ -129,7 +130,7 @@ function downloadFile(sourceFileUrl, targetFilePath) {
129130
})
130131
}
131132

132-
function downloadImages(body, folder, filename) {
133+
function downloadImages(body, folder, filename, baseImagePath = './images') {
133134
return new Promise(async (resolve, reject) => {
134135
try {
135136
const images = body?.match(/["(]https:\/\/github\.com\/(.+)\/assets\/(.+)[)"]/g) || []
@@ -139,8 +140,8 @@ function downloadImages(body, folder, filename) {
139140
const sourceUrl = images[n].replace(/^["(](.+)[)"]$/, '$1')
140141
fs.ensureDirSync(folder)
141142
const realTargetFilename = basename(await downloadFile(sourceUrl, targetPath))
142-
body = body.replace(`"${sourceUrl}"`, '"./images/' + realTargetFilename + '"')
143-
body = body.replace(`(${sourceUrl})`, '(./images/' + realTargetFilename + ')')
143+
body = body.replace(`"${sourceUrl}"`, `"${baseImagePath}/${realTargetFilename}"`)
144+
body = body.replace(`(${sourceUrl})`, `(${baseImagePath}/${realTargetFilename})`)
144145
}
145146
return resolve(body)
146147
} catch (err) {
@@ -237,6 +238,30 @@ async function backup() {
237238
// Clone repository
238239
shell.exec(`git clone https://${TOKEN}@github.com/${USERNAME}/${repository.name}.git ${folder}/repositories/${repository.name}/repository`)
239240

241+
// Get markdown files
242+
const repoFolder = `${folder}/repositories/${repository.name}/repository/`
243+
const imageFolder = `${folder}/repositories/${repository.name}/images/`
244+
const markdownFiles = await glob(`${repoFolder}**/*.{md,MD}`)
245+
246+
// Loop markdown files
247+
for (const markdownFile of markdownFiles) {
248+
249+
// Download markdown images
250+
const baseImagePath = relative(dirname(markdownFile), imageFolder)
251+
const imageFileBasename = markdownFile.replace(repoFolder, '').replace(/\//g, '_').replace(/\.md$/i, '')
252+
let markdownFileContent = fs.readFileSync(markdownFile, { encoding: 'utf8' })
253+
markdownFileContent = await downloadImages(
254+
markdownFileContent,
255+
imageFolder,
256+
`markdown_${imageFileBasename}_{id}`,
257+
baseImagePath
258+
)
259+
260+
// Update markdown file
261+
fs.writeFileSync(markdownFile, markdownFileContent)
262+
263+
}
264+
240265
}
241266

242267
// Get user details

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"type": "module",
33
"dependencies": {
44
"fs-extra": "^11.1.1",
5+
"glob": "^10.3.10",
56
"mime-types": "^2.1.35",
67
"node-fetch": "^3.3.2",
78
"shelljs": "^0.8.5"

0 commit comments

Comments
 (0)