diff --git a/.parcelrc b/.parcelrc index 4066202..e218313 100644 --- a/.parcelrc +++ b/.parcelrc @@ -1,5 +1,4 @@ { - "extends": [ - "@parcel/config-default" - ] + "extends": ["@parcel/config-default"], + "resolvers": ["@parcel/resolver-default", "@parcel/resolver-glob"] } diff --git a/src/messaging/components/message/message.js b/src/messaging/components/message/message.js index d78270b..4abaefc 100644 --- a/src/messaging/components/message/message.js +++ b/src/messaging/components/message/message.js @@ -9,9 +9,13 @@ import { nextTick } from "petite-vue"; import { htmlEscape as _e } from "escape-goat"; -import { highlightElement } from "@speed-highlight/core"; +import { + highlightElement, + loadLanguage as highlightLoadLanguage +} from "@speed-highlight/core"; import linkifyHtml from "linkify-html"; import emojiRegex from "emoji-regex"; +import highlightLanguages from "../../libraries/highlighting/languages.js"; import DateHelper from "../../helpers/date.js"; import $store from "../../stores/feed.js"; import $context from "../../stores/option.js"; @@ -503,21 +507,30 @@ function MessagePartText(content) { if (codeElements.length > 0) { for (const codeElement of codeElements) { - // Extract code language (opportunistic) - let codeLanguage = null; + // Extract code language name (opportunistic) + let codeLanguageName = null; for (const className of codeElement.classList) { if (className.startsWith(CODE_HIGHLIGHT_CLASS_PREFIX) === true) { - codeLanguage = + codeLanguageName = className.substring(CODE_HIGHLIGHT_CLASS_PREFIX.length) || null; break; } } - // Any code language found? Apply highlighting + // Acquire code language (if known) + const codeLanguage = highlightLanguages[codeLanguageName] || null; + + // Any code language found? if (codeLanguage !== null) { - await highlightElement(codeElement, codeLanguage); + // Ensure language is loaded + // Notice: this simply inserts the language instance into the map \ + // of loaded languages. + highlightLoadLanguage(codeLanguageName, codeLanguage); + + // Apply highlighting + await highlightElement(codeElement, codeLanguageName); } } } diff --git a/src/messaging/libraries/highlighting/languages.js b/src/messaging/libraries/highlighting/languages.js new file mode 100644 index 0000000..cf5fb41 --- /dev/null +++ b/src/messaging/libraries/highlighting/languages.js @@ -0,0 +1,19 @@ +/* + * prose-core-views + * + * Copyright: 2024, Valerian Saliou + * License: Mozilla Public License v2.0 (MPL v2.0) + */ + +// IMPORTS + +// Notice: languages need to be imported manually there, since the dynamic \ +// 'import()' method as provided by the '@speed-highlight/core' library do \ +// not work in 'file://' loading mode (eg. Tauri builds of the Prose app \ +// implementing the views). This catch-all import depends on the \ +// '@parcel/resolver-glob' resolver. +import * as highlightLanguages from "@speed-highlight/core/dist/languages/*.js"; + +// EXPORTS + +export default highlightLanguages;