(Live Preview) Don't color square bracketed text as a link unless it's part of a link

Use case or problem

I want to use square brackets as normal without having to escape them. Markdown should only treat square bracket text as a link IF it is followed by ().

Proposed solution

Fix it.

Current workaround (optional)

.cm-s-obsidian span.cm-link, .cm-s-obsidian span.cm-link:hover {
color: var(–text-normal);
text-decoration: none;
}

This removes highlighting for actual links as well.

Related feature requests (optional)

7 Likes

There’s an older comment here:

The issue in the editor (there’s no issue in Reading view) is that single square brackets are in a state of limbo until you do something with them. They could become a [markdown](link.md), a [reference]: link, a [^footnote], and so on.

The CSS assigned to [something] is cm-hmd-barelink, so you could try this for the time being:

.cm-s-obsidian {
    & .cm-link.cm-hmd-barelink {
        color: var(--text-normal);
    }
    
    & .cm-link.cm-hmd-barelink:hover {
        color: var(--text-normal);
        text-decoration: none;
    }
}

or non-nested:

.cm-s-obsidian .cm-link.cm-hmd-barelink {
    color: var(--text-normal);
}

.cm-s-obsidian .cm-link.cm-hmd-barelink:hover {
    color: var(--text-normal);
    text-decoration: none;
}
4 Likes

Yeah, this is a quirk of Live Preview’s syntax highlighting — it’s highlighting too eagerly. WhiteNoise’s comment doesn’t make sense to me. Fortunately we can work around it with CSS snippets (I use the non-nested one that trainbuffer posted).

+1

Personally, having to deal a lot with them as I copy text from literature that has extensive use of square brackets for mentioning references.

Example:

The global biobased PA market is assumed to comprise around 200 × 10^3 t/a [94]

It is even more problematic if there are multiple occurrences:

The global biobased PA market is assumed to comprise around 200 × 10^3 t/a [94] [110] [114] [116]

In Live Preview mode: one every two occurrences is not even shown, see screenshot below.

image

1 Like

It seems not to be the case on my end, see screenshot below.

What’s the markdown in that note? It looks as if 94 and 114 are internal links and have double brackets[[94]] [[114]]? If they were single brackets, you would see the brackets themselves.

This topic is about single [square] brackets on their own and how Live Preview treats them.

Both shots from Live Preview:

See previous comment, I provided the markdown and it’s single brackets, not internal links.

Apologies! I saw the notification post on my phone and quickly replied on the laptop without scrolling up. Yeah, something is odd about how Live Preview is handling the consecutive single brackets.

source mode | live preview | reading view

There’s a bug report here:

and there may be a few more involving single bracketed content in Live Preview.

2 Likes

Too bad, I didn’t know this bug.

Usually I avoid brackets if not for internal/external links. The only solution here is to put your formula between backtiks:

The global biobased PA market is assumed to comprise around `200 × 10^3 t/a [94] [110] [114] [116]`

I really don’t want to add extra formatting like this because it’s time-consuming and not good if I need to export notes in the future.

My other important use of square brackets is when I add additional details not coming directly from the original source like this: When to Use Square Brackets [ ] in English | The Editor’s Manual

Example

MD Text:

[La choucroute est une] [[Lacto-fermentation|Fermentations lactiques]] des [[Fructoses]] et [[Glucoses]] par les [[Bactéries]]

La baisse pH du aux [[Acides lactiques (LA)]] évite la production de [[Micro-organismes|microorganismes]] pathogènes

Source : INRAE, 2023 - Ressources #3

In Live preview mode:


One internal link is not visible, and it breaks the sentence (in French). Furthermore, as previously identified, the text inside single square brackets is erroneously displayed as a link.

2 Likes

Backtiks are easy to add, just select a word/sentence and type ` once to enclose the whole.
The point here is, to not lose multiple strings in brackets, if they appear in a sequence.
I understand, Backtiks are more in use by developers, still, you may try it too; anyway that’s just my suggestion.

agreed!

The issue in the editor (there’s no issue in Reading view ) is that single square brackets are in a state of limbo until you do something with them. They could become a [markdown](link.md) , a [reference]: link , a [^footnote] , and so on.

This doesn’t seem all that relevant to me. They should simply behave like normal text unless they are one of these things.

3 Likes

I agree completely. Single brackets serve a different purpose in prose than parentheses, and we should be able to use them freely without having to resort to escape characters and css hacks.

This behavior might make sense in a code editor but not in an app designed for note-taking and writing.

1 Like

Updated the post title for greater precision. (Was “Don’t treat square bracketed text as a link unless followed by parens”.)

1 Like

I know there have been a lot of posts about this, but they all seem to be locked. Some are even bug reports where the only response is “not a bug” with no explanation about why it’s not considered a bug.

https://forum.obsidian.md/search?q=Single%20bracket

Because of the way the parser works, you can even end up in situations where the next word is not rendered. For example, the following will render “[Foo]” as a link, but “[Bar]” will not be rendered at all in the live editor.

I really think there is a bug in the markdown parser. User’s shouldn’t need to manually escape uses of brackets.

Steps to reproduce

  1. Open a new/vanilla Vault
  2. Type in “[Foo] [Bar]”
  3. Move the cursor to a different line

Did you follow the troubleshooting guide? [Y/N]

Y

Expected result

“[Bar]” should render

Both “[Foo]” and “[Bar]” should render as plain text, not a link.

Actual result

“[Bar]” will disappear.

Environment

SYSTEM INFO:
Obsidian version: v1.6.5
Installer version: v1.4.13
Operating system: Darwin Kernel Version 24.0.0: Mon Jul 1 21:58:14 PDT 2024; root:xnu-11215.0.132.501.1~1/RELEASE_ARM64_T6000 24.0.0
Login status: logged in
Catalyst license: insider
Insider build toggle: on
Live preview: on
Base theme: adapt to system
Community theme: none
Snippets enabled: 0
Restricted mode: on

1 Like

Related:

1 Like

This is specifically a quirk of Live Preview. If you switch to Reading View (which typically renders more correctly because it’s not also trying to be a text editor made of lines), you’ll see the text as expected.

Live Preview is highlighting what looks like an incomplete reference-style link. You can probably work around it for now by “escaping” one of the brackets by placing a backslash before it.

Similar/same issue: The display text of a markdown link will be hidden in live preview if different bracketed text comes before it separated by only a single space.

In the following example
[Display me] [Also display me](www.example.com)
[Also display me] will be completely hidden and the link might not function properly either.

Current fix: Add a separate space or a different character between the two bracketed snippets like [Display me].[Also display me](www.example.com)