diff --git a/src/modules/blocks/code/RenderCode.tsx b/src/modules/blocks/code/RenderCode.tsx index 7810cd8c8..c3432b4a1 100644 --- a/src/modules/blocks/code/RenderCode.tsx +++ b/src/modules/blocks/code/RenderCode.tsx @@ -20,7 +20,7 @@ import { useUIPreferencesStore } from '~/common/state/store-ui'; import { OVERLAY_BUTTON_RADIUS, OverlayButton, overlayButtonsActiveSx, overlayButtonsClassName, overlayButtonsTopRightSx, overlayGroupWithShadowSx, StyledOverlayButton } from '../OverlayButton'; import { RenderCodeHtmlIFrame } from './code-renderers/RenderCodeHtmlIFrame'; import { RenderCodeMermaid } from './code-renderers/RenderCodeMermaid'; -import { RenderCodeSVG } from './code-renderers/RenderCodeSVG'; +import { heuristicIsSVGCode, RenderCodeSVG } from './code-renderers/RenderCodeSVG'; import { RenderCodeSyntax } from './code-renderers/RenderCodeSyntax'; import { heuristicIsBlockPureHTML } from '../danger-html/RenderDangerousHtml'; import { heuristicIsCodePlantUML, RenderCodePlantUML, usePlantUmlSvg } from './code-renderers/RenderCodePlantUML'; @@ -163,7 +163,7 @@ function RenderCodeImpl(props: RenderCodeBaseProps & { const { data: plantUmlSvgData, error: plantUmlError } = usePlantUmlSvg(renderPlantUML, code); renderPlantUML = renderPlantUML && (!!plantUmlSvgData || !!plantUmlError); - const isSVGCode = (code.startsWith('\n'); + const isSVGCode = heuristicIsSVGCode(code); const renderSVG = isSVGCode && showSVG; const canScaleSVG = renderSVG && code.includes('viewBox="'); diff --git a/src/modules/blocks/code/code-renderers/RenderCodeSVG.tsx b/src/modules/blocks/code/code-renderers/RenderCodeSVG.tsx index 26ef5658a..67e811b57 100644 --- a/src/modules/blocks/code/code-renderers/RenderCodeSVG.tsx +++ b/src/modules/blocks/code/code-renderers/RenderCodeSVG.tsx @@ -4,6 +4,26 @@ import type { SxProps } from '@mui/joy/styles/types'; import { Box } from '@mui/joy'; +function _removePotentialComments(code: string): string { + return code.replace(/^()*\s*/i, ''); +} + +/** + * Detects whether a string contains valid SVG trimmedCode + */ +export function heuristicIsSVGCode(trimmedCode: string): boolean { + + // quick outs + if (!trimmedCode.startsWith('<') || !trimmedCode.endsWith('')) return false; + + // strip HTML comments from the start of the trimmedCode + const codeWithoutInitialComments = trimmedCode.startsWith('