Fix extra spaces a user selects when adding a link

AI-generated summary

Users discuss a minor UI quirk in Discourse where selecting text with a leading space before creating a link causes the space to be removed. Cardinalflyer reports the issue and shares a GIF demonstrating it. ondrej asks if manually adding a space fixes the appearance. Stephen explains that the toolbar intentionally trims whitespace from link selections to avoid messy formatting, and advises users to avoid selecting leading spaces. codinghorror agrees it’s not a bug but suggests a user-friendly enhancement: automatically restoring single leading or trailing spaces outside the anchor tag. Cardinalflyer confirms this would be ideal, noting the space should be placed outside the anchor to preserve clean HTML. Finally, vinothkannans links to a commit (580b289) that resolves the issue by detecting and restoring such spaces appropriately. The feature request was implemented to improve usability without compromising formatting integrity.

Here’s a small detail I keep bumping into: When adding a link to text in Post edit, if the selection includes the leading space that space will be swallowed in the process.

Testingthe leading space on Meta. Yep, it does the same. I try to be more careful but it keeps happening and might be a quick thing to fix…

Here’s how it looks in my instance:

3 Likes

It does not add a space automatically? Does it look normal if you manually add a space in-between what is circled?

In your example you select the leading space when creating the link. That’s not typical behavior. Try selecting the word without the space and the issue won’t occur.

It’s hard to call this a bug as the toolbar button is trimming white space from the selection because whitespace within anchors looks messy.

The latter is only happening because you’re selecting the leading space, don’t do that and the link presentation cleanup won’t hit you.

3 Likes

You are correct but a nice user convenience feature would be to detect this and fix it up for the user, if a single leading or trailing space is selected, put them back in the replacement.

8 Likes

Exactly… and it can be put back outside the anchor to avoid the messy anchor whitespace issue…

3 Likes

https://github.com/discourse/discourse/commit/580b28986192cb659ea4232c3a22c865125e9197

The above commit should resolve this issue.

4 Likes

This topic was automatically closed after 2 days. New replies are no longer allowed.