# Links and "\]" in code spans

**URL:** <https://talk.commonmark.org/t/links-and-in-code-spans/3807>\
**Category:** Spec\
**Created:** [March 3, 2021, 6:46pm UTC](https://talk.commonmark.org/t/links-and-in-code-spans/3807 "2021-03-03T18:46:03Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sergey\_Tkachenko](https://talk.commonmark.org/user_avatar/talk.commonmark.org/sergey_tkachenko/32/2185_2.png) [@Sergey\_Tkachenko](https://talk.commonmark.org/u/Sergey_Tkachenko)\
**Post date:** [March 3, 2021, 6:46pm UTC](https://talk.commonmark.org/t/links-and-in-code-spans/3807/1 "2021-03-03T18:46:03Z")

</div>

In the following code sample, I assume that [ref] becomes a hyperlink (shortcut reference link).  
However, (in commonmark.js dingus) it does not.

```
[ref][sometext`code]`

[ref]: ddd

```

If I remove “]” from code, it works as expected.  
May it be a bug? I believe code span content must not affect link definitions.

---

<div class="post-metadata">

**Author:** ![Sergey\_Tkachenko](https://talk.commonmark.org/user_avatar/talk.commonmark.org/sergey_tkachenko/32/2185_2.png) [@Sergey\_Tkachenko](https://talk.commonmark.org/u/Sergey_Tkachenko)\
**Post date:** [March 4, 2021, 9:13am UTC](https://talk.commonmark.org/t/links-and-in-code-spans/3807/2 "2021-03-04T09:13:55Z")

</div>

I re-read the spec about links and code spans, and I think it is unclear about a precedence, and it does not include code samples on this topic.

It explains that code spans have higher priority than links.  
However, as I can see in commonmark.js dingus, it is true only for link text.  
For inline links, link target and link title have higher priority than code spans:

```
[link](`fo)o`

```

In this example, this is a link + “o`” text, not code span. This is not mentioned in the spec (if I am wrong, please correct me).

And for reference link, intersection with code spans is not processed correctly, as it is shown in my previous message.

---

<div class="post-metadata">

**Author:** ![vas](https://talk.commonmark.org/user_avatar/talk.commonmark.org/vas/32/1267_2.png) [@vas](https://talk.commonmark.org/u/vas)\
**Post date:** [March 4, 2021, 9:33am UTC](https://talk.commonmark.org/t/links-and-in-code-spans/3807/3 "2021-03-04T09:33:21Z")

</div>

It’s clearly a bug. [Compare](https://spec.commonmark.org/dingus/?text=%5Bref%5D%5Bsometext%20code%60%5D%60%0A%0A%5Bref%5D%3A%20ddd):

```auto
[ref][sometext code`]`

[ref]: ddd

```

[with](https://spec.commonmark.org/dingus/?text=%5Bref%5D%5Bsometext%20code%0A%0A%5Bref%5D%3A%20ddd):

```auto
[ref][sometext code

[ref]: ddd

```

Also, note that some CommonMark parsers, including MD4C and Markdown-it, [parse it correctly](https://johnmacfarlane.net/babelmark2/?normalize=1&text=%5Bref%5D%5Bsometext+code%60%5D%60%0A%0A%5Bref%5D%3A+ddd).

---

<div class="post-metadata">

**Author:** ![jgm](https://talk.commonmark.org/user_avatar/talk.commonmark.org/jgm/32/1360_2.png) [@jgm](https://talk.commonmark.org/u/jgm)\
**Post date:** [March 8, 2021, 5:41pm UTC](https://talk.commonmark.org/t/links-and-in-code-spans/3807/4 "2021-03-08T17:41:12Z")

</div>

No, I don’t think it’s a bug. Have a look at the definition of “link label”:

> A link label begins with a left bracket (`[`) and ends with the first right bracket (`]`) that is not backslash-escaped. Between these brackets there must be at least one non-whitespace character. Unescaped square bracket characters are not allowed inside the opening and closing square brackets of link labels. A link label can have at most 999 characters inside the square brackets.

By this definition

```
[sometext code`]

```

is a link label.

---

<div class="post-metadata">

**Author:** ![Sergey\_Tkachenko](https://talk.commonmark.org/user_avatar/talk.commonmark.org/sergey_tkachenko/32/2185_2.png) [@Sergey\_Tkachenko](https://talk.commonmark.org/u/Sergey_Tkachenko)\
**Post date:** [March 8, 2021, 7:05pm UTC](https://talk.commonmark.org/t/links-and-in-code-spans/3807/5 "2021-03-08T19:05:52Z")

</div>

[sometext`code] would be a valid link if this label was defined.  
In this code, a valid link is inserted:

```
[text][sometext`code]`
[sometext`code]: eee

```

(I was wrong in my second post, “`” has lower priority than “]” in a link label, so it is consistent with preferences of link target and title).

But the bug in my initial example is different.  
It is converted to:

```
<p>[ref][sometext<code>code]</code></p>

```

But it should be converted to:

```
<p><a href="ddd">ref</a>[sometext<code>code]</code></p>

```

---

<div class="post-metadata">

**Author:** ![jgm](https://talk.commonmark.org/user_avatar/talk.commonmark.org/jgm/32/1360_2.png) [@jgm](https://talk.commonmark.org/u/jgm)\
**Post date:** [March 9, 2021, 1:50am UTC](https://talk.commonmark.org/t/links-and-in-code-spans/3807/6 "2021-03-09T01:50:17Z")

</div>

Your first case was:

> [@Sergey\_Tkachenko](#):
>
> ```auto
> [ref][sometext`code]`
> 
> [ref]: ddd
> 
> ```

Here we have bracketed `ref` followed by a link label `[sometext `code]`.  
Since that label does not have a definition, this is not a link.  
You might think, shouldn’t it be a **shortcut reference link** to `ddd` followed by plain text `[sometext` and then code `code]`? This is ruled out by the spec:

> A [shortcut reference link](https://spec.commonmark.org/0.29/#shortcut-reference-link) consists of a [link label](https://spec.commonmark.org/0.29/#link-label) that [matches](https://spec.commonmark.org/0.29/#matches) a [link reference definition](https://spec.commonmark.org/0.29/#link-reference-definition) elsewhere in the document **and is not followed by `[]` or a link label.**

Since `[sometext `code]` meets the condition for being a link label, this isn’t a shortcut reference link. Does that make sense?

---

<div class="post-metadata">

**Author:** ![Sergey\_Tkachenko](https://talk.commonmark.org/user_avatar/talk.commonmark.org/sergey_tkachenko/32/2185_2.png) [@Sergey\_Tkachenko](https://talk.commonmark.org/u/Sergey_Tkachenko)\
**Post date:** [March 9, 2021, 8:48am UTC](https://talk.commonmark.org/t/links-and-in-code-spans/3807/7 "2021-03-09T08:48:33Z")

</div>

I think this is the reason why the parser does it in this way.  
However, I believe that this behavior is inconsistent, because unresolved labels are never treated as labels.

---

<div class="post-metadata">

**Author:** ![jgm](https://talk.commonmark.org/user_avatar/talk.commonmark.org/jgm/32/1360_2.png) [@jgm](https://talk.commonmark.org/u/jgm)\
**Post date:** [March 9, 2021, 4:58pm UTC](https://talk.commonmark.org/t/links-and-in-code-spans/3807/8 "2021-03-09T16:58:05Z")

</div>

You said originally that you thought it was a bug. Since this behavior is required by the spec, it is not a bug. Instead, you just don’t like this aspect of the spec.

If we were going to change anything in this area, I’d actually prefer to go in the opposite direction, so that structure is _less_ dependent on whether labels are defined. See  
[https://johnmacfarlane.net/beyond-markdown.html#reference-links](https://johnmacfarlane.net/beyond-markdown.html#reference-links).

But all things considered I think it’s fine to keep things as they are.

---

<div class="post-metadata">

**Author:** ![Sergey\_Tkachenko](https://talk.commonmark.org/user_avatar/talk.commonmark.org/sergey_tkachenko/32/2185_2.png) [@Sergey\_Tkachenko](https://talk.commonmark.org/u/Sergey_Tkachenko)\
**Post date:** [March 9, 2021, 7:06pm UTC](https://talk.commonmark.org/t/links-and-in-code-spans/3807/9 "2021-03-09T19:06:22Z")

</div>

Initially, I did not say that it is a bug, I asked “may it be a bug?” )

I think the both ways are ok.

I just wish the specification to be more clear on this topic, explicitly saying that a shortcut link is not created when a list text is followed by an unresolved link label. I think it is not obvious.

---

<div class="post-metadata">

**Author:** ![jgm](https://talk.commonmark.org/user_avatar/talk.commonmark.org/jgm/32/1360_2.png) [@jgm](https://talk.commonmark.org/u/jgm)\
**Post date:** [March 9, 2021, 9:07pm UTC](https://talk.commonmark.org/t/links-and-in-code-spans/3807/10 "2021-03-09T21:07:33Z")

</div>

Sergey Tkachenko via CommonMark Discussion  
[noreply@talk.commonmark.org](mailto:noreply@talk.commonmark.org) writes:

> I just wish the specification to be more clear on this topic, explicitly saying that a shortcut link is not created when a list text is followed by an unresolved link label. I think it is not obvious.

Agreed, this would be a good candidate for an addition clarifying example. Could you put up on issue on the GitHub tracker so we don’t forget? (commonmark/commonmark-spec).

---

<div class="post-metadata">

**Author:** ![Sergey\_Tkachenko](https://talk.commonmark.org/user_avatar/talk.commonmark.org/sergey_tkachenko/32/2185_2.png) [@Sergey\_Tkachenko](https://talk.commonmark.org/u/Sergey_Tkachenko)\
**Post date:** [March 13, 2021, 10:13am UTC](https://talk.commonmark.org/t/links-and-in-code-spans/3807/11 "2021-03-13T10:13:19Z")

</div>

I found that example 567 demonstrates this issue:

```auto
[foo][bar][baz]

[baz]: /url1
[foo]: /url2

```

( foo is not a link here because bar is not defined)  
So no additional examples are needed, sorry.

* * *

But probably, examples that show preferences of link destination, link title, and link label (in full reference links) over code spans, autolinks and html tags would be useful…
