Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for base64 images #154

Merged
merged 6 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/importer/PageImporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,14 @@ export default class PageImporter {
}

src = img.getAttribute('src');
// try to handle b64 img
if (!src || src.indexOf('data:') === 0) {
// we cannot handle b64 asset for now, remove
img.remove();
const dataUrl = DOMUtils.getDataUrlFromB64Img(img.src);
if (dataUrl) {
img.setAttribute('src', dataUrl);
} else {
img.remove();
}
}

const alt = img.getAttribute('alt');
Expand Down
18 changes: 18 additions & 0 deletions src/utils/DOMUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,22 @@ export default class DOMUtils {
}, interval);
});
}

static getDataUrlFromB64Img(src) {
try {
const arr = src.split(',');
const type = arr[0].split(':')[1];
const b64Str = atob(arr[1]);
const bytesArray = new Uint8Array(b64Str.length);
for (let i = 0; i < b64Str.length; i += 1) {
bytesArray[i] = b64Str.charCodeAt(i);
}
const blob = new Blob([bytesArray], { type });
return URL.createObjectURL(blob);
} catch (e) {
// eslint-disable-next-line no-console
console.error(`get data url from a base64 image (${src}):`, e);
return null;
}
}
}
15 changes: 13 additions & 2 deletions test/importers/PageImporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ describe('PageImporter tests - various options', () => {
});

describe('PageImporter tests - fixtures', () => {
const featureTest = async (feature) => {
const featureTest = async (feature, assertFn) => {
// eslint-disable-next-line no-param-reassign
assertFn = assertFn || ((got, want) => strictEqual(got, want, 'imported md is expected one'));

class Test extends PageImporter {
async fetch() {
const html = await fs.readFile(path.resolve(__dirname, 'fixtures', `${feature}.spec.html`), 'utf-8');
Expand All @@ -183,7 +186,7 @@ describe('PageImporter tests - fixtures', () => {

const md = await storageHandler.get(results[0].md);
const expectedMD = await fs.readFile(path.resolve(__dirname, 'fixtures', `${feature}.spec.md`), 'utf-8');
strictEqual(md.trim(), expectedMD.trim(), 'imported md is expected one');
assertFn(md.trim(), expectedMD.trim(), 'imported md is expected one');

// parse md to verify mdast
const mdast = unified()
Expand Down Expand Up @@ -238,6 +241,14 @@ describe('PageImporter tests - fixtures', () => {
await featureTest('subsup');
});

it('import - images', async () => {
await featureTest('img', (got, want) => {
if (got.indexOf(want) !== 0) {
throw new Error('imported md is not expected one');
}
});
});

it('import - video', async () => {
await featureTest('video');
});
Expand Down
11 changes: 11 additions & 0 deletions test/importers/fixtures/img.spec.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<body>
<h1>Images sample</h1>
<h3>Standard Image - should be imported as is</h3>
<img src="https://www.server.com/image.jpg" alt="empty alt" />
<h3>Image with wrong Base64 data - should be removed</h3>
<img src="data:image/png;base64,--dummy--" alt="empty alt" />
<h3>Image with correct Base64 data - should be transformed into Object Url data</h3>
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="empty alt" />
</body>
</html>
15 changes: 15 additions & 0 deletions test/importers/fixtures/img.spec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Images sample

### Standard Image - should be imported as is

![empty alt][image0]

### Image with wrong Base64 data - should be removed

### Image with correct Base64 data - should be transformed into Object Url data

![empty alt][image1]

[image0]: https://www.server.com/image.jpg

[image1]: blob:nodedata:
19 changes: 19 additions & 0 deletions test/utils/DOMUtils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,3 +432,22 @@ describe('DOMUtils#getImgFromBackground', () => {
test(createElement('p', {}, { 'background-image': 'url( /image.jpg )' }, 'Some content'), '<img src="/image.jpg">');
});
});

describe('DOMUtils#getDataUrlFromB64Img', () => {
const test = (img, expected) => {
const dataUrl = DOMUtils.getDataUrlFromB64Img(img.src);
return expected(dataUrl) === true;
};

it('no data url in original image', () => {
test(createElement('img', { src: 'https://www.server.com/image.jpg' }, {}, ''), (res) => res === null);
});

it('malformed base64 data url in original image', () => {
test(createElement('img', { src: 'data:image/png;base64,--dummy--' }, {}, ''), (res) => res === null);
});

it('base64 data url in original image', () => {
test(createElement('img', { src: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==' }, {}, ''), (res) => res.indexOf('blob:nodedata:') === 0);
});
});