gradio-pr-bot commited on
Commit
6e30e22
·
verified ·
1 Parent(s): 3e1196e

Upload folder using huggingface_hub

Browse files
6.22.0/paramviewer/ParamViewer.svelte CHANGED
@@ -1,33 +1,34 @@
1
  <script module lang="ts">
2
- import Prism from "prismjs";
3
-
4
- // `prismjs/components/*` are side-effect scripts: each does
5
- // `Prism.languages.x = ...` against a bare global and declares no imports of
6
- // its own, so nothing in the module graph orders them after prismjs. As
7
- // *static* imports the bundler is free to hoist them into a chunk that
8
- // evaluates before the assignment below, and rolldown compounds it by
9
- // emitting prismjs as a lazily-invoked CommonJS factory — so importing it
10
- // does not initialise it either, and `window.Prism` stayed undefined. Every
11
- // docs page then died during hydration on `ReferenceError: Prism is not
12
- // defined` (#13677).
13
- //
14
- // Dynamic imports are not hoisted, so the global below is guaranteed to be in
15
- // place before any grammar runs.
16
- (globalThis as any).Prism = Prism;
17
-
18
- // Awaited here rather than gated behind a promise: the component must not
19
- // render before the grammars are registered. prismjs schedules its own
20
- // `highlightAll()` on a rAF as soon as it loads, and without a grammar
21
- // `highlightElement` overwrites the element with `encode(textContent)` —
22
- // which strips the server's highlighting *and* the hydration comments the
23
- // `{@html}` blocks own. Anything derived later then repaints into detached
24
- // nodes, so the types stay plain until the component is recreated.
25
- try {
26
- await import("prismjs/components/prism-python");
27
- await import("prismjs/components/prism-typescript");
28
- } catch (e) {
29
- // `highlight` already falls back to plain text without a grammar.
30
- console.error("failed to load Prism grammars", e);
 
31
  }
32
  </script>
33
 
@@ -60,8 +61,23 @@
60
  let component_root: HTMLElement;
61
  let all_open = $state(false);
62
  let lang: "python" | "typescript" = "python";
 
63
 
64
- let _docs = $derived(highlight_code(docs, lang));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
  function create_slug(name: string, anchor_links: string | boolean): string {
67
  let prefix = "param-";
@@ -72,8 +88,8 @@
72
  }
73
 
74
  function highlight(code: string, lang: "python" | "typescript"): string {
75
- let highlighted = Prism.languages[lang]
76
- ? Prism.highlight(code, Prism.languages[lang], lang)
77
  : code;
78
 
79
  for (const link of linkify) {
@@ -88,7 +104,8 @@
88
 
89
  function highlight_code(
90
  _docs: typeof docs,
91
- lang: "python" | "typescript"
 
92
  ): Param[] {
93
  if (!_docs) {
94
  return [];
 
1
  <script module lang="ts">
2
+ type PrismApi = typeof import("prismjs");
3
+
4
+ let prism: PrismApi | null = null;
5
+ let prism_languages: Promise<PrismApi> | null = null;
6
+
7
+ function load_prism_languages(): Promise<PrismApi> {
8
+ if (prism_languages) return prism_languages;
9
+
10
+ prism_languages = (async () => {
11
+ // Prism checks this pre-existing global while its module evaluates.
12
+ // Manual mode prevents its scheduled highlightAll() from rewriting
13
+ // Svelte's hydration markers before the grammars are ready.
14
+ (globalThis as any).Prism = { manual: true };
15
+ const prism_module = await import("prismjs");
16
+ const loaded_prism =
17
+ (
18
+ prism_module as unknown as {
19
+ default?: PrismApi;
20
+ }
21
+ ).default ?? (prism_module as PrismApi);
22
+ loaded_prism.manual = true;
23
+ (globalThis as any).Prism = loaded_prism;
24
+ // @ts-expect-error Prism component modules do not ship declarations.
25
+ await import("prismjs/components/prism-python");
26
+ // @ts-expect-error Prism component modules do not ship declarations.
27
+ await import("prismjs/components/prism-typescript");
28
+ prism = loaded_prism;
29
+ return loaded_prism;
30
+ })();
31
+ return prism_languages;
32
  }
33
  </script>
34
 
 
61
  let component_root: HTMLElement;
62
  let all_open = $state(false);
63
  let lang: "python" | "typescript" = "python";
64
+ let prism_ready = $state(prism !== null);
65
 
66
+ $effect(() => {
67
+ let active = true;
68
+ load_prism_languages()
69
+ .then(() => {
70
+ if (active) prism_ready = true;
71
+ })
72
+ .catch((e) => {
73
+ console.error("failed to load Prism grammars", e);
74
+ });
75
+ return () => {
76
+ active = false;
77
+ };
78
+ });
79
+
80
+ let _docs = $derived(highlight_code(docs, lang, prism_ready));
81
 
82
  function create_slug(name: string, anchor_links: string | boolean): string {
83
  let prefix = "param-";
 
88
  }
89
 
90
  function highlight(code: string, lang: "python" | "typescript"): string {
91
+ let highlighted = prism?.languages[lang]
92
+ ? prism.highlight(code, prism.languages[lang], lang)
93
  : code;
94
 
95
  for (const link of linkify) {
 
104
 
105
  function highlight_code(
106
  _docs: typeof docs,
107
+ lang: "python" | "typescript",
108
+ _prism_ready: boolean
109
  ): Param[] {
110
  if (!_docs) {
111
  return [];
6.22.0/paramviewer/package.json CHANGED
@@ -1,6 +1,6 @@
1
  {
2
  "name": "@gradio/paramviewer",
3
- "version": "0.11.1",
4
  "description": "Gradio UI packages",
5
  "type": "module",
6
  "author": "",
 
1
  {
2
  "name": "@gradio/paramviewer",
3
+ "version": "0.12.0",
4
  "description": "Gradio UI packages",
5
  "type": "module",
6
  "author": "",