12

If there is a link to a Stack Exchange post from the same site, it is automatically expanded to the title. (Unless the post is deleted.)

For example, https://meta.stackexchange.com/q/77394 renders like this: Replace links with the current question title in comments.

How can I prevent Stack Exchange from such expansion? (One case where I could use this is when discussing on meta how something - in this case a bare URL - is displayed in a comment.)

I tried the following [https://meta.stackexchange.com/q/77394](https://meta.stackexchange.com/q/77394) - but it expands to the title, too: Replace links with the current question title in comments.

Interestingly, when I tried to link directly to this question - to illustrate what I am talking about - it wasn't expanded: How can I stop the site from expanding an URL to the title of the post?. (And now the link already expands to the title - for some reason, this wasn't the case immediately after I posted this question.)

6
  • 3
    The "expansion mechanism" is looking at some internal cache, most likely, so it takes few minutes until that cache is synched with new questions, that's why your question wasn't expanded at first. Jul 27 at 6:40
  • 2
    @ShadowTheKidWizard Thanks, that seems like a relatively likely explanation. (I wanted to use this question as an example - so that I do not have to link some "random" post just for the sake of illustration. Since after posting I saw that it did not work, I changed it to another post. (I did not realize that it will eventually change - but even than, for some time the question would look weird.)
    – Martin
    Jul 27 at 7:02
  • This comment is used to test whether expansion takes place in comments: https://meta.stackexchange.com/q/77394 => meta.stackexchange.com/q/77394; [https://meta.stackexchange.com/q/77394](https://meta.stackexchange.com/q/77394)=> https://meta.stackexchange.com/q/77394. It seems that comments are not expanded.
    – Lily White
    Aug 5 at 13:04
  • @LilyWhite This feature request is marked (status-declined): Replace links with the current question title in comments. It is rather old - but looking at the questions linked there, it seems that it came up in other posts, too.
    – Martin
    Aug 5 at 14:27
  • @LilyWhite: Correct... Though I find it a bit annoying that it's not automatic. (Personally, when I come across comments containing bare URLs pointing to other related questions, I often edit them to include the title of the post and format that as a link.)
    – V2Blast StaffMod
    Aug 5 at 17:18
  • @Martin Interesting is that it does remove the https:// part so at least some preprocessing have taken place. This is chaos.
    – Lily White
    Aug 6 at 2:25

3 Answers 3

11

One easy solution is making the content of the link text a literal, like so:

https://meta.stackexchange.com/q/77394

By surrounding the link text with backticks:

[`https://meta.stackexchange.com/q/77394`](https://meta.stackexchange.com/q/77394)

1
  • 2
    This is the correct way. Putting characters in to make them "not the same" to thwart the code will stop working if the code is "fixed" to normalize the string and the URL and remove any extra spaces etc. Putting the back ticks in to tell the render code "don't mess with this" is less likely to get broken by code changes.
    – ColleenV
    Jul 27 at 12:41
8
+50

If you do not want your link to appear as inline code, rather, if you just want it to appear like any other link (e.g., https://www.google.com), then the best solution1 would be to use an HTML comment within the usual link syntax ([link text](URL)).

For example,

[https://meta.stackexchange.com/q/77394 <!-- HTML comment -->](https://meta.stackexchange.com/q/77394)

Would render as:

https://meta.stackexchange.com/q/77394

The wording in the HTML comment should be such that it should prevent any editor(s) from messing with that specific formatting, and let them know it was done on purpose. For example, <!-- HTML comment inserted to prevent link from expanding into title. Please do not remove. --> would suffice.


1 There are other ways to prevent links from automatically expanding into the title, as mentioned in a previous version of this post, however, they are not quite ideal as editors are likely to remove such formatting, thus rendering the solution useless.

5
  • 3
    Or add a forward slash to the link (or text, but not both). The point is to make the text and the link not match. Jul 27 at 8:51
  • 1
    Relying on the code not properly normalizing the string inside the brackets to detect if the URL is the same is probably not the best solution. Some go-getter is going to look at that code eventually, not know that people are actually trying to thwart it by adding spaces and slashes, and "fix" it so that this solution doesn't work.
    – ColleenV
    Jul 27 at 12:43
  • 2
    Marking a URL as code instead of text is semantically appropriate. Regardless, I didn't say "never use this hack". I said "not the best solution". The intent of the differences you've inserted between the link text and the URL is not immediately apparent. An editor could come by and think "They forgot to add the user id in the link text, let me fix it." or "Oops, they accidentally added a space and that will keep it from expanding." If it is possible to accomplish what you want in a way that someone else can look at it and understand your intent, that's the better way.
    – ColleenV
    Jul 27 at 14:25
  • @ColleenV: I changed my answer to include a better solution than the previous solutions I had put out. Hopefully, this should be enough to prevent editors from messing with the formatting, IMO. If there is/are any disadvantage(s) of the solution above that I may have missed, please feel free to point it/them out :)
    – Justin
    Jul 29 at 12:40
  • 1
    It still risks a developer changing that code to properly identify the URL in there, but it's much better. Another solution might be to link more of the text than just the problematic URL: https://meta.stackexchange.com/a/380733/273494 There's a good chance that not detecting the URL within a larger string is intentional. Hopefully that intention documented. :)
    – ColleenV
    Jul 29 at 13:22
5

Another way around is adding querystring parameter to the URL, e.g. https://meta.stackexchange.com/q/77394?noredirect=1 isn't being expanded.

The above use something SE already knows, but you can also add something shorter to the URL to stop it from being expanded and link still works: https://meta.stackexchange.com/q/77394?x

Note: this does not work with the Share link, e.g. https://meta.stackexchange.com/q/380728/152859, you have to first remove the userid then add the ?x or ?noredirect=1.

2
  • 1
    ?x / ?noredirect=1 doesn't seem to work with the user id included, i.e., the link from the Share dialog: i.stack.imgur.com/HsBYx.png
    – Justin
    Jul 27 at 7:38
  • 2
    @Justin interesting, thanks. I've edited the answer to mention it. Jul 27 at 7:43

You must log in to answer this question.

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