@bendyline/squisq-react 2.12.2 → 2.12.4

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.
@@ -1,8 +1,70 @@
1
+ // src/svgIsland/svgImageUrl.ts
2
+ import { useEffect, useState } from "react";
3
+ import {
4
+ parseSvgIsland,
5
+ svgIslandDataUrl,
6
+ svgSourceFromBytes,
7
+ svgSourceFromDataUrl
8
+ } from "@bendyline/squisq/markdown";
9
+ function isLocalSvgUrl(url) {
10
+ if (/^(?:blob|data):/i.test(url)) return true;
11
+ return !/^[a-z][a-z0-9+.-]*:/i.test(url) && !url.startsWith("//");
12
+ }
13
+ var CACHE_LIMIT = 64;
14
+ var cache = /* @__PURE__ */ new Map();
15
+ async function load(url, isStatic) {
16
+ let source = svgSourceFromDataUrl(url);
17
+ if (source === null) {
18
+ const response = await fetch(url);
19
+ if (!response.ok) return "";
20
+ source = svgSourceFromBytes(await response.arrayBuffer());
21
+ }
22
+ const result = parseSvgIsland(source, { static: isStatic });
23
+ return result.ok ? svgIslandDataUrl(result.island) : "";
24
+ }
25
+ function sanitizedSvgImageUrl(url, options = {}) {
26
+ const isStatic = options.static === true;
27
+ const key = `${isStatic ? "s" : "a"}|${url}`;
28
+ let pending = cache.get(key);
29
+ if (!pending) {
30
+ pending = load(url, isStatic).catch(() => "");
31
+ cache.set(key, pending);
32
+ if (cache.size > CACHE_LIMIT) {
33
+ const oldest = cache.keys().next().value;
34
+ if (oldest !== void 0) cache.delete(oldest);
35
+ }
36
+ }
37
+ return pending;
38
+ }
39
+ function svgDataImageSrc(src) {
40
+ const trimmed = src?.trim() ?? "";
41
+ return /^data:image\/svg\+xml[;,]/i.test(trimmed) ? trimmed : null;
42
+ }
43
+ function useSanitizedSvgImage(url, isSvg, options = {}) {
44
+ const isStatic = options.static === true;
45
+ const needsSanitizing = isSvg && url !== "" && isLocalSvgUrl(url);
46
+ const [sanitized, setSanitized] = useState(null);
47
+ useEffect(() => {
48
+ if (!needsSanitizing) return;
49
+ let cancelled = false;
50
+ void sanitizedSvgImageUrl(url, { static: isStatic }).then((result) => {
51
+ if (!cancelled) setSanitized({ url, isStatic, result });
52
+ });
53
+ return () => {
54
+ cancelled = true;
55
+ };
56
+ }, [needsSanitizing, url, isStatic]);
57
+ if (!needsSanitizing) return url;
58
+ if (sanitized?.url !== url || sanitized.isStatic !== isStatic) return void 0;
59
+ return sanitized.result || null;
60
+ }
61
+
1
62
  // src/hooks/MediaContext.tsx
2
- import { createContext, useContext, useState, useEffect, useMemo } from "react";
63
+ import { createContext, useCallback, useContext, useState as useState2, useEffect as useEffect2, useMemo } from "react";
3
64
  import {
4
65
  DEFAULT_INTERACTIVE_RESOURCE_POLICY,
5
- isResourceUrlAllowed
66
+ isResourceUrlAllowed,
67
+ isSvgImageSource
6
68
  } from "@bendyline/squisq/markdown";
7
69
  var MediaContext = createContext(null);
8
70
  var ResourcePolicyContext = createContext(
@@ -14,18 +76,23 @@ function useResourcePolicy() {
14
76
  function useMediaProvider() {
15
77
  return useContext(MediaContext);
16
78
  }
17
- function useMediaUrl(relativePath, basePath) {
79
+ function useMediaUrl(relativePath, basePath, options = {}) {
18
80
  const provider = useMediaProvider();
19
81
  const resourcePolicy = useResourcePolicy();
20
82
  const safePath = typeof relativePath === "string" ? relativePath : "";
83
+ const isSvg = isSvgImageSource(safePath);
84
+ const allowed = useCallback(
85
+ (url2) => isResourceUrlAllowed(url2, resourcePolicy) || isSvg && /^data:image\/svg\+xml[;,]/i.test(url2),
86
+ [resourcePolicy, isSvg]
87
+ );
21
88
  const isAbsolute = !safePath || /^(?:[a-z][a-z0-9+.-]*:|\/\/|\/)/i.test(safePath);
22
89
  const fallback = useMemo(() => {
23
90
  const candidate = isAbsolute ? safePath : `${basePath.replace(/\/$/, "")}/${safePath}`;
24
- return isResourceUrlAllowed(candidate, resourcePolicy) ? candidate : "";
25
- }, [isAbsolute, safePath, basePath, resourcePolicy]);
91
+ return allowed(candidate) ? candidate : "";
92
+ }, [isAbsolute, safePath, basePath, allowed]);
26
93
  const needsProvider = !isAbsolute && !!provider;
27
- const [resolved, setResolved] = useState(null);
28
- useEffect(() => {
94
+ const [resolved, setResolved] = useState2(null);
95
+ useEffect2(() => {
29
96
  if (!needsProvider) {
30
97
  return;
31
98
  }
@@ -37,7 +104,7 @@ function useMediaUrl(relativePath, basePath) {
37
104
  path: safePath,
38
105
  provider,
39
106
  policy: resourcePolicy,
40
- url: isResourceUrlAllowed(resolved2, resourcePolicy) ? resolved2 : ""
107
+ url: allowed(resolved2) ? resolved2 : ""
41
108
  });
42
109
  }
43
110
  },
@@ -49,8 +116,9 @@ function useMediaUrl(relativePath, basePath) {
49
116
  return () => {
50
117
  cancelled = true;
51
118
  };
52
- }, [needsProvider, provider, safePath, fallback, resourcePolicy]);
53
- return !needsProvider ? fallback : resolved?.path === safePath && resolved.provider === provider && resolved.policy === resourcePolicy ? resolved.url : "";
119
+ }, [needsProvider, provider, safePath, fallback, resourcePolicy, allowed]);
120
+ const url = !needsProvider ? fallback : resolved?.path === safePath && resolved.provider === provider && resolved.policy === resourcePolicy ? resolved.url : "";
121
+ return useSanitizedSvgImage(url, isSvg, { static: options.staticSvg === true }) ?? "";
54
122
  }
