From c28dcf89053dd2d50a86b3a6e6af34aeccfe0909 Mon Sep 17 00:00:00 2001
From: Ultroncn <82553854+Ultroncn@users.noreply.github.com>
Date: Tue, 17 Nov 2026 21:08:00 +0000
Subject: [PATCH] fix: render brackets literally when a link has no URL (fixes
#105)
'[text]' with neither an inline URL nor a matching reference
definition rendered as 'text'. A link
is now only created when a URL was actually found; otherwise the
brackets are left as literal text, as in CommonMark.
---
src/index.js | 14 ++++++++++++--
test/index.js | 11 +++++++++++
2 files changed, 23 insertions(+), 2 deletions(-)
diff --git a/src/index.js b/src/index.js
index 3938c5c..e7c89df 100644
--- a/src/index.js
+++ b/src/index.js
@@ -82,8 +82,18 @@ export default function parse(md, prevLinks) {
}
// Links:
else if (token[10]) {
- out = out.replace('', ``);
- chunk = flush() + '';
+ let href = token[11] || links[prev.toLowerCase()];
+ if (href) {
+ out = out.replace('', ``);
+ chunk = flush() + '';
+ }
+ else {
+ // No URL (inline or reference): this is not a link,
+ // render the brackets literally instead of href="undefined".
+ let i = out.lastIndexOf('');
+ if (~i) out = out.slice(0, i) + '[' + out.slice(i+3);
+ chunk = ']';
+ }
}
else if (token[9]) {
chunk = '';
diff --git a/test/index.js b/test/index.js
index 5262b16..1320eaa 100644
--- a/test/index.js
+++ b/test/index.js
@@ -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 World!');
});
+
+ it('renders brackets literally when there is no URL', () => {
+ // #105: "[text]" with no inline URL and no reference
+ // definition used to render as .
+ 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('ok and [nope]');
+ expect(snarkdown('[nope] and [ok](http://x.com)')).to.equal('[nope] and ok');
+ });
});
describe('lists', () => {