Skip to content

Commit

Permalink
Merge 565357a into 8886347
Browse files Browse the repository at this point in the history
  • Loading branch information
vhf committed Mar 19, 2017
2 parents 8886347 + 565357a commit 0171fad
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const remark2rehype = require('remark-rehype')
const htmlBlocks = require('./packages/html-blocks')
const escapeEscaped = require('./packages/escape-escaped')
const kbd = require('./packages/kbd')
const customBlocks = require('./packages/custom-blocks')

const fromFile = (filepath) => fs.readFileSync(filepath)
const logO = (...xs) => // eslint-disable-line no-unused-vars
Expand All @@ -27,6 +28,10 @@ const render = (zmd) => {
blocks: [],
})
.use(remark2rehype, { allowDangerousHTML: true })
.use(customBlocks({
secret: 'secret',
s: 'secret',
}))
.use(htmlBlocks)
.use(escapeEscaped())
.use(kbd)
Expand Down
76 changes: 76 additions & 0 deletions packages/custom-blocks/block.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
function escapeRegExp (str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&') // eslint-disable-line no-useless-escape
}

const C_NEWLINE = '\n'
const C_FENCE = '|'

module.exports = (blocks) => function blockPlugin (opts = {}) {
const pattern = Object
.keys(blocks)
.map(escapeRegExp)
.join('|')
const regex = new RegExp(`\\[\\[(${pattern})\\]\\]`)

function blockTokenizer (eat, value, silent) {
const now = eat.now()
const keep = regex.exec(value)
if (!keep) return
// if (keep.index !== 0) return

if (value.indexOf('[[') !== 0) {
return
}

const linesToEat = []
const content = []

let idx = 0
while ((idx = value.indexOf(C_NEWLINE)) !== -1) {
const next = value.indexOf(C_NEWLINE, idx + 1)
// either slice until next NEWLINE or slice until end of string
const lineToEat = next !== -1 ? value.slice(idx + 1, next) : value.slice(idx + 1)
if (lineToEat[0] !== C_FENCE) break
// remove leading `FENCE ` or leading `FENCE`
const line = lineToEat.slice(lineToEat.startsWith(`${C_FENCE} `) ? 2 : 1)
linesToEat.push(lineToEat)
content.push(line)
value = value.slice(idx + 1)
}

const contentString = content.join(C_NEWLINE)
const stringToEat = `${keep[0]}${C_NEWLINE}${linesToEat.join(C_NEWLINE)}`

const add = eat(stringToEat)
const exit = this.enterBlock()
const contents = this.tokenizeBlock(contentString, now)
exit()

return add({
type: 'custom',
children: contents,
data: {
hName: 'div',
hProperties: {
className: blocks[keep[1]]
}
}
})
}

const Parser = this.Parser

// Inject blockTokenizer
const blockTokenizers = Parser.prototype.blockTokenizers
const blockMethods = Parser.prototype.blockMethods
blockTokenizers.custom_blocks = blockTokenizer
blockMethods.splice(blockMethods.indexOf('fencedCode') + 1, 0, 'custom_blocks')

// Inject into interrupt rules
const interruptParagraph = Parser.prototype.interruptParagraph
const interruptList = Parser.prototype.interruptList
const interruptBlockquote = Parser.prototype.interruptBlockquote
interruptParagraph.splice(interruptParagraph.indexOf('fencedCode') + 1, 0, ['custom_blocks'])
interruptList.splice(interruptList.indexOf('fencedCode') + 1, 0, ['custom_blocks'])
interruptBlockquote.splice(interruptBlockquote.indexOf('fencedCode') + 1, 0, ['custom_blocks'])
}
3 changes: 3 additions & 0 deletions packages/custom-blocks/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const blockPlugin = require('./block')

module.exports = blockPlugin

0 comments on commit 0171fad

Please sign in to comment.