aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorache <ache@ache.one>2018-05-06 18:51:59 +0200
committerache <ache@ache.one>2018-05-06 18:51:59 +0200
commit6fb1ec956b6ad7074bd1890a2e0fcc7e7f2bb033 (patch)
treed25b6e8ef958bb9b0bd6468ff45805780f8e821b
Init commit
-rw-r--r--index.js59
1 files changed, 59 insertions, 0 deletions
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
+ );
+}
+