11

I can imagine situations when somebody might include a bare URL in post. And the link might even be between [ and ] - if it is a part of some explanatory remark.

It seems that in such cases the link is not actually rendered as a link - see the links which I posted below as a test. (They are not "clickable" in the post - although they are displayed as links in the preview.)

Of course, this can be easily avoided - one can simply use [text](URL) syntax. So it can be considered a very minor bug - but I though that it was still worth reporting.


Here is a test - the links are displayed just as text:

In Stack Exchange posts we often use hyperlinks. [Still, we should keep in mind that the post should make sense even if the links stops working: https://en.wikipedia.org/wiki/Main_Page. As an example, I chose a link to the Wikipedia: https://en.wikipedia.org/wiki/Main_Page.]

(The same text works if I do not use square brackets: Still, we should keep in mind that the post should make sense even if the links stops working: https://en.wikipedia.org/wiki/Main_Page. As an example, chose a link to the Wikipedia: https://en.wikipedia.org/wiki/Main_Page.)

[Does it work with escaped brackets? Yes it does: Still, we should keep in mind that the post should make sense even if the links stops working: https://en.wikipedia.org/wiki/Main_Page. As an example, I chose a link to the Wikipedia: https://en.wikipedia.org/wiki/Main_Page.\]

Interestingly, the behavior changes a bit if there is [text](URL) at some place between the brackets. [Still, we should keep in mind that the post should make sense even if the links stops working: https://en.wikipedia.org/wiki/Main_Page. As an example, I chose a link to the Wikipedia: https://en.wikipedia.org/wiki/Main_Page.]


This is what I see after posting:

Screenshot of the post, where the links do not work

This is what I see in the preview (while posting/editing):

screenshot of the preview, where the links appear to work

5

1 Answer 1

5

This is not a bug; the behaviour you're pointing out is in accordance with the CommonMark Specification.

The text inside the square brackets [] is refered to as link text (emphasis mine):

6 Inlines

6.3 Links

A link text consists of a sequence of zero or more inline elements enclosed by square brackets ([ and ]). The following rules apply:

  • Links may not contain other links, at any level of nesting. If multiple otherwise valid link definitions appear nested inside each other, the inner-most definition is used.

So what happens is the bare URL https://en.wikipedia.org/wiki/Main_Page will not render as an HTML <a href></a> if it's nested inside a link text.

Your example that isn't nested or escapes the enclosing square brackets of the link text renders as (source indented for readability):

<p>[Does it work with escaped brackets? Yes it does:
    Still, we should keep in mind that the post should make sense even if the links stops working:
    <a href="https://en.wikipedia.org/wiki/Main_Page" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/Main_Page</a>.
    As an example, I chose a link to the Wikipedia:
    <a href="https://en.wikipedia.org/wiki/Main_Page.%5C%5D" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/Main_Page.\]</a>
</p>

While in the nested example no <a href></a> is rendered.

<p> (...) [Still, we should keep in mind that the post should make sense even if the links stops working: https://en.wikipedia.org/wiki/Main_Page. As an example, I chose a link to the Wikipedia: https://en.wikipedia.org/wiki/Main_Page.]
</p>
4
  • "If multiple otherwise valid link definitions appear nested inside each other, the inner-most definition is used." But is the outer "link" valid? And wouldn't this mean that it should be the inner one which is linked anyway?
    – Laurel
    Apr 28, 2022 at 11:39
  • 1
    @Laurel the problem is there are 2 nested link definitions at the same level so the specification isn't clear about which one would take precedence (that's likely left as an implementation detail). But in any case the outer link would not be valid.
    – bad_coder
    Apr 28, 2022 at 11:44
  • 5
    I think that, while this is true, there's a different experience between the preview and the final post rendering, so at the very least, we should be fixing the preview so it doesn't seem like the links will render properly.
    – Catija
    Apr 30, 2022 at 14:05
  • @Catija indeed consistency between preview and final render is desirable (it's possible this also impacts the new stacks editor, I haven't tested it). From a theoretical stand point I think this post goes as far as reading the specification alone will allow us to go.
    – bad_coder
    Apr 30, 2022 at 14:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .