@salesforcedevs/dx-components 1.20.7 → 1.20.8-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-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,6 +118,27 @@ 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");
@@ -162,6 +183,9 @@ export default class CodeBlock extends LightningElement {
162
183
  ? "html"
163
184
  : this.selectedLanguage.id || "text";
164
185
 
186
+ // Render plain code immediately
187
+ this.renderPlainCode(cleanCodeBlock, templateEl, divEl);
188
+
165
189
  try {
166
190
  // Use Shiki to highlight the code with current theme
167
191
  const highlightedHtml = await highlightCode(
@@ -1,110 +1,24 @@
1
- // Mock for shiki module
2
- export const createHighlighter = jest.fn().mockImplementation(() => {
3
- return Promise.resolve({
4
- codeToHtml: jest.fn().mockImplementation((code, options) => {
5
- // Preserve the original code content and add variable elements if present
6
- let htmlContent = code;
1
+ // Mock for Shiki module used in tests
2
+ // This mock provides fallback implementations when Shiki can't load in test environment
3
+
4
+ // Export the real escapeHtml function for use in tests
5
+ export function escapeHtml(text: string): string {
6
+ const div = document.createElement("div");
7
+ div.textContent = text;
8
+ return div.innerHTML;
9
+ }
10
+
11
+ // Mock highlightCode that returns plain text fallback
12
+ // This simulates the behavior when Shiki fails to load
13
+ export async function highlightCode(
14
+ code: string,
15
+ language: string,
16
+ theme: "light" | "dark" = "light"
17
+ ): Promise<string> {
18
+ // Return plain text fallback just like the real function does when it fails
19
+ const bgColor = theme === "dark" ? "#212121" : "#FFFFFF";
20
+ const textColor = theme === "dark" ? "#EEFFFF" : "#000000";
21
+
22
+ return `<pre class="shiki ${theme}" style="background-color: ${bgColor}; color: ${textColor};"><code>${escapeHtml(code)}</code></pre>`;
23
+ }
7
24
 
8
- // Check for <var> elements in the code and preserve them
9
- if (code.includes("<var>")) {
10
- // Replace <var> elements with tokenized spans
11
- htmlContent = code.replace(
12
- /<var>(.*?)<\/var>/g,
13
- (match, content) =>
14
- `<span class="token variable"><var>${content}</var></span>`
15
- );
16
- }
17
-
18
- // Add test content for specific test cases
19
- if (code.includes("Salesforce Functions")) {
20
- htmlContent = "Connected to Salesforce Functions";
21
- }
22
-
23
- return `<pre class="shiki ${
24
- options?.theme || "light"
25
- }"><code>${htmlContent}</code></pre>`;
26
- }),
27
- getLoadedLanguages: jest
28
- .fn()
29
- .mockReturnValue([
30
- "javascript",
31
- "typescript",
32
- "html",
33
- "css",
34
- "apex",
35
- "text",
36
- "handlebars"
37
- ])
38
- });
39
- });
40
-
41
- export type Highlighter = {
42
- codeToHtml: (code: string, options: any) => string;
43
- getLoadedLanguages: () => string[];
44
- };
45
-
46
- export type BundledLanguage = string;
47
- export type BundledTheme = string;
48
-
49
- export const codeToHast = jest.fn();
50
- export const codeToHtml = jest.fn();
51
- export const codeToTokens = jest.fn();
52
- export const codeToTokensBase = jest.fn();
53
- export const codeToTokensWithThemes = jest.fn();
54
- export const getLastGrammarState = jest.fn();
55
- export const getSingletonHighlighter = jest.fn();
56
-
57
- // Mock for shiki/core exports
58
- export const createHighlighterCore = jest.fn().mockImplementation(() => {
59
- return Promise.resolve({
60
- codeToHtml: jest.fn().mockImplementation((code, options) => {
61
- // Preserve the original code content and add variable elements if present
62
- let htmlContent = code;
63
-
64
- // Check for <var> elements in the code and preserve them
65
- if (code.includes("<var>")) {
66
- // Replace <var> elements with tokenized spans
67
- htmlContent = code.replace(
68
- /<var>(.*?)<\/var>/g,
69
- (match, content) =>
70
- `<span class="token variable"><var>${content}</var></span>`
71
- );
72
- }
73
-
74
- // Add test content for specific test cases
75
- if (code.includes("Salesforce Functions")) {
76
- htmlContent = "Connected to Salesforce Functions";
77
- }
78
-
79
- return `<pre class="shiki ${
80
- options?.theme || "light"
81
- }"><code>${htmlContent}</code></pre>`;
82
- }),
83
- getLoadedLanguages: jest
84
- .fn()
85
- .mockReturnValue([
86
- "javascript",
87
- "typescript",
88
- "html",
89
- "css",
90
- "apex",
91
- "text",
92
- "handlebars"
93
- ]),
94
- loadLanguage: jest.fn().mockResolvedValue(undefined)
95
- });
96
- });
97
-
98
- export type HighlighterCore = {
99
- codeToHtml: (code: string, options: any) => string;
100
- getLoadedLanguages: () => string[];
101
- loadLanguage: (lang: any) => Promise<void>;
102
- };
103
-
104
- // Mock for shiki/engine/oniguruma
105
- export const createOnigurumaEngine = jest.fn().mockImplementation(() => {
106
- return Promise.resolve({});
107
- });
108
-
109
- // Mock for shiki/wasm - just return a promise that resolves to a buffer
110
- export default Promise.resolve(new ArrayBuffer(0));
@@ -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.