diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..5ecf4d0 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,13 @@ +{ + "name": "html-to-formatted-text", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "striptags": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/striptags/-/striptags-3.1.1.tgz", + "integrity": "sha1-yMPn/db7S7OjKjt1LltePjgJPr0=" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..0d43990 --- /dev/null +++ b/package.json @@ -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 (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" + } +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..fd55554 --- /dev/null +++ b/src/index.js @@ -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, [" "], " ")) + .map(val => val.replace(/\ \;/g, "")) + .map(val => val.replace(/\n\n/g, "\n")) + .map(val => val.replace(/\n$/, ""))[0];