Skip to content

Commit

Permalink
feat: allow to transform and report back (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
kptdobe committed Jan 5, 2023
1 parent e2d6e50 commit bfeea5d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
15 changes: 12 additions & 3 deletions src/importer/HTML2x.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,15 @@ async function html2x(
results.forEach((result) => {
const name = path.basename(result.path);
const dirname = path.dirname(result.path);

const pir = new PageImporterResource(name, dirname, result.element, null, {
const extra = {
html: result.element.outerHTML,
});
};

if (result.report) {
extra.report = result.report;
}

const pir = new PageImporterResource(name, dirname, result.element, null, extra);
pirs.push(pir);
});
return pirs;
Expand Down Expand Up @@ -181,6 +186,10 @@ async function html2x(
html: pir.extra.html,
};

if (pir.extra.report) {
res.report = pir.extra.report;
}

res.path = path.resolve(pir.directory, pir.name);

if (config.toMd) {
Expand Down
58 changes: 56 additions & 2 deletions test/importers/HTML2x.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* governing permissions and limitations under the License.
*/

import { ok, strictEqual } from 'assert';
import { deepStrictEqual, ok, strictEqual } from 'assert';
import { describe, it } from 'mocha';
import { JSDOM } from 'jsdom';
import { docx2md } from '@adobe/helix-docx2md';
Expand Down Expand Up @@ -155,7 +155,7 @@ describe('html2md tests', () => {
strictEqual(out2.path, '/folder/my-custom-path-p2');
});

it('html2md handles multiple transform', async () => {
it('html2md handles multiple transform (but single output)', async () => {
const out = await html2md('https://www.sample.com/page.html', '<html><body><h1>Hello World</h1></body></html>', {
transform: ({ document }) => {
const p1 = document.createElement('p');
Expand All @@ -176,6 +176,60 @@ describe('html2md tests', () => {
strictEqual(out.path, '/my-custom-path-p1');
});

it('html2md allows to report when using transform', async () => {
const out = await html2md('https://www.sample.com/page.html', '<html><body><h1>Hello World</h1></body></html>', {
transform: ({ document }) => {
const p1 = document.createElement('p');
p1.innerHTML = 'My Hello to the World 1';

const p2 = document.createElement('p');
p2.innerHTML = 'My Hello to the World 2';

return [{
element: p1,
path: '/my-custom-path-p1',
report: {
custom: 'A custom property',
customArray: ['a', 'b', 'c'],
customObject: {
a: 1,
b: true,
c: {
d: 'e',
},
},
},
}, {
element: p2,
path: '/folder/my-custom-path-p2',
report: {
custom: 'Another value',
customArray: ['a', 'b', 'c'],
somethingElse: 'something else',
},
}];
},
});

const out1 = out[0];
strictEqual(out1.html.trim(), '<p>My Hello to the World 1</p>');
strictEqual(out1.md.trim(), 'My Hello to the World 1');
strictEqual(out1.path, '/my-custom-path-p1');
ok(out1.report);
strictEqual(out1.report.custom, 'A custom property');
deepStrictEqual(out1.report.customArray, ['a', 'b', 'c']);
deepStrictEqual(out1.report.customObject, { a: 1, b: true, c: { d: 'e' } });

const out2 = out[1];
strictEqual(out2.html.trim(), '<p>My Hello to the World 2</p>');
strictEqual(out2.md.trim(), 'My Hello to the World 2');
strictEqual(out2.path, '/folder/my-custom-path-p2');
ok(out2.report);
strictEqual(out2.report.custom, 'Another value');
deepStrictEqual(out2.report.customArray, ['a', 'b', 'c']);
strictEqual(out2.report.somethingElse, 'something else');
});

it('html2md does not crash if transform returns null', async () => {
const out = await html2md('https://www.sample.com/page.html', '<html><body><h1>Hello World</h1></body></html>', {
transform: () => null,
Expand Down

0 comments on commit bfeea5d

Please sign in to comment.