I maintain a small plugin that turns text that matches a regex into a clickable link. Specifically, go/something
is rendered as a link and when clicked on, sends the user to http://go/something
. I don’t use live preview, so I’ve gotten by with unstyled text in source mode and a MarkdownRenderChild
for reader view.
I’ve recently been trying to adapt the plugin for the various editing modes using a Codemirror ViewPlugin
. I’ve got everything working the way I want for source mode, but I’ve been having trouble with live preview.
I’m correctly turning go/something
ranges into the following span:
<span class="cm-url cm-golink" data-golink-href="http://go/something">
go/something
</span>
This is powered by the following Decoration
:
Decoration.mark({
class: [
"cm-url", // to make styling work like we want
GOLINK_HTML_CLASS, // to identify these as being ours
].join(" "),
attributes: {
[DATA_PROPERTY]: href,
title: href,
},
inclusiveStart: true,
inclusiveEnd: true,
}).range(from, to);
The created spans acts just like a link I’ve typed manually (e.g. https://example.com
). My issue is in live preview mode.
For links that Obsidian recognizes, it adds <a>
tags under the span when the cursor is elsewhere on the page:
<span class="cm-url" spellcheck="false">
<a class="cm-underline" tabindex="-1" href="#">
https://go/big
</a>
</span>
However, my spans
don’t get the same treatment. I can fake it with some additional CSS and a class that’s only applied to live preview, but it would be nice to tell Obsidian that these spans are links (just like the ones its finding) and let it take care of the rest. Is that possible? Am I missing something?
Thank you!