Skip to content

Commit

Permalink
feat: give access to the original dom, before any preprocessing (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
kptdobe committed Nov 23, 2022
1 parent fa9b4b5 commit 6c394c5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions src/importer/HTML2x.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import Utils from '../utils/Utils.js';

// import docxStylesXML from '../resources/styles.xml';

function preprocessDOM(document) {
function setBackgroundImagesFromCSS(document) {
const elements = document.querySelectorAll('body, header, footer, div, span, section, main');
const getComputedStyle = document.defaultView?.getComputedStyle;
if (getComputedStyle) {
Expand Down Expand Up @@ -76,8 +76,19 @@ async function html2x(
}
}

if (config.preprocess !== false) {
preprocessDOM(doc);
// for more advanced use cases, give access to the original dom with
// no preprocessing at all
if (transformer.preprocess) {
transformer.preprocess({
url,
document: doc,
html: doc.documentElement.outerHTML,
params,
});
}

if (config.setBackgroundImagesFromCSS !== false) {
setBackgroundImagesFromCSS(doc);
}

const html = doc.documentElement.outerHTML;
Expand Down
28 changes: 28 additions & 0 deletions test/importers/HTML2x.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,15 @@ describe('html2x parameters', () => {
await html2md(URL, HTML, {
transformDOM: testParams,
generateDocumentPath: testParams,
preprocess: testParams,
}, null, {
originalURL: ORIGNAL_URL,
});

await html2docx(URL, HTML, {
transformDOM: testParams,
generateDocumentPath: testParams,
preprocess: testParams,
}, null, {
originalURL: ORIGNAL_URL,
});
Expand All @@ -87,12 +89,14 @@ describe('html2x parameters', () => {
it('parameters are correctly passed in multi mode', async () => {
await html2md(URL, HTML, {
transform: testParams,
preprocess: testParams,
}, null, {
originalURL: ORIGNAL_URL,
});

await html2docx(URL, HTML, {
transform: testParams,
preprocess: testParams,
}, null, {
originalURL: ORIGNAL_URL,
});
Expand Down Expand Up @@ -205,6 +209,30 @@ describe('html2md tests', () => {
);
strictEqual(out.html.trim(), '<body><img src="./image.png"></body>');
});

it('html2md removes images with src attributes', async () => {
const out = await html2md('https://www.sample.com/page.html', '<html><body><img src="data:abc"></body></html>');
strictEqual(out.html.trim(), '<body></body>');
strictEqual(out.md.trim(), '');
});

it('html2md set image src with data-src attribute value', async () => {
const out = await html2md('https://www.sample.com/page.html', '<html><body><img src="data:abc" data-src="./image.jpg"></body></html>');
strictEqual(out.html.trim(), '<body><img src="./image.jpg" data-src="./image.jpg"></body>');
strictEqual(out.md.trim(), '![][image0]\n\n[image0]: ./image.jpg');
});

it('html2md allows to preprocess the document', async () => {
const out = await html2md('https://www.sample.com/page.html', '<html><body><img src="data:abc" data-fancy-src="./image.jpg"></body></html>', {
preprocess: ({ document }) => {
const img = document.querySelector('img');
img.setAttribute('src', img.getAttribute('data-fancy-src'));
img.removeAttribute('data-fancy-src');
},
});
strictEqual(out.html.trim(), '<body><img src="./image.jpg"></body>');
strictEqual(out.md.trim(), '![][image0]\n\n[image0]: ./image.jpg');
});
});

describe('html2docx tests', () => {
Expand Down

0 comments on commit 6c394c5

Please sign in to comment.