Skip to content

Commit a5f053c

Browse files
committed
Send encoded images to Renderer API
1 parent f7f6d41 commit a5f053c

File tree

4 files changed

+602
-70
lines changed

4 files changed

+602
-70
lines changed

commands/storybook.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ async function storybook(url, options) {
5959
}
6060
}
6161
// Capture DoM of every story and send it to renderer API
62-
sendDoM(stories, storybookConfig, options);
62+
sendDoM(url, stories, storybookConfig, options);
6363
})
6464
.catch(function (error) {
6565
if (error.response) {

commands/utils/dom.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
const axios = require('axios');
22
const fs = require('fs');
3+
const path = require('path')
34
const formData = require('form-data');
4-
var { constants } = require('.//constants');
5+
const { JSDOM } = require("jsdom");
6+
var { constants } = require('./constants');
57

6-
async function sendDoM(stories, storybookConfig, options) {
8+
async function sendDoM(storybookUrl, stories, storybookConfig, options) {
79
const createBrowser = require('browserless')
810
const browser = createBrowser()
911

@@ -18,8 +20,16 @@ async function sendDoM(stories, storybookConfig, options) {
1820
const browserless = await browser.createContext()
1921
const html = await browserless.html(storyInfo.url)
2022

23+
dom = new JSDOM(html);
24+
for(element of dom.window.document.querySelectorAll('img')) {
25+
let image = new URL(element.getAttribute('src'), storybookUrl).href;
26+
let format = path.extname(image).replace(/^./, '');
27+
format = format === 'svg' ? 'svg+xml' : format
28+
let imageAsBase64 = await getBase64(image);
29+
element.setAttribute('src', 'data:image/'+format+';base64,'+imageAsBase64);
30+
}
2131
try {
22-
fs.writeFileSync('doms/' + storyId + '.html', html);
32+
fs.writeFileSync('doms/' + storyId + '.html', dom.serialize());
2333
} catch (err) {
2434
console.error(err);
2535
}
@@ -63,4 +73,16 @@ async function sendDoM(stories, storybookConfig, options) {
6373
});
6474
};
6575

76+
function getBase64(url) {
77+
return axios.get(url, {
78+
responseType: "text",
79+
responseEncoding: "base64",
80+
})
81+
.then(response => response.data)
82+
.catch(function (error) {
83+
console.log('[smartui] Error: ', error.message);
84+
process.exit(0);
85+
});
86+
}
87+
6688
module.exports = { sendDoM };

0 commit comments

Comments
 (0)