Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Templater Syntax Highlight is broken on Obsidian 1.5.0 (Desktop - Catalyst/Insider release) #1251

Closed
I-Pch opened this issue Nov 21, 2023 · 15 comments · Fixed by #1263
Closed
Labels
bug Something isn't working

Comments

@I-Pch
Copy link

I-Pch commented Nov 21, 2023

Hi there 😊 !

This is much more a visual bug than anything else (apparently) but the Desktop Catalyst release of Obsidian 1.5.0 broke Templater's syntax higlighter 😔 ...

Hoping this is something that can be fixed (🍀 ), here I am 😊 ...


Plugin information:

  • OS: MacOS Big Sur 11.7.10
  • Templater version: 1.18.1
  • Obsidian version: 1.5.0 (Catalyst/Insider release)
  • Templater settings:
    • Fresh install in the Sandbox Vault
    • Template folder location: /Temp
    • Syntax Highlighting on Desktop: On
    • Syntax Highlighting on Mobile: Off
    • Automatic jump to cursor: On
    • Trigger Templater on new file creation: On
    • Show icon in sidebar: On
    • Template Hotkeys: None set up
    • Folder Templates: Enabled but none set up
    • Startup Templates: None
    • User Script Functions: Not set up
    • User System Command Functions: Disabled

Describe the bug:

Apparently, the Catalyst release of Obsidian 1.5.0 (Desktop) doesn't seem to play well with Templater's syntax highlighter and all templates are now rendered as simple .md files 😔 .
In terms of functionality, the templates still seem to work as expected but writing and/or re-reading a long and complex template can now be a bit hard and confusing experience 🥺 ...


Expected behavior:

If possible 🥺 , I really would like to have Templater's syntax highlight back 😊


Screenshots:

I wish I would be able to do a before/after comparison but:

Screenshot 1: From the Sandbox Vault (+ Templater)

Here's how a template now looks like in the Sandbox Vault.
(The template works as expected though 😊 )

Sandbox - Broken Syntax Highlight - Templater

Screenshot 2: From a messy "Test" vault

And here's a weird looking templater's template from one of my messy test vaults 🙃 : Live Preview vs. Source Mode

It seems like Obsidian is actively trying to render the HTML and markdown link used in this template, leading to some gaps in Live Preview, instead of considering this .md file as what it actually is: a template 😔 .

templater - syntax highlight

Additional context:

I didn't change anything in my vaults other than rebooting Obsidian once the Catalyst/Insider release was installed 😊 .

I really really do hope a fix exists for this visual issue 🍀 😇 (for the sake of the template's readability) ...

Thank you so very much in advance for any help regarding this 🙌 !!!

@I-Pch I-Pch added the bug Something isn't working label Nov 21, 2023
@Zachatoo
Copy link
Collaborator

Can reproduce, will investigate. Thanks!

@Zachatoo
Copy link
Collaborator

Zachatoo commented Dec 2, 2023

Update: This will be tricky for me to fix. Looks like with the deprecation of CodeMirror 5 in Obsidian 1.5.0, the previous way of adding syntax highlighting will no longer work and will have to be rewritten. I am not very experienced in CodeMirror, so migrating the code to CodeMirror 6 will take some time. When it is fixed, it may not be exactly the same as it was before.

@iamrecursion
Copy link
Contributor

iamrecursion commented Dec 2, 2023

Given that highlighting still works on mobile, which is CM6, you can likely just unify the both of those now.

The highlighter is a StreamParser, which is the CM6 API to CM5-style highlighters. It hasn't, to my knowledge, been removed or deprecated.

@Zachatoo
Copy link
Collaborator

Zachatoo commented Dec 5, 2023

I don't think it's the deprecation of CM5 itself that broke it, rather some side effect of that change broke it.

I've tried a few things trying to get the token function for the templater mode to run at all, but it's never being called. The token function for the hypermd mode is being called. The templater mode is being registered as an editor extension, though I don't know if it's being done correctly since it isn't being called.

@iamrecursion
Copy link
Contributor

It might be necessary to work out what's changed between the APIs. I'll let you know if I find anything.

@iamrecursion
Copy link
Contributor

According to some discussions I've had internally, the removal of CM5 has resulted in cm.setOption used here no longer working. Templater uses it to toggle the highlight mode on and off.

Strangely enough, this should never have worked on mobile, but I cannot see where else in the codebase the mode would be handled for mobile specifically.

The syntax is created here as an overlay on HyperMD, and is defined correctly using StreamParser as best I can tell. One thing that isn't clear to me is whether StreamParser is sufficient as an Extension itself in CM6 terms. Perhaps it needs to be wrapped in a ViewPlugin.define.

@iamrecursion
Copy link
Contributor

Possibly mixed language parsing might be helpful.

@iamrecursion
Copy link
Contributor

iamrecursion commented Dec 8, 2023

I have a PR open #1263 that fixes this.

While there was some work to fix the dynamic loading and unloading of the highlighter, the basic issue came from the fact that 1.5.x changed the priority for the default hypermd highlighter, and hence the extension here was getting loaded below it. Bumping the priority was all that was needed in this case.

@Zachatoo
Copy link
Collaborator

Zachatoo commented Dec 8, 2023

Released in 2.0.0 of Templater! Thank you so much @iamrecursion !

Note: Syntax highlighting will not work on mobile if you sync your vault between desktop and mobile and are therefore able to install the latest version of Templater on your mobile device. I do not think this is a concern. If you're syncing between desktop and mobile, you can manage your templates on desktop if you want syntax highlighting until mobile reaches Obsidian 1.5.x feature parity.

@iamrecursion
Copy link
Contributor

iamrecursion commented Dec 8, 2023

@Zachatoo Are you sure about the mobile thing? I have plugin sync on and it was working on mobile during my testing! Mobile has always used CM6, and the new way of doing things should be compatible.

@iamrecursion
Copy link
Contributor

Yeah, can confirm it's still working on mobile even in the full 2.0.0 release.

@Zachatoo
Copy link
Collaborator

Zachatoo commented Dec 8, 2023

I had to restart Obsidian, now syntax highlighting is working for me on mobile on 2.0.0. That's my bad!

@iamrecursion
Copy link
Contributor

Phew! Was worried I'd broken something strange for a moment there!

@I-Pch
Copy link
Author

I-Pch commented Dec 8, 2023

I just really need to say this 😄 :

Thank you so so so very much to the both of you @iamrecursion and @Zachatoo for fixing this 🙌 🥳 !!!

It works perfectly on Desktop (Obsidian 1.5+) and Mobile (Obsidian 1.4.16) 🎉 !!!

@mProjectsCode
Copy link

mProjectsCode commented Dec 10, 2023

It might be worthwhile to port the syntax highlighting to a CM6 View Plugin that creates mark decorations. That approach should be a lot simpler, more powerful and future-proof. I am using such an approach in Meta Bind.

I would consider writing a PR for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants