Hi there,
First of all, thanks for creating Obsidian and it is my favourite note-taking editor at the moment, besides VSCode. In fact, it is much better than VSCode in terms of note-taking and note-linking IMHO.
I just started to learn how to interact with the Obsidian API and would like to write a plugin to do some additional HTML rendering in the preview mode.
I registered to listen to both ‘layout-ready’ and ‘layout-change’ events,
this.registerEvent(
this.app.workspace.on("layout-ready", this.markupPreview.bind(this))
);
this.registerEvent(
this.app.workspace.on("layout-change", this.markupPreview.bind(this))
);
Then in markupPreview
, I tried to get the DOM elements to perform some manipulations:
var markdownLeaves = this.app.workspace.getLeavesOfType("markdown");
for (var i = 0; i < markdownLeaves.length; i++) {
var markdownLeave = markdownLeaves[i];
if (markdownLeave.view && markdownLeave.view instanceof MarkdownView) {
if (markdownLeave.view.getMode() == "preview") {
console.log(markdownLeave.view);
var elements = markdownLeave.view.containerEl.querySelectorAll('.markdown-preview-view div>p');
...
Pardon me for my ignorance - I am a newbie with TypeScript and Obsidian API. In my case, I found that I can find the DOM elements under ‘layout-change’ event but empty under ‘layout-ready’. However, I can see the views under these two events contains the HTML code that I am after, but somehow I cannot query them (empty) under ‘layout-ready’ event.
Any help would be much appreciated! Thanks.