Skip to content

Commit eebb398

Browse files
committed
init
0 parents  commit eebb398

File tree

5 files changed

+535
-0
lines changed

5 files changed

+535
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

manifest.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
name: netlify-plugin-cloudinary

package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "netlify-plugin-cloudinary",
3+
"version": "0.0.1",
4+
"description": "",
5+
"main": "src/index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": {
10+
"name": "Colby Fayock",
11+
"email": "[email protected]",
12+
"url": "https://twitter.com/colbyfayock"
13+
},
14+
"license": "MIT",
15+
"dependencies": {
16+
"fs": "^0.0.1-security",
17+
"glob": "^7.2.0",
18+
"jsdom": "^18.1.1"
19+
}
20+
}

src/index.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const glob = require('glob');
2+
const fs = require('fs').promises;
3+
const { JSDOM } = require('jsdom');
4+
5+
const CLOUD_NAME = 'colbycloud';
6+
7+
module.exports = {
8+
9+
async onPostBuild({ constants }) {
10+
const { PUBLISH_DIR } = constants;
11+
12+
const pages = glob.sync(`${PUBLISH_DIR}/**/*.html`);
13+
14+
15+
for ( const page of pages ) {
16+
const html = await fs.readFile(page, 'utf-8');
17+
const dom = new JSDOM(html);
18+
const images = Array.from(dom.window.document.querySelectorAll('img'));
19+
20+
for ( const $img of images ) {
21+
const imgSrc = $img.getAttribute('src');
22+
let cloudinarySrc = imgSrc
23+
24+
if ( !imgSrc.startsWith('http') ) {
25+
if ( !imgSrc.startsWith('/') ) {
26+
cloudinarySrc = `/${cloudinarySrc}`;
27+
}
28+
cloudinarySrc = `${process.env.DEPLOY_URL}${cloudinarySrc}`;
29+
}
30+
31+
cloudinarySrc = `https://res.cloudinary.com/${CLOUD_NAME}/image/fetch/${cloudinarySrc}`;
32+
33+
$img.setAttribute('src', cloudinarySrc)
34+
35+
await fs.writeFile(page, dom.serialize());
36+
}
37+
}
38+
}
39+
40+
}

0 commit comments

Comments
 (0)