Make Obsidian code blocks compatible with prismjs spec and other apps

I’ve just discovered Obsidian. Am very, very impressed by how such a simple interface solves so many pain points I’ve had with text notes.

However!

Use case or problem

Obsidian defines prismjs code blocks using backticks followed by the language, e.g., ```js

The prismjs spec requires prefixing with language- or lang-, e.g., ```language-js

This means that code blocks inserted via Obsidian are not compatible if the text file is opened in any other apps using prismjs (in my case DEVONthink), and vice versa.

Proposed solution

Allow Obsidian to recognise code blocks in a manner consistent with the prismjs spec, i.e., with the language- or lang- prefix.

2 Likes

Use case or problem

The current syntax highlighting for the markdown previews uses “prismjs” and this highlighting isn’t applied to the code blocks in editing mode. There are many requests for this. To fill the void there is a plugin to achieve this but it is based on “codemirror”. This results in some weird usage:

  • They don’t come with matching themes
  • They use different tokenizers and results in different HTML, so you have to write your themes twice
  • Both prismjs and codemirror are highlighters built for web and they don’t have the best tokenization. Text editors use the textmate tokenizer which results in waaaay better syntax highlighting. It supports theme customization prismjs and codemirror can’t even come close to.

Proposed solution

  1. Switch all code highlighting to GitHub - shikijs/shiki: A beautiful yet powerful syntax highlighter - It uses TextMate grammar to tokenize strings, and colors the tokens with VS Code themes.
  2. Enable syntax highlighting in editor mode using shiki
  3. Expose a syntaxTheme configuration option that can use any vscode theme, portentially expose two options: lightSyntaxTheme and darkSyntaxTheme

Current workaround (optional)

Use the community plugin for editor syntax highlighting + write two similar (but not matching) themes for both underlying syntax highlighters

Related feature requests (optional)

5 Likes

Having written a plugin now I see that switching to shiki would be quite hard. all current plugins are tied to codemirror so changing to shiki (an by extension monaco) would probably be a huge lift.

Instead the solve for this could be just switching the preview pane from using prism to highlighting with codemirror. We miss out on the better textmate tokenization, but will at least have matching themes when switching between the editor and preview

3 Likes

I merged the two threads for and against using primjs, as both options can’t be implemented. I’m tempted to merge this with the syntax highlighting thread as well.

1 Like

Hi @hipstersmoothie,
Did you crack this one in the end? It’d love to be able to use shiki to highlight my code!!
Cheers

1 Like

This is also just clicking for me: Obsidian has syntax highlighting, but only in Reading-mode.
But I never even noticed, because I’m always editing.

There’s the Editor Syntax Highlight Plugin, which works even while editing the syntax.

The built-in one uses PrismJS, the plugin uses CodeMirror. So the results look different. And behave different:

  • the style and colors are different.
  • the language names are different. py works as code definition Prism, but not for CodeMirror.
  • the plugin/CodeMirror supports far fewer languages. Godot’s GDScript and ABAP for example. Adding new language to CodeMirror’s codebase isn’t an option.

Not terrible - but also not very nice to use. And neither seem extendable with new syntax definitions. E.g. your personalized syntax.

Ideally the same system would cover both reading, editing and preview views. And allow syntax modification. Prism seems better. Shiki which hipstersmoothie mentioned above even better, as it takes VSCode definitions, so looks the same and provides the most code options. But I can see how that could be too much work.

2 Likes

Hi. Did you manage to personalize the ABAP syntax after all? I have the same problem, and my expectation was that prismjs can already handle it.

Thanks!

bash doesn’t render as good as it could
Exemple command:

conda config --append channels CHANNEL_NAME

PrismJS test drive:
https://prismjs.com/test.html#language=bash&text=conda%20config%20--append%20channels%20CHANNEL_NAME

In Obsidian:

1 Like