Browse Source

Add an attrGet method to Token

To make it easier to write code that consumes tokens for
tasks other than generating HTML.
pull/251/head
Marijn Haverbeke 8 years ago
parent
commit
5837f6bc5b
  1. 14
      lib/token.js
  2. 13
      test/misc.js

14
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)
*

13
test/misc.js

@ -357,4 +357,17 @@ describe('Token attributes', function () {
'<pre><code class="bar"></code></pre>\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');
});
});

Loading…
Cancel
Save