@salesforcedevs/dx-components 1.18.5 → 1.18.6-dynamic-pg-border2
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 +4 -7
- package/src/modules/dx/codeBlock/codeBlock.css +16 -10
- package/src/modules/dx/codeBlock/codeBlock.ts +81 -71
- package/src/modules/dx/mainContentHeader/mainContentHeader.css +1 -1
- package/src/modules/dx/mainContentHeader/mainContentHeader.html +2 -0
- package/src/modules/dx/mainContentHeader/mainContentHeader.ts +1 -1
- package/src/modules/dx/tabbedCodeBlock/tabbedCodeBlock.css +0 -1
- package/src/modules/dx/toc/toc.css +1 -1
- package/src/modules/dxHelpers/code/code.css +19 -87
- package/src/modules/dxUtils/shiki/__mocks__/shiki.ts +110 -0
- package/src/modules/dxUtils/shiki/__mocks__/shikijs.ts +23 -0
- package/src/modules/dxUtils/shiki/shiki.html +3 -0
- package/src/modules/dxUtils/shiki/shiki.ts +192 -0
- package/src/modules/dxUtils/shikiGrammars/shikiGrammars.html +3 -0
- package/src/modules/dxUtils/shikiGrammars/shikiGrammars.ts +2165 -0
- package/LICENSE +0 -12
- package/src/modules/dx/codeBlock/customLanguages.js +0 -48
- package/src/modules/dx/codeBlock/darkAndLightThemes.css +0 -177
- package/src/modules/dxUtils/prismjs/prismjs.html +0 -3
- package/src/modules/dxUtils/prismjs/prismjs.ts +0 -4183
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import * as shiki from "shiki";
|
|
2
|
+
|
|
3
|
+
import type { BundledLanguage, BundledTheme } from "shiki";
|
|
4
|
+
import { transformerColorizedBrackets } from "@shikijs/colorized-brackets";
|
|
5
|
+
import { getCustomLanguageGrammars } from "dxUtils/shikiGrammars";
|
|
6
|
+
|
|
7
|
+
interface ShikiSingleton {
|
|
8
|
+
highlighter: shiki.HighlighterCore | null;
|
|
9
|
+
initialized: boolean;
|
|
10
|
+
initPromise: Promise<shiki.HighlighterCore> | null;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const shikiInstance: ShikiSingleton = {
|
|
14
|
+
highlighter: null,
|
|
15
|
+
initialized: false,
|
|
16
|
+
initPromise: null
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// Theme mapping for light/dark modes
|
|
20
|
+
const THEME_MAP: Record<string, BundledTheme> = {
|
|
21
|
+
light: "light-plus",
|
|
22
|
+
dark: "material-theme-darker"
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const THEME_MAP_COLOR_REPLACEMENTS: Record<
|
|
26
|
+
string,
|
|
27
|
+
{ colorReplacements: Record<string, string> }
|
|
28
|
+
> = {
|
|
29
|
+
light: {
|
|
30
|
+
colorReplacements: {}
|
|
31
|
+
},
|
|
32
|
+
dark: {
|
|
33
|
+
colorReplacements: {
|
|
34
|
+
"#545454": "#8A8A8A" // Replace comment color for WCAG 2.1 compliance
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// Language mapping for custom languages to supported ones
|
|
40
|
+
const LANGUAGE_MAP: Record<string, BundledLanguage> = {
|
|
41
|
+
ssjs: "javascript", // Server-Side JavaScript
|
|
42
|
+
gtl: "handlebars", // Guide Template Language similar to Handlebars
|
|
43
|
+
lwc: "jsx",
|
|
44
|
+
sql_docs_template: "sql", // Custom SQL variant
|
|
45
|
+
visualforce: "html", // Visualforce uses HTML-like syntax
|
|
46
|
+
js: "javascript",
|
|
47
|
+
ts: "typescript",
|
|
48
|
+
yml: "yaml",
|
|
49
|
+
md: "markdown",
|
|
50
|
+
sh: "bash"
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// Core languages that should be loaded immediately for best performance
|
|
54
|
+
const CORE_LANGUAGES = [
|
|
55
|
+
"apex",
|
|
56
|
+
"javascript",
|
|
57
|
+
"html",
|
|
58
|
+
"css",
|
|
59
|
+
"json",
|
|
60
|
+
"sql",
|
|
61
|
+
"bash",
|
|
62
|
+
"xml",
|
|
63
|
+
"graphql",
|
|
64
|
+
"jsx",
|
|
65
|
+
getCustomLanguageGrammars().ampscript
|
|
66
|
+
];
|
|
67
|
+
|
|
68
|
+
// Non-critical languages that can be loaded asynchronously when needed
|
|
69
|
+
const OPTIONAL_LANGUAGES: Record<string, any> = {
|
|
70
|
+
// @ts-ignore
|
|
71
|
+
typescript: "typescript",
|
|
72
|
+
// @ts-ignore
|
|
73
|
+
markdown: "markdown",
|
|
74
|
+
// @ts-ignore
|
|
75
|
+
python: "python",
|
|
76
|
+
// @ts-ignore
|
|
77
|
+
java: "java",
|
|
78
|
+
// @ts-ignore
|
|
79
|
+
yaml: "yaml",
|
|
80
|
+
// @ts-ignore
|
|
81
|
+
php: "php",
|
|
82
|
+
// @ts-ignore
|
|
83
|
+
swift: "swift",
|
|
84
|
+
// @ts-ignore
|
|
85
|
+
kotlin: "kotlin",
|
|
86
|
+
// @ts-ignore
|
|
87
|
+
handlebars: "handlebars",
|
|
88
|
+
// @ts-ignore
|
|
89
|
+
dataweave: getCustomLanguageGrammars().dataweave,
|
|
90
|
+
// @ts-ignore
|
|
91
|
+
afscript: getCustomLanguageGrammars().afscript
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
// Initialize Shiki highlighter with fine-grained modules for better performance
|
|
95
|
+
async function initializeShiki(): Promise<shiki.HighlighterCore> {
|
|
96
|
+
if (shikiInstance.highlighter) {
|
|
97
|
+
return shikiInstance.highlighter;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (shikiInstance.initPromise) {
|
|
101
|
+
return shikiInstance.initPromise;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
shikiInstance.initPromise = shiki.createHighlighter({
|
|
105
|
+
themes: ["light-plus", "material-theme-darker"],
|
|
106
|
+
langs: CORE_LANGUAGES
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
try {
|
|
110
|
+
shikiInstance.highlighter = await shikiInstance.initPromise;
|
|
111
|
+
shikiInstance.initialized = true;
|
|
112
|
+
return shikiInstance.highlighter;
|
|
113
|
+
} catch (error) {
|
|
114
|
+
console.error("Failed to initialize Shiki:", error);
|
|
115
|
+
shikiInstance.initPromise = null;
|
|
116
|
+
throw error;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Async function to load additional languages when needed
|
|
121
|
+
async function loadLanguageIfNeeded(
|
|
122
|
+
highlighter: shiki.HighlighterCore,
|
|
123
|
+
language: string
|
|
124
|
+
): Promise<void> {
|
|
125
|
+
const loadedLanguages = highlighter.getLoadedLanguages();
|
|
126
|
+
|
|
127
|
+
if (!loadedLanguages.includes(language as BundledLanguage)) {
|
|
128
|
+
// Check if it's an optional language that needs to be loaded
|
|
129
|
+
const languageLoader = OPTIONAL_LANGUAGES[language];
|
|
130
|
+
|
|
131
|
+
if (languageLoader) {
|
|
132
|
+
try {
|
|
133
|
+
await highlighter.loadLanguage(languageLoader);
|
|
134
|
+
} catch (error) {
|
|
135
|
+
console.warn(
|
|
136
|
+
`Failed to load optional language ${language}:`,
|
|
137
|
+
error
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Get the mapped language or fallback to the original
|
|
145
|
+
function getMappedLanguage(language: string): BundledLanguage {
|
|
146
|
+
const mapped =
|
|
147
|
+
LANGUAGE_MAP[language.toLowerCase()] || language.toLowerCase();
|
|
148
|
+
return mapped as BundledLanguage;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Highlight code with Shiki
|
|
152
|
+
export async function highlightCode(
|
|
153
|
+
code: string,
|
|
154
|
+
language: string,
|
|
155
|
+
theme: "light" | "dark" = "light"
|
|
156
|
+
): Promise<string> {
|
|
157
|
+
try {
|
|
158
|
+
const highlighter = await initializeShiki();
|
|
159
|
+
let mappedLanguage = getMappedLanguage(language);
|
|
160
|
+
|
|
161
|
+
// Try to load the language asynchronously if it's not already loaded
|
|
162
|
+
await loadLanguageIfNeeded(highlighter, mappedLanguage);
|
|
163
|
+
|
|
164
|
+
// Check if the language is supported after potential async loading
|
|
165
|
+
const loadedLanguages = highlighter.getLoadedLanguages();
|
|
166
|
+
if (!loadedLanguages.includes(mappedLanguage)) {
|
|
167
|
+
mappedLanguage = "text" as BundledLanguage;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return highlighter.codeToHtml(code, {
|
|
171
|
+
lang: mappedLanguage,
|
|
172
|
+
theme: THEME_MAP[theme],
|
|
173
|
+
transformers: [transformerColorizedBrackets()],
|
|
174
|
+
colorReplacements:
|
|
175
|
+
THEME_MAP_COLOR_REPLACEMENTS[theme].colorReplacements
|
|
176
|
+
});
|
|
177
|
+
} catch (error) {
|
|
178
|
+
console.error("Failed to highlight code with Shiki:", error);
|
|
179
|
+
// Fallback to plain text on error
|
|
180
|
+
return `<pre class="shiki ${theme}"><code>${escapeHtml(
|
|
181
|
+
code
|
|
182
|
+
)}</code></pre>`;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Utility function to escape HTML
|
|
187
|
+
function escapeHtml(text: string): string {
|
|
188
|
+
const div = document.createElement("div");
|
|
189
|
+
div.textContent = text;
|
|
190
|
+
// eslint-disable-next-line @lwc/lwc/no-inner-html
|
|
191
|
+
return div.innerHTML;
|
|
192
|
+
}
|