Giter VIP home page Giter VIP logo

tempo-email-parser's Introduction

tempo-email-parser

Parse and extract main message from an HTML email. Also runs several transformations to the email so that it can be displayed safely and correctly inside a browser or Electron iframe for example.

  • Extract quotations (replies), signatures
  • Remove scripts, trackers
  • Convert text links into anchor tags
  • Remove trailing whitespaces
  • Block remote content

Usage

import prepareMessage, {
	blockRemoteContent,
	linkify,
} from 'tempo-email-parser';

const emailHtml = `
<div>Hello there</div>
`;

const remoteContentReplacements = {
	image: 'replacement-image-url', // Remote image URLs replacement. Default to 1x100 transparent image
	other: '#', // Other URLs replacements
};

// All options default to false.
const OPTIONS = {
	noQuotations: true,
	autolink: true,
	forceViewport: '<meta name="viewport" content="width=device-width" />',
	noRemoteContent: true,
	remoteContentReplacements,
	includeStyle: `
		.custom-style {
			color: red;
		}
	`,
};

const {
	// The extracted message
	messageHtml,
	// The whole message processed, including quotations and signature
	completeHtml,
	// Did we removed quotes or signature
	didFindQuotation,
} = prepareMessage(emailHtml, OPTIONS);

Autolinking and remote-content blocking are available as separate functions as well.

const withLinks = linkify(messageHtml);

const noRemoteContent = blockRemoteContent(
	messageHtml,
	remoteContentReplacements
);

Development

For tests

yarn run test

The main function prepareMessage has a list of fixtures used for tests. The input HTML are files named xxx.input.html. The expected outputs are named xxx.output-complete.html and xxx.output-message.html.

yarn run generate:fixtures

This script generates the respective outputs files for any .input.html file found without corresponding outputs.

To easily add a fixture from a real-world email, you can put the input HTML at /src/tests/prepareMessage/my-test.input.html, and then run yarn run generate:fixtures to generate the output files based on what prepareMessage produced. You now only have to check that the outputs look good and make adjustments if necessary.

Benchmarks

There are benchmarks to ensure the tool remains fast to not alter UI performance, and also to compare some external libraries. See the benchmark folder.

tempo-email-parser's People

Contributors

soreine avatar 0nn0 avatar henriquebf avatar leolabs avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.