Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support boolean attributes #652

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/common/utils.js
Expand Up @@ -2,6 +2,7 @@
//
'use strict';

function isNil(v) { return v === null || typeof v === 'undefined'; }

function _class(obj) { return Object.prototype.toString.call(obj); }

Expand Down Expand Up @@ -299,6 +300,7 @@ exports.lib = {};
exports.lib.mdurl = require('mdurl');
exports.lib.ucmicro = require('uc.micro');

exports.isNil = isNil;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not good idea to spread micro helpers as lib feature, when used in only one place. Such things clutter api significantly in long term.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was trying to follow the existing organization.

Where do you want it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Until "helper" is not used wide, it should be in the same file where it's needed. Preferably - inlined, without separate fn at all.

exports.assign = assign;
exports.isString = isString;
exports.has = has;
Expand Down
4 changes: 3 additions & 1 deletion lib/renderer.js
Expand Up @@ -11,6 +11,7 @@
var assign = require('./common/utils').assign;
var unescapeAll = require('./common/utils').unescapeAll;
var escapeHtml = require('./common/utils').escapeHtml;
var isNil = require('./common/utils').isNil;


////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -174,7 +175,8 @@ Renderer.prototype.renderAttrs = function renderAttrs(token) {
result = '';

for (i = 0, l = token.attrs.length; i < l; i++) {
result += ' ' + escapeHtml(token.attrs[i][0]) + '="' + escapeHtml(token.attrs[i][1]) + '"';
var value = token.attrs[i][1];
result += ' ' + escapeHtml(token.attrs[i][0]) + (isNil(value) ? '' : '="' + escapeHtml(value) + '"');
}

return result;
Expand Down
7 changes: 7 additions & 0 deletions test/misc.js
Expand Up @@ -382,6 +382,13 @@ describe('Token attributes', function () {
md.renderer.render(tokens, md.options),
'<pre><code class="bar"></code></pre>\n'
);

t.attrSet('hidden');

assert.strictEqual(
md.renderer.render(tokens, md.options),
'<pre><code class="bar" hidden></code></pre>\n'
);
});

it('.attrGet', function () {
Expand Down