Skip to content

Commit

Permalink
Embed SVG images in the readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
o-alexandre-felipe committed Sep 8, 2022
1 parent f524382 commit c040474
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,4 @@ src/index.js
src/index.js.map
src/text-renderer.d.ts
src/text-renderer.js.map
static/frozen-*
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
"name": "@datadocs/formatter",
"license": "MIT",
"author": "Alexandre Felipe <o.alexandre.felipe@gmail.com>",
"version": "0.1.4",
"version": "0.1.5",
"private": false,
"main": "dist/index.js",
"files": [
"dist/index.js",
"src/index.d.ts",
"src/*.d.ts",
"static/frozen-*.svg",
"static/frozen-*.png"
],
Expand Down
63 changes: 43 additions & 20 deletions scripts/freeze-readme.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,55 @@ const fs = require('fs');
const https = require('https');
const readme_file = __dirname + '/../README.md'
let README = fs.readFileSync(readme_file, {encoding:'utf-8'})
let FROZEN_SECTION = /\<\!--\s*BEGIN\s*FROZEN\s*IMAGE\s*(https:\/\/\S*?)(?:\s*as\s*(\S*?))?\s*-->.*?\<!--\s*END\s*FROZEN\s*IMAGE\s*--\>/igs
const UPDATED_README = README.replace(FROZEN_SECTION, downloadAndUpdate);
fs.writeFileSync(readme_file, UPDATED_README)
console.log(UPDATED_README)
const FROZEN_SECTION = /\<\!--\s*BEGIN\s*FROZEN\s*IMAGE\s*(https:\/\/\S*?)(?:\s*as\s*(\S*?))?\s*-->.*?\<!--\s*END\s*FROZEN\s*IMAGE\s*--\>/igs
const FROZEN_RE = /\!\[]\((static\/frozen-[^)]*)\)/g

const pending_downloads = []
README = README.replace(FROZEN_SECTION, downloadAndUpdate);
Promise.all(pending_downloads).then(
r => {
README = README.replace(FROZEN_RE, embedFrozenImage)
}
).then(r => {
fs.writeFileSync(readme_file, README)
})
function downloadAndUpdate(body, url, alias){
if(!alias){
alias = url.split('?')[0].split('/').pop()
}
https.get(url,
{
"headers": {
"cache-control": "no-cache",
"pragma": "no-cache"

pending_downloads.push(new Promise((resolve, reject) => {
setTimeout(() => reject(new Error('Timeout')), 15000)
https.get(url,
{
"headers": {
"cache-control": "no-cache",
"pragma": "no-cache"
},
},
}, (response, error) => {
if(error){
console.log(`Failed to download ${url}`)
}else{
const file = fs.createWriteStream(`${__dirname}/../static/frozen-${alias}`);
file.on('finish', () => {
console.log(`Saved file ${alias}`)
(response, error) => {
if(error){
console.log(`Failed to download ${url}`)
reject(new Error(`Failed to download ${url}`))
}else{
const file = fs.createWriteStream(`${__dirname}/../static/frozen-${alias}`);
file.on('finish', () => {
console.log(`Saved file ${alias}`)
resolve();
})
response.pipe(file);
}
})
response.pipe(file);
}
});
}))
return `<!-- BEGIN FROZEN IMAGE ${url} as ${alias} -->
<img src="./static/frozen-${alias}">
![](static/frozen-${alias})
<!-- END FROZEN IMAGE -->`
}

function embedFrozenImage(_, img){
if(img.toLowerCase().endsWith('.svg')){
return fs.readFileSync(`${__dirname}/../${img}`, {encoding:'utf-8'})
}else{
return _;
}
}

0 comments on commit c040474

Please sign in to comment.