Giter VIP home page Giter VIP logo

textversionjs's Introduction

textversionjs

Generate the text version of your HTML email in a second.

This tool is an open source project. Feel free to use it any time in your projects!

htmlToPlainText

The function that generates plain text from email htmls.

Params

Param Type Required Default value Description
htmlText string Yes The html version of the email
styleConfig json/javascript object No Options for converting

styleConfig

Param Type Required Default value Description
linkProcess function No Callback function to customize links appearance
imgProcess function No Callback function to customize image appearance
headingStyle string No "underline" Define heading appearance, options: "underline", "linebreak", "hashify"
listStyle string No "indention" Define list appearance, options: "indention", "linebreak"
uIndentionChar string No "-" If listStyle is indention, uIndentionChar is the character that fills the indention for unordered lists
oIndentionChar string No "-" If listStyle is indention, oIndentionChar is the character that fills the indention for ordered lists after the heading number
listIndentionTabs int No 3 If listStyle is indention, listIndentionTabs is the width of the indention
keepNbsps boolean No false Define the behaviour of the non-braking spaces. If set to true, nbsps are not collapsed to single space.

linkProcess

Param Type Required Default value Description
href string Yes The destination (href property) of the link
linkText string Yes The text of the link

imgProcess

Param Type Required Default value Description
src string Yes The source (src property) of the image
alt string Yes The alternative text (alt property) of the image

Examples

Simple conversion with default style

var textVersion = require("textversionjs");
var htmlText = "<html>" +
					"<body>" +
						"Lorem ipsum <a href=\"http://foo.foo\">dolor</a> sic <strong>amet</strong><br />" +
						"Lorem ipsum <img src=\"http://foo.jpg\" alt=\"foo\" /> sic <pre>amet</pre>" +
						"<p>Lorem ipsum dolor <br /> sic amet</p>" +
						"<script>" +
							"alert(\"nothing\");" +
						"</script>" +
					"</body>" +
				"</html>";

var plainText = textVersion(htmlText);
// returns
// "Lorem ipsum [dolor] (http://foo.foo) sic amet
// Lorem ipsum ![foo] (http://foo.jpg) sic amet
// Lorem ipsum dolor
// sic amet"

Customize link appearance

var textVersion = require("textversionjs");
var htmlText = "<p>Lorem <a href=\"http://foo.foo\">ipsum</a> dolor sic amet</p>";

var styleConfig = {
	linkProcess: function(href, linkText){
		return linkText + " " + "(" + href + ")";
	}
};

var plainText = textVersion(htmlText, styleConfig);
// returns "Lorem ipsum (http://foo.foo) dolor sic amet"

Customize headings

var textVersion = require("textversionjs");

var htmlText = "<h1>Lorem ipsum</h1>" +
				"<p>Lorem ipsum dolor sic amet</p>";

var styleConfig = {
	headingStyle: "hashify"
};

var plainText = textVersion(htmlText, styleConfig);
// returns
// "# Lorem ipsum
//
// Lorem ipsum dolor sic amet"

Customize lists

var textVersion = require("textversionjs");
var htmlText = "<ul>" +
					"<li>Lorem</li>" +
					"<li>ipsum</li>" +
				"</ul>" +
				"<ol>" +
					"<li start=\"3\">Lorem</li>" +
					"<li>ipsum</li>" +
				"</ol>";

var styleConfig = {
	headingStyle: "indention",
	uIndentionChar: ".";
	listIndentionTabs: 2;
};

var plainText = textVersion(htmlText, styleConfig);
// returns "
// ..Lorem
// ..ipsum
// 3.Lorem
// 4.ipsum"

Try it online in our Demo page!

Don't forget to check out our other open source projects at EDMdesigner.

Follow us on github and twitter!

textversionjs's People

Contributors

anikoborosova avatar gyulanemeth avatar nunosempere avatar regevbr avatar sharpensteel avatar smiska avatar stubendek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

textversionjs's Issues

TypeError: Cannot read property '1' of null

Hi, there is an error if <ol> have attribute start with value greater then one symbol:

var createTextVersion = require('textversionjs');
createTextVersion('<ol start="10">test</ol>');
TypeError: Cannot read property '1' of null
    at textversion.js:86

