Tired of writing helper functions to manipulate markup? Markup tools is a library that provides some pretty useful transformations for you while being highly configurable.
The idea for a separate module can about after need to write the same helper functions for use on multiple projects, Peer Query, Adom, and Curator, with potentially, even more, coming in the near future.
There are three core utilities of this library.
Builders can be used for markup building including:
- templates using mustache styled placeholders
- hyperlinked hashtags from plain text
- hyperlinked mentions from plain text
- image tags from raw image links
- link tags from raw urls
const mtools = require('markup-tools'); const text = 'hello there @mic, did you join the #omg topic'; const res = mtools.build.hashtags(text, '/hot'); console.log(res); //'hello there @mic, did you join the <a target="_blank" href="/hot/omg "> #omg </a> topic';
Parsers can be used on text to:
- extract image src and image url from text
- extract raw links and ahref sources from text
const mtools = require('markup-tools'); //Call on even slightly healthy text const text = ' mycomputer.home/kofi.png Kofi is going to school. This is a picture of his school: < img alt ' + '="kofi\'s school" src = "[www.ko.fi/sch/img\](http://www.ko.fi/sch/img\)'>. His favorite teacher is called Ama.' ; const school_thumbnail = mtools.parse.images(text, null, null, 'src'); //'www.ko.fi/sch/img'
Formatters can format:
raw links out of text
raw image links out of text
HTML attributes of out text
const mtools = require('markup-tools'); const text = '<b>bold</b> face'; const text = mtools.format.text(text); //'bold face'
Markup tools can be used in both Node.js and web browser.
Install via NPM:
npm install markup-tools --save
To use in the browser, get the dist:
const text = '<b>hello</b>'; const res = mtools.format.text(text); console.log(res); //'hello'
Code is linted with ESLint to enforce a consistent style. We also use Husky to run pre-commit and pre-push tasks as well as build with Travis CI.
There are 10 test suites with a total of 23 Mocha and Chai based tests that cover the entire library's functionalities.
The browser build is built with webpack 4. Contributions is always welcome if you can help improve our regexes, helpers and anything else.
Markup JS is a small library with no dependencies, yet designed to work in both Node.js and the web browser. The entire library is based on REGEX and used it to parse everything, including HTML.
NOT a sanitizer
Another thing worth noting is that markup-tools does NOT sanitize input.
Published on Peer Query - Blockchain-powered p2p collaboration.