Forráskód Böngészése

refactor(editor): replace randomUUID with 6-char NanoID for block IDs

Blocks now get compact IDs like `a3Xk9m` instead of UUIDs — a 77%
reduction in ((reference)) syntax. Uses nanoid's default URL-safe
alphabet (A-Za-z0-9_-). Also removes dead generateId() function
from editor-operations.ts.
Zander Hawke 2 napja
szülő
commit
f4832b2885

+ 1 - 0
packages/editor/package.json

@@ -61,6 +61,7 @@
     "@lezer/markdown": "^1.6.3",
     "@tailwindcss/vite": "^4.3.0",
     "katex": "^0.17.0",
+    "nanoid": "^5.1.11",
     "prosemirror-commands": "^1.7.1",
     "prosemirror-gapcursor": "^1.4.1",
     "prosemirror-keymap": "^1.2.3",

+ 5 - 4
packages/editor/src/lib/block-parser.ts

@@ -1,5 +1,6 @@
 import type { SyntaxNode } from "@lezer/common"
 import { GFM, parser } from "@lezer/markdown"
+import { nanoid } from "nanoid"
 import { createMarkdownExtensions } from "@/lib/markdown-extensions"
 
 export interface EditorBlockData {
@@ -11,11 +12,11 @@ export interface EditorBlockData {
 // ── ID utilities ──────────────────────────────────────────────────
 
 /**
- * Generate a new block identifier. Abstracted from `crypto.randomUUID()`
- * so the caller (or tests) can provide an alternative.
+ * Generate a new block identifier using a 6-character NanoID.
+ * Not every block needs an ID — only blocks referenced via ((id)) do.
  */
 export function generateBlockId(): string {
-  return crypto.randomUUID()
+  return nanoid(6)
 }
 
 /**
@@ -99,7 +100,7 @@ function ownContentEnd(node: SyntaxNode): number {
  */
 export function splitMarkdownIntoBlocks(
   md: string,
-  idFn: () => string = crypto.randomUUID.bind(crypto),
+  idFn: () => string = () => nanoid(6),
   existing?: Pick<EditorBlockData, "id">[],
 ): EditorBlockData[] {
   if (!md.trim()) return []

+ 0 - 4
packages/editor/src/lib/editor-operations.ts

@@ -21,10 +21,6 @@ export interface IndentResult {
   childIndices: number[]
 }
 
-function generateId(): string {
-  return crypto.randomUUID()
-}
-
 /**
  * Split block at `index` into two: the original retains `before`,
  * a new sibling gets `after`.

+ 10 - 0
pnpm-lock.yaml

@@ -123,6 +123,9 @@ importers:
       katex:
         specifier: ^0.17.0
         version: 0.17.0
+      nanoid:
+        specifier: ^5.1.11
+        version: 5.1.11
       prosemirror-commands:
         specifier: ^1.7.1
         version: 1.7.1
@@ -2481,6 +2484,11 @@ packages:
     engines: {node: ^10 || ^12 || ^13.7 || ^14 || >=15.0.1}
     hasBin: true
 
+  [email protected]:
+    resolution: {integrity: sha512-v+KEsUv2ps74PaSKv0gHTxTCgMXOIfBEbaqa6w6ISIGC7ZsvHN4N9oJ8d4cmf0n5oTzQz2SLmThbQWhjd/8eKg==}
+    engines: {node: ^18 || >=20}
+    hasBin: true
+
   [email protected]:
     resolution: {integrity: sha512-8Ofs/AUQh8MaEcrlq5xOX0CQ9ypTF5dl78mjlMNfOK08fzpgTHQRQPBxcPlEtIw0yRpws+Zo/3r+5WRby7u3Gg==}
     engines: {node: '>= 0.6'}
@@ -6132,6 +6140,8 @@ snapshots:
 
   [email protected]: {}
 
+  [email protected]: {}
+
   [email protected]: {}
 
   [email protected]: {}