i tried to create merge request: #6

License?

Can you please provide the License? It is not usable in any project without it

I assume MIT?

the regex used to remove all text that is not included in the <body> element causes node to get stuck

In the case the html is very long and doesnt include a body element (yes it can happen to me) the regex used causes the node js process to get stuck on 100% cpu for a long time.
Instead we can use a better approach - look for the index of the body element (without using the * matcher) and then use substring to cut out the desired text.
E.g.

	// remove everything before and after <body> tags including the tag itself
	const bodyEndMatch = tmp.match(/<\/body>/i);
	if (bodyEndMatch) {
		tmp = tmp.substring(0, bodyEndMatch.index);
	}
	const bodyStartMatch = tmp.match(/<body[^>]*>/i);
	if (bodyStartMatch) {
		tmp = tmp.substring(bodyStartMatch.index + bodyStartMatch[0].length, tmp.length);
	}

Tables?

Hey

Nice utility this, saved me some time, thanks!

Have you ever thought about whether you could generate a text version of tables? Like, using the same sort of methodology you use for headings and lists?

cheers

Tom

Include other attributes in link formatting

Currently, the link only passes on the href and linkText. I also need to include the link name and a custom attribute called xt and it's value.

I've tried modifying the code but had no luck at all.

Here is an example of my link HTML output:
<a data-mce-href="http://1.com" href="http://1.com" target="_blank" class="SPCLICK" name="me" rel="noopener" data-mce-selected="inline-boundary" xt="SPCLICK">ipsum</a>

The output needs to appear like this:
linklText (Link: %%HYPERLINK:linkName_txt#xtValue"#hrefValue#%%) ")

Any thoughts? Your assistance would be greatly appreciated
-Michael

Paste from Word or Google Docs: some tags are not removed

Hi!

I've faced a little problem with textversionjs while pasting text\html from word or google docs.
Some tags are not unsignificant tags are not replaced from text.

  1. Paste from google docs
    html:

line 1

line 2

line 3


text:

line 1
line 2
line 3

  1. paste from Word
    html:

Line 1

Line 2

Line 3

text:
Line 1**<o:p></o:p>**
Line 2**<o:p></o:p>**
Line 3**<o:p></o:p>**

Probably, the source of this error in this line of code:
tmp = tmp.replace(/<((?!h1-6>)(?!img( [^>])>)(?!a( [^>])>)(?!ul( [^>])>)(?!ol( [^>])>)(?!li( [^>])>)(?!p( [^>])>)(?!div( [^>])>)(?!td( [^>])>)(?!br( [^>])>)[^>])>/gi, "");

Tags, that ends up with "a ", "p " are not replaced. Even if any attribute contains "a ", "p ", "br " - this tags are not removed:
<span test="a " lang=EN-US style='font-size:10.5pt;line-height:107%; font-family:"Arial",sans-serif;color:#333333;mso-ansi-language:EN-US'>

I've tried the regex below, it seems to work fine:
tmp = tmp.replace(/<(/)?(?!h1-6>)(?!img( [^>\/])>)(?!a( [^>\/])>)(?!ul( [^\/>])>)(?!ol( [^>\/])>)(?!li( [^>\/])>)(?!p( [^>\/])>)(?!div( [^>\/])>)(?!td( [^>\/])>)(?!br( [^>\/])>)[^>\/]>/gi, "");
It should check exectly the full name of tag.

Will you please check my suggestion and fix this defect?

Regards, Yuri

Not able to run in strict mode

In strict mode (node --use_strict), the following code creates a TypeError.

const textVersion = require('textversionjs')
TypeError: Cannot read property 'define' of undefined
    at C:\Users\samuel\Code\node\telegram\openshift-dev\node_modules\textversionjs\src\textversion.js:176:17
    at Object.<anonymous> (C:\Users\samuel\Code\node\telegram\openshift-dev\node_modules\textversionjs\src\textversion.js:190:3)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
    at Module.require (module.js:468:17)
    at require (internal/module.js:20:19)
    at repl:1:21

while executing the same code without --use_strict works just fine.

Versions:

node v6.3.1
npm v3.10.3
textversionjs v1.0.0

running on Windows 10

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.