123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- 'use strict'
- function cell (content, node) {
- var index = Array.prototype.indexOf.call(node.parentNode.childNodes, node)
- var prefix = ' '
- if (index === 0) prefix = '| '
- return prefix + content + ' |'
- }
- var highlightRegEx = /highlight highlight-(\S+)/
- module.exports = [
- {
- filter: 'br',
- replacement: function () {
- return '\n'
- }
- },
- {
- filter: ['del', 's', 'strike'],
- replacement: function (content) {
- return '~~' + content + '~~'
- }
- },
- {
- filter: function (node) {
- return node.type === 'checkbox' && node.parentNode.nodeName === 'LI'
- },
- replacement: function (content, node) {
- return (node.checked ? '[x]' : '[ ]') + ' '
- }
- },
- {
- filter: ['th', 'td'],
- replacement: function (content, node) {
- return cell(content, node)
- }
- },
- {
- filter: 'tr',
- replacement: function (content, node) {
- var borderCells = ''
- var alignMap = { left: ':--', right: '--:', center: ':-:' }
- if (node.parentNode.nodeName === 'THEAD') {
- for (var i = 0; i < node.childNodes.length; i++) {
- var align = node.childNodes[i].attributes.align
- var border = '---'
- if (align) border = alignMap[align.value] || border
- borderCells += cell(border, node.childNodes[i])
- }
- }
- return '\n' + content + (borderCells ? '\n' + borderCells : '')
- }
- },
- {
- filter: 'table',
- replacement: function (content) {
- return '\n\n' + content + '\n\n'
- }
- },
- {
- filter: ['thead', 'tbody', 'tfoot'],
- replacement: function (content) {
- return content
- }
- },
- // Fenced code blocks
- {
- filter: function (node) {
- return node.nodeName === 'PRE' &&
- node.firstChild &&
- node.firstChild.nodeName === 'CODE'
- },
- replacement: function (content, node) {
- return '\n\n```\n' + node.firstChild.textContent + '\n```\n\n'
- }
- },
- // Syntax-highlighted code blocks
- {
- filter: function (node) {
- return node.nodeName === 'PRE' &&
- node.parentNode.nodeName === 'DIV' &&
- highlightRegEx.test(node.parentNode.className)
- },
- replacement: function (content, node) {
- var language = node.parentNode.className.match(highlightRegEx)[1]
- return '\n\n```' + language + '\n' + node.textContent + '\n```\n\n'
- }
- },
- {
- filter: function (node) {
- return node.nodeName === 'DIV' &&
- highlightRegEx.test(node.className)
- },
- replacement: function (content) {
- return '\n\n' + content + '\n\n'
- }
- }
- ]
|