55
123
 
56
124
  // src/mermaid/mermaidRuntime.ts
@@ -116,6 +184,10 @@ function mermaidRenderErrorMessage(error) {
116
184
  }
117
185
 
118
186
  export {
187
+ isLocalSvgUrl,
188
+ sanitizedSvgImageUrl,
189
+ svgDataImageSrc,
190
+ useSanitizedSvgImage,
119
191
  MediaContext,
120
192
  ResourcePolicyContext,
121
193
  useResourcePolicy,
@@ -5,13 +5,14 @@ import {
5
5
  InlineVideoPlayer,
6
6
  MarkdownRenderer,
7
7
  VideoEmbedFrame
8
- } from "./chunk-T7NVETOH.js";
8
+ } from "./chunk-YVQ7KEYI.js";
9
9
  import {
10
10
  BlockRenderer
11
- } from "./chunk-XSSIHHDY.js";
11
+ } from "./chunk-PFKYO6C2.js";
12
12
  import {
13
+ svgDataImageSrc,
13
14
  useMediaUrl
14
- } from "./chunk-HCKL2AFN.js";
15
+ } from "./chunk-JP2VHB5H.js";
15
16
 
16
17
  // src/page/PageViewContext.ts
17
18
  import { createContext, useContext } from "react";
@@ -87,7 +88,7 @@ function PageImg({
87
88
  className
88
89
  }) {
89
90
  const { basePath } = usePageView();
90
- const safeSrc = sanitizeUrl(media.src, "media");
91
+ const safeSrc = sanitizeUrl(media.src, "media") ?? svgDataImageSrc(media.src);
91
92
  const resolved = useMediaUrl(safeSrc ?? "", basePath);
92
93
  if (!safeSrc) return null;
93
94
  return /* @__PURE__ */ jsx2(
@@ -3,7 +3,7 @@ import {
3
3
  } from "./chunk-TT6ENR6T.js";
4
4
  import {
5
5
  MarkdownRenderer
6
- } from "./chunk-T7NVETOH.js";
6
+ } from "./chunk-YVQ7KEYI.js";
7
7
 
8
8
  // src/jsonView/useJsonViewTokens.ts
9
9
  import { useMemo } from "react";
@@ -4,6 +4,7 @@ import {
4
4
  MermaidLayer,
5
5
  PathLayer,
6
6
  ShapeLayer,
7
+ SvgLayer,
7
8
  TableLayer,
8
9
  TextLayer,
9
10
  TreeLayer,
@@ -11,7 +12,7 @@ import {
11
12
  getAnchorOffset,
12
13
  getTransitionClass,
13
14
  resolveValue
14
- } from "./chunk-3RVUSY4V.js";
15
+ } from "./chunk-4ILJDBJ7.js";
15
16
 
16
17
  // src/BlockRenderer.tsx
17
18
  import { useEffect, useId, useLayoutEffect, useMemo, useRef, useState } from "react";
@@ -268,7 +269,8 @@ function BlockRenderer({
268
269
  theme,
269
270
  tableContentRenderer,
270
271
  growTextToFit = false,
271
- videoEmbeds = "poster"
272
+ videoEmbeds = "poster",
273
+ staticMedia = false
272
274
  }) {
273
275
  let transitionClass = "";
274
276
  const transitionStyle = {};
@@ -400,7 +402,8 @@ function BlockRenderer({
400
402
  theme,
401
403
  tableContentRenderer,
402
404
  textScale,
403
- videoEmbeds
405
+ videoEmbeds,
406
+ staticMedia: staticMedia || reducedMotion || !animationsEnabled
404
407
  },
405
408
  layer.id
406
409
  )) })
@@ -420,7 +423,8 @@ function LayerRenderer({
420
423
  theme,
421
424
  tableContentRenderer,
422
425
  textScale,
423
- videoEmbeds
426
+ videoEmbeds,
427
+ staticMedia
424
428
  }) {
425
429
  const renderedLayer = animationsEnabled || !layer.animation ? layer : { ...layer, animation: void 0 };
426
430
  switch (renderedLayer.type) {
@@ -432,7 +436,8 @@ function LayerRenderer({
432
436
  basePath,
433
437
  viewport,
434
438
  blockTime,
435
- animationsEnabled
439
+ animationsEnabled,
440
+ isStatic: staticMedia
436
441
  }
437
442
  );
438
443
  case "text":
@@ -496,6 +501,16 @@ function LayerRenderer({
496
501
  theme
497
502
  }
498
503
  );
504
+ case "svg":
505
+ return /* @__PURE__ */ jsx2(
506
+ SvgLayer,
507
+ {
508
+ layer: renderedLayer,
509
+ viewport,
510
+ blockTime,
511
+ isStatic: staticMedia
512
+ }
513
+ );
499
514
  default:
500
515
  console.warn(`Unknown layer type: ${renderedLayer.type}`);
501
516
  return null;