-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const blockPlugin = require('./block') | ||
|
||
module.exports = blockPlugin |