Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bates64/mrk
🗒️ Tiny extensible markdown parser/renderer single-file JS library
https://github.com/bates64/mrk
javascript js markdown markdown-parser
Last synced: 2 months ago
JSON representation
🗒️ Tiny extensible markdown parser/renderer single-file JS library
- Host: GitHub
- URL: https://github.com/bates64/mrk
- Owner: bates64
- License: mit
- Created: 2017-12-18T14:51:35.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2022-12-21T23:03:29.000Z (about 2 years ago)
- Last Synced: 2024-09-19T19:11:15.446Z (3 months ago)
- Topics: javascript, js, markdown, markdown-parser
- Language: JavaScript
- Homepage: https://mrk.nanaian.town
- Size: 29.3 KB
- Stars: 16
- Watchers: 4
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
🗒️ mrk
Tiny extensible markdown renderermrk is a tiny, easily-extensible markdown parser created for [Decent](https://github.com/decent-chat/decent). We needed it
for a few reasons:* We didn't want to have to bring in an **npm** module or do anything fancy to use it
* We needed to be able to **disable features** and add new ones easily
* We wanted access to the parsed **token stream**
* We wanted something small, simple, and **easy to extend**### How do I use it?
Include [mrk.js](mrk.js) in your page, or `npm install mrkjs`:
Usage is as follows:```js
// require('mrk.js') orlet mark = mrk()
let html = mark('Some _markdown_').html()
console.log(html)
```That's it! You can also directly access the parsed token stream by looking at `mark(...).tokens`, if you're so inclined.
### Can I haz `Promise`?
Yes!!
```js
const mrk = require('mrk.js/async')
const mark = mrk()mark('Awesome!').then(parsed => {
console.log('tokens:', parsed.tokens)return parsed.html()
}).then(html => {
console.log('HTML:', html)
})
```### I want/to remove Feature X!
You can implement/remove it yourself. mrk was designed to be easily extendable:
#### Removing a feature
```js
mark('Visit http://google.com').html() // Visit http://google.comdelete mark.patterns.autolink // See mrk.js for other patterns/features you can remove
mark('Visit http://google.com').html() // Visit http://google.com
```#### Adding a new parse rule
Say we wanted to add `~~strikethrough~~` text, GFM-style:
```js
// Pass `mrk()` extensions to patterns, pairs, or htmlify
const markWithStrikethrough = mrk({
extendPatterns: {
strikethroughStart: ({ read, has }) => {
// If this function returns a truthy value, it will be parsed as a strikethroughStart token
// See mrk.js for how `read` and `has` work, plus other functions you get access to.return read(2) === '~~' // Next 2 characters should be `~~`
&& !has('strikethroughStart', 'strikethroughEnd') // Not already strikethrough!
},strikethroughEnd: ({ read, has }) => {
return read(2) === '~~' // Next 2 characters should be `~~`
&& has('strikethroughStart', 'strikethroughEnd') // Must have a strikethroughStart before this token
},
},extendPairs: {
// If there is a strikethroughStart token on its own without a strikethroughEnd token to be paired
// to, it will be discarded and parsed as text.
strikethroughStart: 'strikethroughEnd'
},extendHtmlify: {
// Declares how to convert these tokens into HTML strings.
strikethroughStart = () => '',
strikethroughEnd = () => ''
}
})// :tada:
mrk('~~hello~~').html() // => hello
```If you end up creating an extension for mrk for your project and it's generic enough,
[please consider adding it to the list](https://github.com/nanaian/mrk/wiki) of
community extensions for mrk. I'd appreciate it!### License
MIT :tada: