Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,18 @@ export default function parse(md, prevLinks) {
}
// Links:
else if (token[10]) {
out = out.replace('<a>', `<a href="${encodeAttr(token[11] || links[prev.toLowerCase()])}">`);
chunk = flush() + '</a>';
let href = token[11] || links[prev.toLowerCase()];
if (href) {
out = out.replace('<a>', `<a href="${encodeAttr(href)}">`);
chunk = flush() + '</a>';
}
else {
// No URL (inline or reference): this is not a link,
// render the brackets literally instead of href="undefined".
let i = out.lastIndexOf('<a>');
if (~i) out = out.slice(0, i) + '[' + out.slice(i+3);
chunk = ']';
}
}
else if (token[9]) {
chunk = '<a>';
Expand Down
11 changes: 11 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ describe('snarkdown()', () => {
it('parses reference links without creating excessive linebreaks', () => {
expect(snarkdown('\nhello [World]!\n\n[world]: http://world.com')).to.equal('hello <a href="http://world.com">World</a>!');
});

it('renders brackets literally when there is no URL', () => {
// #105: "[text]" with no inline URL and no reference
// definition used to render as <a href="undefined">.
expect(snarkdown('mSrc [, options ])')).to.equal('mSrc [, options ])');
expect(snarkdown('[missing]')).to.equal('[missing]');
expect(snarkdown('[x]()')).to.equal('[x]()');
expect(snarkdown('[a [b] c]')).to.equal('[a [b] c]');
expect(snarkdown('[ok](http://x.com) and [nope]')).to.equal('<a href="http://x.com">ok</a> and [nope]');
expect(snarkdown('[nope] and [ok](http://x.com)')).to.equal('[nope] and <a href="http://x.com">ok</a>');
});
});

describe('lists', () => {
Expand Down