wildlife-analysis v0.5.13
wildlife-analysis
Create your own markup language.
The markup language is based on start and end tokens, e.g ## I am the content ##
Play with it
https://codepen.io/newfivefour/pen/rQBeem?editors=0010
Usage
Install it
npm install wildlife-analysis
Use it
markedup.mjs
:
import {markupCreator} from 'wildlife-analysis'
const matchers = [
{ markers: ["#", "#"], format: t => `<h1>${markedup(t)}</h1>` },
{ markers: ["*", "[*]"], format: t => `<i>${t}</i>` }
]
const markedup = markupCreator(matchers, t => t)
console.log(markedup(process.argv[2]))
Run it
nodejs --experimental-modules markedup.mjs "#sup in *italics*#there"
<h1>sup in <i>italics</i></h1>there
Usage instructions
The matchers
list, the first parameter to markupCreator
, is where everything happens.
markers
- a list start and ending markers- The first is a normal string
- The second is a regex - if you want to match
*
, then make it[*]
format
- how to format the token- If you've matched
*
and[*]
you can then surround them in<i>
.
- If you've matched
dontSendToEveryTokenFormatter
- the second parameter tomarkupCreator
is a function- With this set to true, you can skip that
- This is useful if most of the time you replace
<
and>
but you don't want to in a<code>
skipChars
- at the end of a token, either skip or go back some characers- if your ending marker is
\n[^*]
you can use-1
here to include whatever after the newline in the next token
- if your ending marker is
The second parameter to markupCreator
is a function that has its parameter as the token text parameter and returns a string.
The third parameter to markupCreator
is like the second, but its parameter is the entire parsed text.
The second and third parameter are for global formatting of either the tokens, replacing HTML characters for example, or the entire text, splitting the text up into <p>
blocks for example.
Usage instructions for matchers
Ordering
Matchers will match in order. So if you want to match both #
and ##
then you must do this:
{ markers: ["##", "##"], format: (t) => `<h2>${t}</h2>` },
{ markers: ["#", "#"], format: (t) => `<h1>${t}</h1>` },
Else #
will match ##
.
Including part of the starting marker
And if your first matcher is https://
and you want to include that in your formatter you need to include it yourself:
{ markers: ["https://", "[)\\s]"], skipChars: -1, format: t => `<a href="https://${t}">https://${t}</a>` },
Example
Here's an example that is parsing all the markdown text on https://newfivefour.com:
https://github.com/newfivefour/wildlife-analysis/blob/master/lib/nearly-markdown.mjs
Limitations
Since we use start and end tokens this is out of scope
Title
===
But doing things like lists is easy enough with:
{ markers: ["\n0. ", "\n[^0]"], format: t => `<br><ol>${t.split("\n0. ").map(s => `<li>${s}</li>`).join("")}</ol>`},
Which parses (note the initial newline)
0. Hiya
0. Sup wit' u?
Testing
npm test
TODO later
- Add tests for nearly markdown
- Make it possible to have a regex in the first matcher
- Make it possible to have a normal string in the second matcher
- Make the tests run with unix/osx - put the babel and mocha stuff in one file