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

feature: HTML Transformation #122

Open
maciek-ibm opened this issue Apr 19, 2022 · 0 comments
Open

feature: HTML Transformation #122

maciek-ibm opened this issue Apr 19, 2022 · 0 comments
Labels

Comments

@maciek-ibm
Copy link

maciek-ibm commented Apr 19, 2022

Problem:

Sometimes there is a need to transform HTML just before generating the output in md-to-pdf, i.e. we need to convert images to Base64, sanitize links, add PagedJS, add some JavaScript.

Solution:

We achieved it by modifying a part of code in md-to-pdf like this:

let html = getHtml(md, config);
	if (config.transform_html) {
		html = await config.transform_html(html);
	}

Where transform_html is a custom function, i.e.:

const { JSDOM } = require('jsdom');
    const { window } = new JSDOM('<!DOCTYPE html>');
    if (!config.req) throw new TypeError(`config.req not defined!`);
    // NOTE: embeds images as base64
    const embedImages = new EmbedHTMLImages({ req: config.req });
    const sanitizedLinksHtml = replaceTextContentWithHref(
      html,
      window.DOMParser,
      true
    );

return await embedImages.run(sanitizedLinksHtml);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant