Skip to content

Commit

Permalink
Add an attrGet method to Token
Browse files Browse the repository at this point in the history
To make it easier to write code that consumes tokens for
tasks other than generating HTML.
  • Loading branch information
marijnh committed May 30, 2016
1 parent 564bfa8 commit 5837f6b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/token.js
Expand Up @@ -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)
*
Expand Down
13 changes: 13 additions & 0 deletions test/misc.js
Expand Up @@ -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');
});
});

0 comments on commit 5837f6b

Please sign in to comment.