obsidianmd / obsidian-api

Type definitions for the latest Obsidian API.
https://docs.obsidian.md
MIT License
1.65k stars 192 forks source link

Request: Expose Codemirror loaded Language instances #117

Closed mayurankv closed 12 months ago

mayurankv commented 12 months ago

Request

It would be useful to expose the loaded Language instances (or LanguageSupport) instances that obsidian has loaded so that plugins could add codemirror syntax highlighting in editing mode.

Motivation

This would expose further functionality to allow for plugins to have a more in-depth ability to deal with writing code (i.e. adding autocompletion, bracket matching or other language features). Also it would mean that a plugin reliant on the '@codemirror/lang-data' module wouldn't have to load a new instance of a language (that obsidian has already loaded) to use syntax highlighting.

Thank you for your consideration!

mayurankv commented 12 months ago

In addition, it would be great to expose the Highlighter that Obsidian uses (even if the languages themselves are not exposed) so plugins would have highlighting style classes consistent with obsidian itself.

lishid commented 12 months ago

Language parsing support currently relies on the old CodeMirror 5 system, you should be able to hook up to it using the global CodeMirror object from a plugin.

lishid commented 12 months ago

The highlighter is available through the alternative cm-language package (https://github.com/lishid/cm-language/blob/main/src/stream-parser.ts#L428), you should be able to get the highlighted classes via these prop fields when interacting with tree nodes from tree.iterate: https://github.com/lishid/cm-language/blob/main/src/stream-parser.ts#L428

lishid commented 12 months ago

Also issues in this repo is only for bugs with the API - if you have feature requests for the API, or questions, please post in our discord server's #plugin-dev channel, or the forum at https://forum.obsidian.md/ instead.

mayurankv commented 12 months ago

Thank you for your help, that's answered everything I wanted to know! I managed to get syntax highlighting done within the plugin.

Apologies, I thought I saw another request issue but I'll default to the forum next time.

Out of curiosity, is Obsidian likely to transition to the codemirror 6 Language style parsing (any time soon I guess) or not really?

lishid commented 12 months ago

Out of curiosity, is Obsidian likely to transition to the codemirror 6 Language style parsing (any time soon I guess) or not really?

Unlikely for the time being. Unfortunately a lot of editor-related functionalities ended up depending on the stream parser, including Live Preview and other token based functionality like context menu actions. A switch in the syntax parser would likely mean a major rewrite of the editor integration which isn't something we can commit to right now.