diff --git a/lib/token.js b/lib/token.js index 448ff49..54e1a5c 100644 --- a/lib/token.js +++ b/lib/token.js @@ -163,6 +163,20 @@ Token.prototype.attrSet = function attrSet(name, value) { }; +/** + * Token.attrGet(name) + * + * Get the value of attribute `name`, or null if it does not exist. + **/ +Token.prototype.attrGet = function attrGet(name) { + var idx = this.attrIndex(name), value = null; + if (idx >= 0) { + value = this.attrs[idx][1]; + } + return value; +}; + + /** * Token.attrJoin(name, value) * diff --git a/test/misc.js b/test/misc.js index de7297d..fd7ba6d 100644 --- a/test/misc.js +++ b/test/misc.js @@ -357,4 +357,17 @@ describe('Token attributes', function () { '
\n'
);
});
+
+ it('.attrGet', function () {
+ var md = markdownit();
+
+ var tokens = md.parse('```'),
+ t = tokens[0];
+
+ assert.strictEqual(t.attrGet('myattr'), null);
+
+ t.attrSet('myattr', 'myvalue');
+
+ assert.strictEqual(t.attrGet('myattr'), 'myvalue');
+ });
});