@salesforcedevs/dx-components 1.20.7 → 1.20.8-pre-shiki

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salesforcedevs/dx-components",
3
- "version": "1.20.7",
3
+ "version": "1.20.8-pre-shiki",
4
4
  "description": "DX Lightning web components",
5
5
  "license": "MIT",
6
6
  "engines": {
@@ -46,6 +46,5 @@
46
46
  "volta": {
47
47
  "node": "20.19.0",
48
48
  "yarn": "1.22.19"
49
- },
50
- "gitHead": "fffbef4d7b536a321824457c6a987e53830a78f1"
49
+ }
51
50
  }
@@ -2,7 +2,7 @@ import { LightningElement, api } from "lwc";
2
2
  import { CodeBlockTheme, CodeBlockLanguage } from "typings/custom";
3
3
  import cx from "classnames";
4
4
  import { track as gtmTrack } from "dxUtils/analytics";
5
- import { highlightCode } from "dxUtils/shiki";
5
+ import { highlightCode, escapeHtml } from "dxUtils/shiki";
6
6
 
7
7
  /*
8
8
  Custom language support is handled by the Shiki wrapper module
@@ -118,10 +118,34 @@ export default class CodeBlock extends LightningElement {
118
118
  this.initializeTheme();
119
119
  }
120
120
 
121
+ // Show plain text immediately, no waiting for highlighting
122
+ private renderPlainCode(
123
+ cleanCodeBlock: string,
124
+ templateEl: HTMLTemplateElement,
125
+ divEl: HTMLElement | null
126
+ ) {
127
+ if (divEl) {
128
+ // Theme-specific colors to match Shiki's output
129
+ const bgColor = this.theme === DARK ? "#212121" : "#FFFFFF";
130
+ const textColor = this.theme === DARK ? "#EEFFFF" : "#000000";
131
+
132
+ // eslint-disable-next-line
133
+ templateEl.innerHTML = `<pre class='shiki' style='background-color: ${bgColor}; color: ${textColor};'><code>${escapeHtml(
134
+ cleanCodeBlock
135
+ )}</code></pre>`;
136
+ // eslint-disable-next-line
137
+ divEl.innerHTML = "";
138
+ divEl.append(templateEl.content);
139
+ }
140
+ }
141
+
121
142
  async formatCodeBlock() {
122
143
  const divEl = this.template.querySelector("div.code-block-content");
123
144
  const templateEl = document.createElement("template");
124
145
 
146
+ // Render plain code immediately
147
+ this.renderPlainCode(this.codeBlock, templateEl, divEl);
148
+
125
149
  // Decode HTML entities if the code is already encoded
126
150
  let cleanCodeBlock = this.codeBlock;
127
151
  if (this.isEncoded) {
@@ -1,13 +1,23 @@
1
- import * as shiki from "shiki";
2
-
3
- import type { BundledLanguage, BundledTheme } from "shiki";
4
- import { transformerColorizedBrackets } from "@shikijs/colorized-brackets";
1
+ import type { BundledLanguage, BundledTheme, HighlighterCore } from "shiki";
5
2
  import { getCustomLanguageGrammars } from "dxUtils/shikiGrammars";
6
3
 
4
+ let shikiModulePromise: Promise<typeof import("shiki")> | null = null;
5
+ let bracketsModulePromise: Promise<
6
+ typeof import("@shikijs/colorized-brackets")
7
+ > | null = null;
8
+
9
+ async function getShiki() {
10
+ return (shikiModulePromise ??= import("shiki"));
11
+ }
12
+
13
+ async function getBrackets() {
14
+ return (bracketsModulePromise ??= import("@shikijs/colorized-brackets"));
15
+ }
16
+
7
17
  interface ShikiSingleton {
8
- highlighter: shiki.HighlighterCore | null;
18
+ highlighter: HighlighterCore | null;
9
19
  initialized: boolean;
10
- initPromise: Promise<shiki.HighlighterCore> | null;
20
+ initPromise: Promise<HighlighterCore> | null;
11
21
  }
12
22
 
13
23
  const shikiInstance: ShikiSingleton = {
@@ -50,8 +60,8 @@ const LANGUAGE_MAP: Record<string, BundledLanguage> = {
50
60
  sh: "bash"
51
61
  };
52
62
 
53
- // Core languages that should be loaded immediately for best performance
54
- const CORE_LANGUAGES = [
63
+ // Core languages loaded eagerly (keep minimal; load others on demand)
64
+ const CORE_LANGUAGES: BundledLanguage[] = [
55
65
  "apex",
56
66
  "javascript",
57
67
  "html",
@@ -91,8 +101,8 @@ const OPTIONAL_LANGUAGES: Record<string, any> = {
91
101
  agentscript: getCustomLanguageGrammars().agentscript
92
102
  };
93
103
 
94
- // Initialize Shiki highlighter with fine-grained modules for better performance
95
- async function initializeShiki(): Promise<shiki.HighlighterCore> {
104
+ // Initialize Shiki highlighter (lazy-load module and keep initial set minimal)
105
+ async function initializeShiki(): Promise<HighlighterCore> {
96
106
  if (shikiInstance.highlighter) {
97
107
  return shikiInstance.highlighter;
98
108
  }
@@ -101,6 +111,7 @@ async function initializeShiki(): Promise<shiki.HighlighterCore> {
101
111
  return shikiInstance.initPromise;
102
112
  }
103
113
 
114
+ const shiki = await getShiki();
104
115
  shikiInstance.initPromise = shiki.createHighlighter({
105
116
  themes: ["light-plus", "material-theme-darker"],
106
117
  langs: CORE_LANGUAGES
@@ -119,7 +130,7 @@ async function initializeShiki(): Promise<shiki.HighlighterCore> {
119
130
 
120
131
  // Async function to load additional languages when needed
121
132
  async function loadLanguageIfNeeded(
122
- highlighter: shiki.HighlighterCore,
133
+ highlighter: HighlighterCore,
123
134
  language: string
124
135
  ): Promise<void> {
125
136
  const loadedLanguages = highlighter.getLoadedLanguages();
@@ -167,6 +178,8 @@ export async function highlightCode(
167
178
  mappedLanguage = "text" as BundledLanguage;
168
179
  }
169
180
 
181
+ const { transformerColorizedBrackets } = await getBrackets();
182
+
170
183
  return highlighter.codeToHtml(code, {
171
184
  lang: mappedLanguage,
172
185
  theme: THEME_MAP[theme],
@@ -184,7 +197,7 @@ export async function highlightCode(
184
197
  }
185
198
 
186
199
  // Utility function to escape HTML
187
- function escapeHtml(text: string): string {
200
+ export function escapeHtml(text: string): string {
188
201
  const div = document.createElement("div");
189
202
  div.textContent = text;
190
203
  // eslint-disable-next-line @lwc/lwc/no-inner-html
package/LICENSE DELETED
@@ -1,12 +0,0 @@
1
- Copyright (c) 2020, Salesforce.com, Inc.
2
- All rights reserved.
3
-
4
- Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
-
6
- * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
-
8
- * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9
-
10
- * Neither the name of Salesforce.com nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11
-
12
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.