Skip to content

Commit

Permalink
client: Don't load syntax highlighter when not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
eliandoran committed Oct 31, 2024
1 parent 00209ec commit 9e3c1b4
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/public/app/services/content_renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ async function renderText(note, $renderedContent) {
await linkService.loadReferenceLinkTitle($(el));
}

applySyntaxHighlight($renderedContent);
await applySyntaxHighlight($renderedContent);
} else {
await renderChildrenList($renderedContent, note);
}
Expand Down
21 changes: 19 additions & 2 deletions src/public/app/services/syntax_highlight.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import library_loader from "./library_loader.js";
import mime_types from "./mime_types.js";
import options from "./options.js";

/**
* Identifies all the code blocks under the specified hierarchy and uses the highlight.js library to obtain the highlighted text which is then applied on to the code blocks.
* Identifies all the code blocks (as `pre code`) under the specified hierarchy and uses the highlight.js library to obtain the highlighted text which is then applied on to the code blocks.
*
* @param $container the container under which to look for code blocks and to apply syntax highlighting to them.
*/
export function applySyntaxHighlight($container) {
export async function applySyntaxHighlight($container) {
if (!isSyntaxHighlightEnabled()) {
return;
}

await library_loader.requireLibrary(library_loader.HIGHLIGHT_JS);

const codeBlocks = $container.find("pre code");
for (const codeBlock of codeBlocks) {
$(codeBlock).parent().toggleClass("hljs");
Expand All @@ -31,6 +39,15 @@ export function applySyntaxHighlight($container) {
}
}

/**
* Indicates whether syntax highlighting should be enabled for code blocks, by querying the value of the `codeblockTheme` option.
* @returns whether syntax highlighting should be enabled for code blocks.
*/
export function isSyntaxHighlightEnabled() {
const theme = options.get("codeBlockTheme");
return theme && theme !== "none";
}

/**
* Given a HTML element, tries to extract the `language-` class name out of it.
*
Expand Down
10 changes: 8 additions & 2 deletions src/public/app/widgets/type_widgets/ckeditor/syntax_highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@
* TODO: Generally this class can be done directly in the CKEditor repository.
*/

import library_loader from "../../../services/library_loader.js";
import mime_types from "../../../services/mime_types.js";
import { isSyntaxHighlightEnabled } from "../../../services/syntax_highlight.js";

export function initSyntaxHighlighting(editor) {
console.log("Init syntax highlight");
export async function initSyntaxHighlighting(editor) {
if (!isSyntaxHighlightEnabled) {
return;
}

await library_loader.requireLibrary(library_loader.HIGHLIGHT_JS);
initTextEditor(editor);
}

Expand Down
6 changes: 2 additions & 4 deletions src/public/app/widgets/type_widgets/editable_text.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import appContext from "../../components/app_context.js";
import dialogService from "../../services/dialog.js";
import { initSyntaxHighlighting } from "./ckeditor/syntax_highlight.js";
import options from "../../services/options.js";
import { isSyntaxHighlightEnabled } from "../../services/syntax_highlight.js";

const ENABLE_INSPECTOR = false;

Expand Down Expand Up @@ -124,7 +125,6 @@ export default class EditableTextTypeWidget extends AbstractTextTypeWidget {

async initEditor() {
await libraryLoader.requireLibrary(libraryLoader.CKEDITOR);
await libraryLoader.requireLibrary(libraryLoader.HIGHLIGHT_JS);

const codeBlockLanguages = buildListOfLanguages();

Expand Down Expand Up @@ -171,9 +171,7 @@ export default class EditableTextTypeWidget extends AbstractTextTypeWidget {
this.watchdog.setCreator(async (elementOrData, editorConfig) => {
const editor = await BalloonEditor.create(elementOrData, editorConfig);

if (options.get("codeBlockTheme") !== "none") {
initSyntaxHighlighting(editor);
}
await initSyntaxHighlighting(editor);

editor.model.document.on('change:data', () => this.spacedUpdate.scheduleUpdate());

Expand Down
5 changes: 2 additions & 3 deletions src/public/app/widgets/type_widgets/read_only_text.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ export default class ReadOnlyTextTypeWidget extends AbstractTextTypeWidget {
// we load CKEditor also for read only notes because they contain content styles required for correct rendering of even read only notes
// we could load just ckeditor-content.css but that causes CSS conflicts when both build CSS and this content CSS is loaded at the same time
// (see https://github.com/zadam/trilium/issues/1590 for example of such conflict)
await libraryLoader.requireLibrary(libraryLoader.CKEDITOR);
await libraryLoader.requireLibrary(libraryLoader.HIGHLIGHT_JS);
await libraryLoader.requireLibrary(libraryLoader.CKEDITOR);

const blob = await note.getBlob();

Expand All @@ -113,7 +112,7 @@ export default class ReadOnlyTextTypeWidget extends AbstractTextTypeWidget {
renderMathInElement(this.$content[0], {trust: true});
}

applySyntaxHighlight(this.$content);
await applySyntaxHighlight(this.$content);
}

async refreshIncludedNoteEvent({noteId}) {
Expand Down

0 comments on commit 9e3c1b4

Please sign in to comment.