fix(snapshot): preserve previews when syntax highlighting fails

Closes #6777
This commit is contained in:
t8y2 committed 2026-08-20 14:33:10 +00:00
1 parent 69e5e81071
commit 5767a0fd59
2 files changed
+63 -10

No files matched your search

@@ -150,6 +150,10 @@ function escapeHtml(value: string): string {
return value.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll('"', "&quot;").replaceAll("'", "&#39;");
}
function renderEscapedSnapshotLines(value: string): string {
return `<span class="line">${escapeHtml(value).replace(/\r\n|\r|\n/g, '</span><span class="line">')}</span>`;
}
function renderSnapshotBar(title: string | undefined, appearance: AppThemeAppearance, showTrafficLights: boolean): string {
const dots = showTrafficLights ? TRAFFIC_LIGHT_COLORS.map((color) => `<span class="dbx-code-snapshot__dot" style="background:${color}"></span>`).join("") : "";
const titleHtml = title ? `<span class="dbx-code-snapshot__title" style="color:${SNAPSHOT_BAR_TEXT[appearance]}">${escapeHtml(title)}</span>` : "";
@@ -162,8 +166,14 @@ function renderSnapshotBar(title: string | undefined, appearance: AppThemeAppear
* `v-html`, then exported with `snapshotElementToPng`.
*/
export async function renderCodeSnapshotHtml(source: CodeSnapshotSource, options: CodeSnapshotStyleOptions): Promise<string> {
const highlight = await getCodeSnapshotHighlighter();
const body = highlight(source.code, source.lang, options.appearance);
let body: string;
try {
const highlight = await getCodeSnapshotHighlighter();
body = highlight(source.code, source.lang, options.appearance);
} catch {
// Legacy WebKit can reject regular expressions generated by Shiki.
body = renderEscapedSnapshotLines(source.code);
}
const appearance = options.appearance;
const showBar = options.showTrafficLights !== false || !!source.title;