https://github.com/reimertz/lit-markdown
es6 literal markdown parser
https://github.com/reimertz/lit-markdown
es6 literal markdown
Last synced: 3 months ago
JSON representation
es6 literal markdown parser
- Host: GitHub
- URL: https://github.com/reimertz/lit-markdown
- Owner: reimertz
- Created: 2017-08-28T13:37:58.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-09-21T12:52:17.000Z (over 7 years ago)
- Last Synced: 2025-03-15T01:47:14.269Z (3 months ago)
- Topics: es6, literal, markdown
- Size: 2.93 KB
- Stars: 1
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# lit-markdown
es6 literal markdown parser## Code
```
function markdown(strings, ...values) {
function para(text, line) {
const trimmed = line.trim()
if (/^<\/?(ul|ol|li|h|p|bl)/i.test(trimmed)) {
return `\n${line}\n`
}
return `\n${trimmed}
\n`
}function ulList(text, item) {
return `\n
- \n\t
- ${item.trim()} \n
}
function olList(text, item) {
return `\n
- \n\t
- ${item.trim()} \n
}
function blockquote(text, tmp, item) {
return `\n
${item.trim()}`
}
function header(text, chars, content) {
const level = chars.length
return `${content.trim()}`
}
return [
{ regex: /(#+)(.*)/g, replacement: header }, // headers
{ regex: /!\[([^\[]+)\]\(([^\)]+)\)/g, replacement: "" }, // image
{ regex: /\[([^\[]+)\]\(([^\)]+)\)/g, replacement: "$1" }, // hyperlink
{ regex: /(\*\*|__)(.*?)\1/g, replacement: '$2' }, // bold
{ regex: /(\*|_)(.*?)\1/g, replacement: '$2' }, // emphasis
{ regex: /\~\~(.*?)\~\~/g, replacement: '$1' }, // del
{ regex: /\:\"(.*?)\"\:/g, replacement: '$1' }, // quote
{ regex: /`(.*?)`/g, replacement: '$1
' }, // inline code
{ regex: /\n\*(.*)/g, replacement: ulList }, // ul lists
{ regex: /\n[0-9]+\.(.*)/g, replacement: olList }, // ol lists
{ regex: /\n(>|\>)(.*)/g, replacement: blockquote }, // blockquotes
{ regex: /\n-{5,}/g, replacement: '\n
' }, // horizontal rule
{ regex: /\n([^\n]+)\n/g, replacement: para }, // add paragraphs
{ regex: /<\/ul>\s?
- /g, replacement: '' }, // fix extra ul
{ regex: /<\/ol>\s?
- /g, replacement: '' }, // fix extra ol
{ regex: /<\/blockquote>
/g, replacement: '\n' } // fix extra blockquote
].reduce((text, rule) => text.replace(rule.regex, rule.replacement),
(Array.isArray(strings) ? strings : [strings])
.map((part, index) => `${part}${values[index] ? values[index] : ''}`)
.join(''))
}
```## usage
```
markdown`
# HeaderThis is a test.`
```