Skip to content

Commit

Permalink
fix: manually load code languages on our end (fixes file protocol iss…
Browse files Browse the repository at this point in the history
…ues)
  • Loading branch information
valeriansaliou committed Jul 28, 2024
1 parent 37c59b0 commit 5c07b16
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
5 changes: 2 additions & 3 deletions .parcelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"extends": [
"@parcel/config-default"
]
"extends": ["@parcel/config-default"],
"resolvers": ["@parcel/resolver-default", "@parcel/resolver-glob"]
}
25 changes: 19 additions & 6 deletions src/messaging/components/message/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/messaging/libraries/highlighting/languages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* prose-core-views
*
* Copyright: 2024, Valerian Saliou <[email protected]>
* 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;

0 comments on commit 5c07b16

Please sign in to comment.