From 6fb1ec956b6ad7074bd1890a2e0fcc7e7f2bb033 Mon Sep 17 00:00:00 2001 From: ache Date: Sun, 6 May 2018 18:51:59 +0200 Subject: Init commit --- index.js | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 index.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..6eb862d --- /dev/null +++ b/index.js @@ -0,0 +1,59 @@ +'use strict'; + +var parseAttr = require('md-attr-parser'); + +module.exports = linkAttr; + +function linkAttr() { + var parser = this.Parser; + var tokenizers; + + if (!isRemarkParser(parser)) { + throw new Error('Missing parser to attach `remark-attr` [link] (to)'); + } + + tokenizers = parser.prototype.inlineTokenizers; + + linkTokenize.locator = tokenizers.link.locator; + let oldLink = tokenizers.link; + + function linkTokenize(eat, value, silent) { + let linkEaten = oldLink.bind(this)(eat,value,silent); + + var self = this; + var index = 0; + var pedantic = self.options.pedantic; + var commonmark = self.options.commonmark; + var gfm = self.options.gfm; + var parsedAttr; + const length = value.length; + + if( !linkEaten || !linkEaten.position ) { + return undefined; + } + index = linkEaten.position.end.offset - linkEaten.position.start.offset; + + if (index < length && value.charAt(index) === '{' ) { + parsedAttr = parseAttr(value, index); + } + + if (parsedAttr) { + linkEaten.data = {hProperties: parsedAttr.prop}; + linkEaten = eat(parsedAttr.eaten)(linkEaten); + } + return linkEaten; + } + + tokenizers.link = linkTokenize; +} + +function isRemarkParser(parser) { + return Boolean( + parser && + parser.prototype && + parser.prototype.inlineTokenizers && + parser.prototype.inlineTokenizers.link && + parser.prototype.inlineTokenizers.link.locator + ); +} + -- cgit v1.2.3