-
-
Notifications
You must be signed in to change notification settings - Fork 39.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf734b0
commit faf70f6
Showing
4 changed files
with
497 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// get all the base64 encoded images and save them to a file from the given markdown file | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
const matter = require('gray-matter'); | ||
|
||
const guidePath = path.join(process.cwd(), 'src/data/guides'); | ||
const tempDir = path.join(process.cwd(), '.temp'); | ||
|
||
const guideId = process.argv[2]; | ||
if (!guideId) { | ||
console.error('Guide ID is required'); | ||
process.exit(1); | ||
} | ||
|
||
const guideContent = fs.readFileSync( | ||
path.join(guidePath, `${guideId}.md`), | ||
'utf8', | ||
); | ||
|
||
// Create temp directory if it doesn't exist | ||
const guideTempDir = path.join(tempDir, guideId); | ||
if (!fs.existsSync(guideTempDir)) { | ||
fs.mkdirSync(guideTempDir, { recursive: true }); | ||
} | ||
|
||
const { data, content } = matter(guideContent); | ||
|
||
// Find all base64 image references in the content | ||
const images = content.match(/\[(.+?)\]:\s+?<data:image\/([^;]+);base64,([^\s]+)/g); | ||
|
||
if (images) { | ||
images.forEach((image) => { | ||
const imageName = image.match(/\[(.+?)\]/)[1]; | ||
const imageExtension = image.match(/<data:image\/([^;]+);base64/)[1]; | ||
const imageData = image.match(/base64,([^\s]+)/)[1]; | ||
|
||
// Write file using Buffer to properly decode base64 | ||
fs.writeFileSync( | ||
path.join(guideTempDir, `${imageName}.${imageExtension}`), | ||
Buffer.from(imageData, 'base64') | ||
); | ||
}); | ||
} |
Oops, something went wrong.