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(utils): add text email generation utility methods #65

Conversation

niloysikdar
Copy link
Member

@niloysikdar niloysikdar commented Jul 16, 2022

Requirements:

  • generateTextEmailFromHTML function which will take the HTML string and convert it to plaintext version
  • generateTextEmail function which will accept the JSX Layout and convert that into plaintext format using generateEmail and generateTextEmailFromHTML functions

The Pull Request covers:

  • generateTextEmailFromHTML utility function
  • generateTextEmail function
  • Tests for generateTextEmailFromHTML and generateTextEmail methods

Limitations:

  • Preheader text will also get included in the text version, which shouldn't be the case

Some proposed solutions:

  • We tried using some HOC and complex logic strategy with some props (mode = "text") to handle the functionality of not rendering the text from the Preheader, but it was throwing issues for some cases and moreover, it was only increasing the complexity in case of implementation.

  • We can remove the Preheader manually from our DOM and then generate the text version, and this won't add the preheader text to the final generated text.

    import { DOMParser, XMLSerializer } from "xmldom";
    
    const domparser = new DOMParser();
    const xmlSerializer = new XMLSerializer();
    
    const res = domparser.parseFromString(html, "text/html");
    const pre = res.getElementById("preheader");
    res.removeChild(pre);
    const finalHTML = xmlSerializer.serializeToString(res);
    
  • I've found another library html-to-text that can help us to achieve this goal. We can use the selectors option to pass our custom selector for the Preheader component and then exclude it from the final generated text.

    const text = convert(html, {
      selectors: [{ selector: '.preheader', format: 'skip' }],
    });
    

    But the only problem with this library is that we also need to define our custom selectors and formatters for all h tags to hashify them in the generated text output.


Resolves #60

Signed-off-by: Niloy Sikdar niloysikdar30@gmail.com

Add generateTextEmailFromHTML utility function to convert HTML to plaintext

Resolves #60

Signed-off-by: Niloy Sikdar <niloysikdar30@gmail.com>
Add generateTextEmail utility function to convert JSX Layout to plaintext

Resolves #60

Signed-off-by: Niloy Sikdar <niloysikdar30@gmail.com>
@agentmilindu
Copy link
Contributor

I think you need to have tests for all types of components we have to make sure all the components turn to the correct text version of them.

@niloysikdar
Copy link
Member Author

I think you need to have tests for all types of components we have to make sure all the components turn to the correct text version of them.

Yeah, make sense, doing the changes.

@niloysikdar niloysikdar added the GSoC 2022 Issues and Pull Requests under Google Summer of Code 2022 label Jul 16, 2022
…o feat/60-implement-text-email-generation-utility-based-on-email-templates
@niloysikdar niloysikdar force-pushed the feat/60-implement-text-email-generation-utility-based-on-email-templates branch from 5b88eee to e67a321 Compare July 18, 2022 08:18
Add some more missing tests for all of the components for generateTextEmail method

Resolves #60

Signed-off-by: Niloy Sikdar <niloysikdar30@gmail.com>
@niloysikdar niloysikdar force-pushed the feat/60-implement-text-email-generation-utility-based-on-email-templates branch from e67a321 to 7c61682 Compare July 20, 2022 14:03
@agentmilindu agentmilindu merged commit 740c089 into main Jul 21, 2022
@agentmilindu agentmilindu deleted the feat/60-implement-text-email-generation-utility-based-on-email-templates branch July 21, 2022 05:06
github-actions bot pushed a commit that referenced this pull request Jul 21, 2022
# [1.7.0](v1.6.0...v1.7.0) (2022-07-21)

### Features

* **utils:** add text email generation utility methods ([#65](#65)) ([740c089](740c089)), closes [#60](#60) [#60](#60) [#60](#60)
@github-actions
Copy link

🎉 This PR is included in version 1.7.0 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GSoC 2022 Issues and Pull Requests under Google Summer of Code 2022 released
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement text email generation utility based on Email templates
2 participants