-
Notifications
You must be signed in to change notification settings - Fork 73
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
Basic syntax highlight support for code blocks #526
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This was referenced Oct 29, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Syntax highlighting & word wrapping for code blocks
Finally, syntax highlighting was added as a feature for code blocks:
Context
In order to achieve the syntax highlight, the Highlight.js library is being used. Do note that support for syntax highlighting in code blocks is not a supported feature of the text editor we are using CKEditor), but rather a hack which makes use of the highlights API (used for highlighting search results for example). Nevertheless, we haven't noticed any major issues during the development of the feature, but feel free to report any issues you might have.
Most of the work to achieve the syntax highlight itself was already done by antoniotejada in https://github.com/antoniotejada/Trilium-SyntaxHighlightWidget. On our side we added customization but also additional functionality.
Migrating from existing syntax highlight plugins
If you are already using a syntax highlighting plugin such as the Trilium-SyntaxHighlightWidget we are basing off of, it is important to disable that plugin before upgrading in order for it not to conflict with our implementation.
Should you encounter any issues after the migration, try running Trilium in safe mode.
New section in settings
In order to configure this new feature, a section has been added in Options → Appearance to control the syntax highlighting. There the color scheme can be chosen, from a builtin selection of themes from Highlight.js.
It is also now possible to disable the word wrapping for code blocks, which can make them easier to read for large amounts of code. Word wrapping has now been disabled by default.
It is also possible to disable the syntax highlighting by selecting “No syntax highlighting” in the “Color scheme” option.
Using the syntax highlight in auto mode
Provided the syntax highlighting is enabled in the settings, simply create a code block within a text note and it will try to automatically detect the language (using the Highlight.js library):
Adjusting the language manually
Should the automatic syntax highlight not work well enough, it is possible to manually adjust the language of the code block:
Adding support for new languages
By going to settings → Code Notes → Available MIME types in the dropdown, it is possible to adjust the languages that are used for code blocks as well.
Note that not all languages that are present in this list (which is meant for code blocks, using CodeMirror as editor) are supported by our syntax highlight library. In this case you will simply see no syntax highlighting when you select the language.
If syntax highlighting is not supported for a given language, feel free to open an issue and we will look whether it is possible to integrate it.
Automatic disable of syntax highlighting
Note that when editing a text note, syntax highlighting is automatically disabled if the code block is too big (somewhere around 500 lines). This value is currently not configured.
For read-only text notes, this limitation is not applied.