obsidian-api icon indicating copy to clipboard operation
obsidian-api copied to clipboard

Expose Markdown HTML handlers

Open HEmile opened this issue 3 years ago • 4 comments

I have some popovers with Markdown that I converted to HTML using renderMarkdown. However, if the resulting HTML is added to any view or modal but Obsidian's Markdown view, the event handlers for eg internal links and images are not added, which means internal links won't open and internal images aren't rendered.

It would be very useful if if were possible to attach these handlers using the Obsidian API, for example by passing an HTMLElement.

HEmile avatar Feb 20 '21 17:02 HEmile

Make sense, I'll need to think of something for this.

lishid avatar Feb 21 '21 22:02 lishid

If I may offer a suggestion, it might be simplest to just define global event handlers tied to the styles (e.g. a.internal-link). This would make it a lot easier to implement things that reference files, generally. For example, the Recent Files and Calendar plugins both have partial re-implementations of the logic for hover, click, ctrl-click etc., and implementing all the right handlers to match Obsidian core is both tedious and error-prone. But if there were a standard for classes or data attributes to signal e.g. "this thing is a reference to file" or "this is a hover parent of hover type X", then a lot of wheel reinvention could be avoided.

(In addition to those challenges, I recently found that internal image links don't load in HTML rendered by renderMarkdown, so there's really no way to implement much on top of Obsidian's markdown rendering. Sadly that part can't be fixed by global event handlers, so some kind of API is needed.)

For the record, the other use cases I've run into in the wild are the Kanban plugin, and my own investigations into supporting WYSIWYG via line widgets for HTML generated from the markdown being edited. On the latter, I've run into a bit of a wall with rendering e.g. tables with images or embeds. I dug into some of the MarkdownRenderer subclasses to see how they worked, but they use lots of private and undocumented things that got really hairy to follow really quickly.

pjeby avatar May 18 '21 08:05 pjeby

I am having the same dilemma with a plugin that I am developing. If I use the registerMarkdownCodeBlockProcessor function and add a MarkdownRenderChild, I can use MarkdownRenderer.renderMarkdown function and have the hover event on a rendered link work properly. However, once I use anything else, like a class that extends the TextFileView, the hover event no longer works. @lishid Has any progress been made on this?

decaf-dev avatar Apr 02 '23 11:04 decaf-dev

Same issue for me. I just want to show links in a custom Modal, but no matter what I try, clicking/hovering over such links does not have any effect. Would be really great to get it working as it is in the default markdown editor! Adding global event handlers for a.internal-link and a.external-link would already solve it for me. Any idea for a workaround?

saluto avatar Oct 19 '23 11:10 saluto