@ai-react-markdown/core 1.4.0 → 1.4.2

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/dist/index.d.cts CHANGED
@@ -217,10 +217,18 @@ interface AIMarkdownRenderState<TConfig extends AIMarkdownRenderConfig = AIMarkd
217
217
  documentId: string;
218
218
  /**
219
219
  * Per-document URI-safe id prefix used by all clobberable attributes
220
- * (`id="…"` / `href="#…"`). Derived once by the provider as
221
- * `${encodeURIComponent(documentId)}-user-content-` and exposed here so
222
- * downstream consumers (placeholder components, cross-chunk anchor logic)
223
- * read a single canonical source instead of recomputing locally.
220
+ * (`id="…"` / `href="#…"`). Derived once by the provider and exposed here
221
+ * so downstream consumers (placeholder components, cross-chunk anchor
222
+ * logic) read a single canonical source instead of recomputing locally.
223
+ *
224
+ * Derivation is `${encodeURIComponent(shortenDocumentId(documentId))}-user-content-`:
225
+ * `documentId` is first run through a MurmurHash3 → Base62 shortener
226
+ * (no-op for ids ≤16 chars, ≤6-char hash otherwise) to keep the rendered
227
+ * HTML compact when consumers pass long UUIDs/nanoids. The shortening
228
+ * is purely a render-output concern — `state.documentId` itself is left
229
+ * raw, and registry keying (`useDocumentRegistry`) reads that raw value.
230
+ * Do not reconstruct this prefix from `documentId` at call sites; always
231
+ * read it through this field.
224
232
  */
225
233
  clobberPrefix: string;
226
234
  /** Active render configuration. */
package/dist/index.d.ts CHANGED
@@ -217,10 +217,18 @@ interface AIMarkdownRenderState<TConfig extends AIMarkdownRenderConfig = AIMarkd
217
217
  documentId: string;
218
218
  /**
219
219
  * Per-document URI-safe id prefix used by all clobberable attributes
220
- * (`id="…"` / `href="#…"`). Derived once by the provider as
221
- * `${encodeURIComponent(documentId)}-user-content-` and exposed here so
222
- * downstream consumers (placeholder components, cross-chunk anchor logic)
223
- * read a single canonical source instead of recomputing locally.
220
+ * (`id="…"` / `href="#…"`). Derived once by the provider and exposed here
221
+ * so downstream consumers (placeholder components, cross-chunk anchor
222
+ * logic) read a single canonical source instead of recomputing locally.
223
+ *
224
+ * Derivation is `${encodeURIComponent(shortenDocumentId(documentId))}-user-content-`:
225
+ * `documentId` is first run through a MurmurHash3 → Base62 shortener
226
+ * (no-op for ids ≤16 chars, ≤6-char hash otherwise) to keep the rendered
227
+ * HTML compact when consumers pass long UUIDs/nanoids. The shortening
228
+ * is purely a render-output concern — `state.documentId` itself is left
229
+ * raw, and registry keying (`useDocumentRegistry`) reads that raw value.
230
+ * Do not reconstruct this prefix from `documentId` at call sites; always
231
+ * read it through this field.
224
232
  */
225
233
  clobberPrefix: string;
226
234
  /** Active render configuration. */
package/dist/index.js CHANGED
@@ -1071,6 +1071,64 @@ var defaultAIMarkdownRenderConfig = Object.freeze({
1071
1071
  preserveOrphanReferences: true
1072
1072
  });
1073
1073
 
