Skip to content

Commit

Permalink
adds library
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeaus committed Jul 19, 2018
1 parent ebaab19 commit 1d852fd
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "html-to-formatted-text",
"version": "1.0.0",
"description": "Convert hml to formatted text",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/lukeaus/html-to-formatted-text.git"
},
"keywords": [
"html",
"formatted",
"text"
],
"author": "Luke Scott <[email protected]> (http://lukescott.co/)",
"license": "MIT",
"bugs": {
"url": "https://github.com/lukeaus/html-to-formatted-text/issues"
},
"homepage": "https://github.com/lukeaus/html-to-formatted-text#readme",
"dependencies": {
"striptags": "3.1.1"
}
}
27 changes: 27 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const striptags = require("striptags");

const TAGS_TO_BREAK_ON = [
"p",
"div",
"br",
"hr",
"title",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"ol",
"ul",
"li"
];

module.exports = (text = "") =>
[text]
.map(val => striptags(val, TAGS_TO_BREAK_ON))
.map(val => striptags(val, [], "\n"))
.map(val => striptags(val, ["&nbsp;"], " "))
.map(val => val.replace(/\&nbsp\;/g, ""))
.map(val => val.replace(/\n\n/g, "\n"))
.map(val => val.replace(/\n$/, ""))[0];

0 comments on commit 1d852fd

Please sign in to comment.