Is there a pre-render pre-processor callback?

I would like to make a plugin that can be used for some simple pre-processing before rendering the preview in Obsidian. One simple example might be to look through the entire MD for say this:

{% highlight javascript %}

and change it to

\`\`\`javascript\`

but needs to do this swap prior to Obsidian doing any markdown rendering.

Is there an appropriate callback I can use for this particular task, like a pre-processor on the MD file?

The reason is so that I can have obsidian render it for me the obsidian way, while passing the marked up version out to whatever else I might be using (in this case Jekyll)

I’ve just figured out this seems to work!

const renderer = view.previewMode.renderer; // view is a MarkdownView
renderer.set(renderer.text.replaceAll('{% highlight javascript %}', '```javascript').replaceAll('{% endhighlight %}', '```'))

Example:

{% highlight javascript %}
function hello(name) {
	console.log("Hello, " + name);
}
{% endhighlight %}

Screen Recording 2023-12-06 at 14.58.56

Note that this operation doesn’t change the actual content of the markdown file. It only affects how it’s rendered.

That is exactly what I need to do…like a pre-processor intended to make Obsidian’s rendering do what I want, while allowing the original markdown to stay in the file for later processor by jekyll, or whatever…

I want to setup my Obsidian rendering enviornment to mimic as closely as possible what will happen in rendering to be like what will happen in GitHub pages, which uses a subset of Jekyll functionality. I will have a look later at what you did here, I am totally new to Obsidian and Atom in general so will take me a minute to grok it.

ps - I notice in the plugin manual, there is mention of a postMarkdown callback, so am I correct to assume that would be the case for modifying already-rendered HTML, as opposed to this approach above, which is modifying the in-transit markdown during Obsidian’s rendering process?

Yes,

  • My approach above (not documented, so I’m not sure this can cause some problems) changes the markdown source before rendering starts
  • while the documented markdown post-processing operates upon a pre-rendered HTML and modifies its contents

Note that you might be able to achieve the same goal with post-processing. But it might require some trick to keep track of the global context, especially when the highlight block contains line breaks because post-processors usually operate on a block-by-block basis.

On the other hand, plugins are usually not expected to perform any markdown pre-processing so this approach will require some tricks, too.

yea exactly that’s what I thought. Well I will see if I go down this road or not. I really don’t want to get into the act of trying to change the post-rendered HTML. A few little pre-processor changes pre-rendering would be useful. But if it’s undocumented…well maybe it would work for a while, but I get you…

still thinking about whether I should even bother with Obsidian preview and just use it to edit markdown in source view and do my previewing entirely with the jekyll system. Which is probably the more simple and better way to go.