1074
+ // src/components/shortenDocumentId.ts
1075
+ var BASE62_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
1076
+ function toBase62(n) {
1077
+ if (n === 0) return BASE62_ALPHABET[0];
1078
+ let v = n >>> 0;
1079
+ let out = "";
1080
+ while (v > 0) {
1081
+ out = BASE62_ALPHABET[v % 62] + out;
1082
+ v = Math.floor(v / 62);
1083
+ }
1084
+ return out;
1085
+ }
1086
+ function rotl32(x, r) {
1087
+ return (x << r | x >>> 32 - r) >>> 0;
1088
+ }
1089
+ var C1 = 3432918353;
1090
+ var C2 = 461845907;
1091
+ var UTF8_ENCODER = /* @__PURE__ */ new TextEncoder();
1092
+ function murmur3_32(s, seed = 0) {
1093
+ const bytes = UTF8_ENCODER.encode(s);
1094
+ const len = bytes.length;
1095
+ let h1 = seed >>> 0;
1096
+ const nblocks = len >>> 2;
1097
+ for (let i = 0; i < nblocks; i++) {
1098
+ const b = i * 4;
1099
+ let k1 = bytes[b] | bytes[b + 1] << 8 | bytes[b + 2] << 16 | bytes[b + 3] << 24;
1100
+ k1 = Math.imul(k1, C1);
1101
+ k1 = rotl32(k1, 15);
1102
+ k1 = Math.imul(k1, C2);
1103
+ h1 ^= k1;
1104
+ h1 = rotl32(h1, 13);
1105
+ h1 = Math.imul(h1, 5) + 3864292196 >>> 0;
1106
+ }
1107
+ const tail = nblocks * 4;
1108
+ const rem = len & 3;
1109
+ if (rem > 0) {
1110
+ let k1 = 0;
1111
+ if (rem === 3) k1 ^= bytes[tail + 2] << 16;
1112
+ if (rem >= 2) k1 ^= bytes[tail + 1] << 8;
1113
+ k1 ^= bytes[tail];
1114
+ k1 = Math.imul(k1, C1);
1115
+ k1 = rotl32(k1, 15);
1116
+ k1 = Math.imul(k1, C2);
1117
+ h1 ^= k1;
1118
+ }
1119
+ h1 ^= len;
1120
+ h1 ^= h1 >>> 16;
1121
+ h1 = Math.imul(h1, 2246822507);
1122
+ h1 ^= h1 >>> 13;
1123
+ h1 = Math.imul(h1, 3266489909);
1124
+ h1 ^= h1 >>> 16;
1125
+ return h1 >>> 0;
1126
+ }
1127
+ function shortenDocumentId(id, threshold = 16) {
1128
+ if (id.length <= threshold) return id;
1129
+ return toBase62(murmur3_32(id));
1130
+ }
1131
+
1074
1132
  // src/context.tsx
1075
1133
  import { jsx } from "react/jsx-runtime";
1076
1134
  var AIMarkdownRenderStateContext = createContext(null);
@@ -1126,7 +1184,15 @@ var AIMarkdownRenderStateProvider = ({
1126
1184
  // construction site, not at the documentId storage site, so consumers
1127
1185
  // accessing `documentId` directly still see the raw React-native value
1128
1186
  // (e.g. `useId()`'s `_r_0_`) while id="..."/href="#..." bytes are safe.
1129
- clobberPrefix: `${encodeURIComponent(resolvedDocumentId)}-user-content-`,
1187
+ //
1188
+ // `shortenDocumentId` is applied here (NOT at the documentId storage
1189
+ // site) for the same reason: consumer-supplied UUIDs and nanoids
1190
+ // shouldn't bloat every rendered `id="…"`. Registry keying — which
1191
+ // reads `state.documentId` directly — stays on the raw value, so the
1192
+ // shortening is a pure HTML-output concern and the `useDocumentRegistry`
1193
+ // API surface is unaffected. Pure function ⇒ all chunks sharing one
1194
+ // logical documentId still produce identical prefixes.
1195
+ clobberPrefix: `${encodeURIComponent(shortenDocumentId(resolvedDocumentId))}-user-content-`,
1130
1196
  config: mergedConfig
1131
1197
  }),
1132
1198
  [streaming, fontSize, variant, colorScheme, resolvedDocumentId, mergedConfig]