Skip to content

Commit

Permalink
✨ (#2128): add markdown-it-container
Browse files Browse the repository at this point in the history
define info, warn, error and success containers

Signed-off-by: Vinicius Reis <[email protected]>
  • Loading branch information
Vinicius Reis committed Mar 3, 2022
1 parent 946df25 commit 6857f36
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 10 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"highlight.js": "^10.7.2",
"lowlight": "^1.20.0",
"markdown-it": "^12.3.2",
"markdown-it-container": "^3.0.0",
"markdown-it-task-lists": "^2.1.1",
"prosemirror-collab": "^1.2.2",
"prosemirror-inputrules": "^1.1.3",
Expand Down
25 changes: 25 additions & 0 deletions src/markdownit/containers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import container from 'markdown-it-container'

export const typesAvailable = ['info', 'warn', 'error', 'success']

const buildRender = type => (tokens, idx, options, env, slf) => {
// add a class to the opening tag
const tag = tokens[idx]

if (tag.nesting === 1) {
tag.attrSet('data-container', type)
tag.attrJoin('class', `custom-container custom-container-${type}`)
}

return slf.renderToken(tokens, idx, options, env, slf)
}

export default (md) => {
typesAvailable.forEach(type => {
md.use(container, type, {
render: buildRender(type),
})
})

return md
}
2 changes: 2 additions & 0 deletions src/markdownit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import MarkdownIt from 'markdown-it'
import taskLists from 'markdown-it-task-lists'
import underline from './underline'
import splitMixedLists from './splitMixedLists'
import containers from './containers'

const markdownit = MarkdownIt('commonmark', { html: false, breaks: false })
.enable('strikethrough')
.use(taskLists, { enable: true, labelAfter: true })
.use(splitMixedLists)
.use(underline)
.use(containers)

export default markdownit
9 changes: 4 additions & 5 deletions src/nodes/CustomContainer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Node, mergeAttributes } from '@tiptap/core'

export const inputRegex = /^\s*>\s$/
import { typesAvailable } from '../markdownit/containers'

export default Node.create({

Expand All @@ -14,7 +13,7 @@ export default Node.create({

addOptions() {
return {
types: ['info', 'warn', 'error', 'success'],
types: typesAvailable,
HTMLAttributes: {
class: 'custom-container',
},
Expand All @@ -26,10 +25,10 @@ export default Node.create({
type: {
default: 'info',
rendered: false,
parseHTML: element => element.getAttribute('data-type'),
parseHTML: element => element.getAttribute('data-container'),
renderHTML: attributes => {
return {
'data-type': attributes.type,
'data-container': attributes.type,
class: attributes.type,
}
},
Expand Down
28 changes: 23 additions & 5 deletions src/tests/markdownit.spec.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import markdownit from './../markdownit'
import { typesAvailable } from './../markdownit/containers'

describe('markdownit', () => {

it('renders task lists', () => {
const rendered = markdownit.render('* [ ] task\n* not a task')
expect(rendered).toBe(stripIndent(`
expect(stripIndent(rendered)).toBe(stripIndent(`
<ul class="contains-task-list">
<li class="task-list-item"><input class="task-list-item-checkbox" disabled="" type="checkbox"> task</li>
</ul>
<ul>
<li>not a task</li>
</ul>
`))
</ul>`
))
})

it('renders bullet and task lists separately', () => {
const rendered = markdownit.render('* not a task\n* [ ] task')
expect(rendered).toBe(stripIndent(`
expect(stripIndent(rendered)).toBe(stripIndent(`
<ul>
<li>not a task</li>
</ul>
Expand All @@ -26,8 +27,25 @@ describe('markdownit', () => {
`))
})

describe('custom containers', () => {
typesAvailable.forEach((type) => {
it(`render ${type}`, () => {
const rendered = markdownit.render(`::: ${type}\nHey there!\n:::`)
expect(stripIndent(rendered)).toBe(stripIndent(
`<div data-container="${type}" class="custom-container custom-container-${type}">
<p>Hey there!</p>
</div>`
))
})
})
})

})

function stripIndent(content) {
return content.replace(/\t/g, '').replace('\n','')
return content
.replace(/\n/g, "")
.replace(/[\t ]+\</g, "<")
.replace(/\>[\t ]+\</g, "><")
.replace(/\>[\t ]+$/g, ">")
}

0 comments on commit 6857f36

Please sign in to comment.