commonmark-helpers
Even the strongest sometimes need help.
Basically, with commonmark-helpers you can find desire elements in markdown document and process them in html or plaintext format.
Bunch of helpers for working with commonmark. Also, you can be interested, why I created this module, that’s why I wrote a post about reasons.
Very important to notice, that this package is very simple and doesn’t contain any smart and/or complicated logic, that’s why it’s tightly coupled with commonmark API for AST tree. Check it out first.
Install
npm install --save commonmark-helpers
Usage
var md = ; mdtextmd; // titlemd; // <p><em>italic</em></p>\nmdtext'**`plaintext`**'; // plaintext { if nodeliteral nodeliteral = nodeliteral; }; mdtextmd; // LOOK\n\nMA
Look into tests for more examples.
API
html(input)
Return html.
input
Type: string
/ AST
text(input)
Return plain text.
input
Type: string
/ AST
ast(input)
Return AST tree for current text.
input
Type: string
match(input, matcher)
Return first AST-node
matched my matcher in process of walking through AST-tree
. Returns undefined
if no one AST-node have been matched.
The most powerful method in this collection.
input
Type: string
/ AST
matcher
Type: function
. Receive: AST-node, event
In most cases you need only AST-node
to match on.
matchRemove(input, matcher)
The same as match()
but remove first matched AST-node and return AST-tree without it.
input
Type: string
/ AST
matcher
Type: function
. Receive: AST-node, event
In most cases you need only AST-node
to match on.
matchRemoveList(input, matcher1, [matcher2, […, matcherN]])
The same as matchRemove()
but resulting AST-tree
after matcher1
passing to matcher2
, result of this to matcher3
and so on. Return AST-tree
without all the matched AST-nodes
.
input
Type: string
/ AST
matcher1, matcher2, …, matcherN
Type: function
. Receive: AST-node, event
In most cases you need only AST-node
to match on.
matchProcess(input, processor)
Match and process AST-nodes
, return modified AST-tree.
input
Type: string
/ AST
processor
Type: function
. Receive: AST-node, event
In most cases you need only AST-node
to match on and modify.
matchProcessList(input, processor1, [processor2, […, processorN]])
The same as matchProcess()
but resulting AST-tree
after processor1
passing to processor2
, result of this to processor3
and so on. Return AST-tree
is AST-tree
with all applied processors.
input
Type: string
/ AST
processor1, processor2, …, processorN
Type: function
. Receive: AST-node, event
In most cases you need only AST-node
to match on and modify.
Bunch of shortcut helpers
const isType = nodetype === type;const isLevel = nodelevel === level;const isText = ;const isEmph = ;const isCode = ;const isHtml = ;const isLink = ;const isItem = ;const isList = ;const isImage = ;const isStrong = ;const isHeader = ;const isDocument = ;const isCodeBlock = ;const isHtmlBlock = ;const isSoftbreak = ;const isHardbreak = ;const isParagraph = ;const isBlockQuote = ;const isHorizontalRule = ; const isRoot = nodeparent && ;const isBreak = || ;
License
MIT © Vladimir Starkov