https://github.com/peerquery/markup-tools
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.
Utilities
There are three core utilities of this library.
1. Builders
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
Example:
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 #omg topic';
2. Parsers
Parsers can be used on text to:
- extract image src and image url from text
- extract raw links and ahref sources from text
Example:
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'
3. Formatters
Formatters can format:
raw links out of text
raw image links out of text
HTML attributes of out text
Example
const mtools = require('markup-tools');
const text = 'bold face';
const text = mtools.format.text(text);
//'bold face'
Installation
Markup tools can be used in both Node.js and web browser.
Node.js
Install via NPM:
npm install markup-tools --save
Browser
To use in the browser, get the dist:
https://unpkg.com/markup-tools/dist/markup-tools.min.js
const text = 'hello';
const res = mtools.format.text(text);
console.log(res);
//'hello'
Development
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.
Limitations
REGEX based
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.
Resources
Github repo: https://github.com/peerquery/markup-tools
NPM: https://www.npmjs.com/package/markup-tools
Published on Peer Query - Blockchain-powered p2p collaboration.