@nuucognition/ncm-ui 0.1.1-beta.0 → 0.1.1-beta.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.
Files changed (39) hide show
  1. package/dist/blocks/admonition/index.js +118 -46
  2. package/dist/blocks/aside/index.js +118 -46
  3. package/dist/blocks/blockquote/index.d.ts +7 -5
  4. package/dist/blocks/blockquote/index.js +1218 -36
  5. package/dist/blocks/chart/index.js +2 -1
  6. package/dist/blocks/container/index.js +72 -31
  7. package/dist/blocks/custom-code/index.js +1 -1
  8. package/dist/blocks/dataview/index.js +2 -1
  9. package/dist/blocks/definition/index.js +117 -45
  10. package/dist/blocks/embedding/index.js +72 -31
  11. package/dist/blocks/figure/index.js +72 -31
  12. package/dist/blocks/footnote/index.js +118 -46
  13. package/dist/blocks/header/index.js +72 -31
  14. package/dist/blocks/html/index.js +2 -1
  15. package/dist/blocks/index.js +118 -46
  16. package/dist/blocks/latex/index.js +2 -2
  17. package/dist/blocks/list/index.js +72 -31
  18. package/dist/blocks/math/index.js +3 -2
  19. package/dist/blocks/mermaid/index.js +2 -1
  20. package/dist/blocks/paragraph/index.js +72 -31
  21. package/dist/blocks/spoiler/index.js +118 -46
  22. package/dist/blocks/table/index.js +73 -32
  23. package/dist/blocks/task-list/index.js +72 -31
  24. package/dist/components/index.js +157 -65
  25. package/dist/components/ncm-block.js +118 -46
  26. package/dist/components/ncm-document.js +157 -65
  27. package/dist/context/index.d.ts +1 -1
  28. package/dist/context/index.js +119 -44
  29. package/dist/hooks/index.js +118 -46
  30. package/dist/hooks/use-block-renderer.js +118 -46
  31. package/dist/index.d.ts +1 -1
  32. package/dist/index.js +164 -58
  33. package/dist/inline/index.d.ts +2 -3
  34. package/dist/inline/index.js +72 -31
  35. package/dist/lib/defaults.js +118 -46
  36. package/dist/references-BM-lvGrq.d.ts +99 -0
  37. package/dist/styles/nuu.css +39 -16
  38. package/package.json +3 -4
  39. package/dist/references-BYW5pnj_.d.ts +0 -63
@@ -1,10 +1,10 @@
1
1
  "use client";
2
- import { createContext, useContext, createElement, useRef, Component, useState, useEffect, useMemo, useCallback, useId } from 'react';
2
+ import { createContext, useContext, createElement, useMemo, useRef, Component, useState, useEffect, useCallback, useId } from 'react';
3
3
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
4
4
  import { clsx } from 'clsx';
5
5
  import { twMerge } from 'tailwind-merge';
6
+ import { serializeInlineContent, isTypedNestedList, parseNcm } from '@nuucognition/ncm';
6
7
  import { extractExtension, embedTypeFromExtension } from '@nuucognition/mesh-core';
7
- import { serializeInlineContent, isTypedNestedList } from '@nuucognition/ncm';
8
8
 
9
9
  function blockKey(block) {
10
10
  return block.stableId ? `${block.id}:${block.stableId}` : block.id;
@@ -153,6 +153,52 @@ function tryParseInlineRef(input) {
153
153
  }
154
154
  return { token: { type: "inline_ref", kind: "footnote", content: inner.trim() }, length };
155
155
  }
156
+ function runLength(input, index, char) {
157
+ let end = index;
158
+ while (end < input.length && input[end] === char) end++;
159
+ return end - index;
160
+ }
161
+ function tryParseEmphasis(input) {
162
+ const char = input[0];
163
+ if (char !== "*" && char !== "_") return null;
164
+ const open = Math.min(3, runLength(input, 0, char));
165
+ const need = open;
166
+ let i = open;
167
+ while (i < input.length) {
168
+ const ch = input[i];
169
+ if (ch === "`") {
170
+ const close = input.indexOf("`", i + 1);
171
+ if (close === -1) break;
172
+ i = close + 1;
173
+ continue;
174
+ }
175
+ if (ch !== char) {
176
+ i++;
177
+ continue;
178
+ }
179
+ const run = runLength(input, i, char);
180
+ const closes = need === 1 ? run % 2 === 1 : run >= need;
181
+ if (!closes) {
182
+ i += run;
183
+ continue;
184
+ }
185
+ const extra = run - need;
186
+ const inner = input.slice(open, i + extra);
187
+ if (inner.length === 0) return null;
188
+ const length = i + run;
189
+ const children = parseInline(inner);
190
+ let token;
191
+ if (need === 3) {
192
+ token = { type: "bold", children: [{ type: "italic", children }] };
193
+ } else if (need === 2) {
194
+ token = { type: "bold", children };
195
+ } else {
196
+ token = { type: "italic", children };
197
+ }
198
+ return { token, length };
199
+ }
200
+ return null;
201
+ }
156
202
  function parseInline(content) {
157
203
  if (!content) return [];
158
204
  const tokens = [];
@@ -224,17 +270,11 @@ function parseInline(content) {
224
270
  remaining = remaining.slice(linkMatch[0].length);
225
271
  continue;
226
272
  }
227
- const boldMatch = remaining.match(/^\*\*(.+?)\*\*|^__(.+?)__/);
228
- if (boldMatch) {
229
- const innerContent = boldMatch[1] ?? boldMatch[2];
230
- if (innerContent !== void 0) {
231
- tokens.push({
232
- type: "bold",
233
- children: parseInline(innerContent)
234
- });
235
- remaining = remaining.slice(boldMatch[0].length);
236
- continue;
237
- }
273
+ const emphasis = tryParseEmphasis(remaining);
274
+ if (emphasis) {
275
+ tokens.push(emphasis.token);
276
+ remaining = remaining.slice(emphasis.length);
277
+ continue;
238
278
  }
239
279
  const strikeMatch = remaining.match(/^~~(.+?)~~/);
240
280
  if (strikeMatch && strikeMatch[1] !== void 0) {
@@ -245,18 +285,6 @@ function parseInline(content) {
245
285
  remaining = remaining.slice(strikeMatch[0].length);
246
286
  continue;
247
287
  }
248
- const italicMatch = remaining.match(/^\*([^*]+)\*|^_([^_]+)_/);
249
- if (italicMatch) {
250
- const innerContent = italicMatch[1] ?? italicMatch[2];
251
- if (innerContent !== void 0) {
252
- tokens.push({
253
- type: "italic",
254
- children: parseInline(innerContent)
255
- });
256
- remaining = remaining.slice(italicMatch[0].length);
257
- continue;
258
- }
259
- }
260
288
  const nextSpecial = findNextSpecialChar(remaining);
261
289
  if (nextSpecial > 0) {
262
290
  tokens.push({ type: "text", content: remaining.slice(0, nextSpecial) });
@@ -367,17 +395,30 @@ function ReferenceMarker({ kind, number, children }) {
367
395
  }
368
396
  );
369
397
  }
370
- var ReferenceBasesContext = createContext(null);
398
+ function normalizeReferenceContent(content) {
399
+ return content.replace(/\s+/g, " ").trim().toLowerCase();
400
+ }
401
+ var ReferenceNumberingContext = createContext(null);
371
402
  var BlockReferenceScopeContext = createContext(null);
372
- function BlockReferenceScope({ blockId, children }) {
373
- const bases = useContext(ReferenceBasesContext);
374
- const base = (blockId ? bases?.get(blockId) : void 0) ?? { ref: 0, footnote: 0 };
403
+ function BlockReferenceScope({ children }) {
404
+ const numbering = useContext(ReferenceNumberingContext);
405
+ const local = useRef({ ref: /* @__PURE__ */ new Map(), footnote: /* @__PURE__ */ new Map() });
375
406
  const counter = useRef({ ref: 0, footnote: 0 });
376
- counter.current = { ref: 0, footnote: 0 };
377
407
  const value = {
408
+ numberFor(kind, content) {
409
+ const key = normalizeReferenceContent(content);
410
+ const fromDocument = numbering?.[kind].get(key);
411
+ if (fromDocument !== void 0) return fromDocument;
412
+ const map = local.current[kind];
413
+ const existing = map.get(key);
414
+ if (existing !== void 0) return existing;
415
+ counter.current[kind] += 1;
416
+ map.set(key, counter.current[kind]);
417
+ return counter.current[kind];
418
+ },
378
419
  next(kind) {
379
420
  counter.current[kind] += 1;
380
- return base[kind] + counter.current[kind];
421
+ return counter.current[kind];
381
422
  }
382
423
  };
383
424
  return /* @__PURE__ */ jsx(BlockReferenceScopeContext.Provider, { value, children });
@@ -385,7 +426,7 @@ function BlockReferenceScope({ blockId, children }) {
385
426
  function useBlockReferenceScope() {
386
427
  return useContext(BlockReferenceScopeContext);
387
428
  }
388
- var INERT_SCOPE = { next: () => 0 };
429
+ var INERT_SCOPE = { numberFor: () => 0, next: () => 0 };
389
430
  function InertReferenceScope({ children }) {
390
431
  return /* @__PURE__ */ jsx(BlockReferenceScopeContext.Provider, { value: INERT_SCOPE, children });
391
432
  }
@@ -676,7 +717,7 @@ function TokenRenderer({ token, disableBionic, disableBeeline }) {
676
717
  );
677
718
  }
678
719
  case "inline_ref": {
679
- const number = refScope?.next(token.kind) ?? 0;
720
+ const number = refScope?.numberFor(token.kind, token.content) ?? 0;
680
721
  return /* @__PURE__ */ jsx(ReferenceMarker, { kind: token.kind, number, children: token.content ? /* @__PURE__ */ jsx(InertReferenceScope, { children: /* @__PURE__ */ jsx(InlineContent, { content: token.content, disableBionic: true, disableBeeline: true }) }) : null });
681
722
  }
682
723
  default:
@@ -742,8 +783,32 @@ function renderReadingText(content, opts) {
742
783
  if (cursor < content.length) pieces.push(content.slice(cursor));
743
784
  return /* @__PURE__ */ jsx(Fragment, { children: pieces });
744
785
  }
786
+ function quoteBlocks(block) {
787
+ try {
788
+ const doc = parseNcm(block.content, {
789
+ preset: "ncm-full",
790
+ allowMissingFrontmatter: true,
791
+ // Attribution off, as the NUU Editor's bridge keeps it: the promoter's
792
+ // trigger is any last quote line starting with `-`, so a nested quote
793
+ // ending in a list item would lose that item to a `— Author` line.
794
+ blockOptions: { extensions: { attribution: false } }
795
+ });
796
+ const blocks = doc.blocks ?? [];
797
+ if (blocks.length === 0 || blocks.some((child) => child.type === "frontmatter")) {
798
+ return null;
799
+ }
800
+ return blocks.map((child, index) => ({ ...child, id: `${block.id}.${index + 1}`, stableId: void 0 }));
801
+ } catch {
802
+ return null;
803
+ }
804
+ }
805
+ function QuotedLines({ content }) {
806
+ const lines = content.split("\n");
807
+ return /* @__PURE__ */ jsx(Fragment, { children: lines.map((line, index) => /* @__PURE__ */ jsx("p", { children: line.trim().length > 0 ? /* @__PURE__ */ jsx(InlineContent, { content: line }) : /* @__PURE__ */ jsx("span", { "aria-hidden": "true", children: "\xA0" }) }, index)) });
808
+ }
745
809
  function BlockquoteBlock({ block, className }) {
746
- const lines = block.content.split("\n");
810
+ const multiline = block.content.includes("\n");
811
+ const blocks = useMemo(() => multiline ? quoteBlocks(block) : null, [block, multiline]);
747
812
  return /* @__PURE__ */ jsx(
748
813
  "blockquote",
749
814
  {
@@ -753,7 +818,9 @@ function BlockquoteBlock({ block, className }) {
753
818
  "data-block-id": block.id,
754
819
  "data-ncm-blockquote": "",
755
820
  className,
756
- children: lines.length <= 1 ? /* @__PURE__ */ jsx(InlineContent, { content: block.content }) : lines.map((line, index) => /* @__PURE__ */ jsx("p", { children: line.trim().length > 0 ? /* @__PURE__ */ jsx(InlineContent, { content: line }) : /* @__PURE__ */ jsx("span", { "aria-hidden": "true", children: "\xA0" }) }, index))
821
+ children: !multiline ? /* @__PURE__ */ jsx(InlineContent, { content: block.content }) : blocks === null ? /* @__PURE__ */ jsx(QuotedLines, { content: block.content }) : blocks.map(
822
+ (child) => child.type === "paragraph" ? /* @__PURE__ */ jsx(QuotedLines, { content: child.content ?? "" }, blockKey(child)) : /* @__PURE__ */ jsx(NcmBlock, { block: child }, blockKey(child))
823
+ )
757
824
  }
758
825
  );
759
826
  }
@@ -768,7 +835,8 @@ function ChartBlock({ block, className }) {
768
835
  "data-ncm-chart": "",
769
836
  "data-ncm-chart-type": block.chartType ?? "unknown",
770
837
  className: cn(
771
- "overflow-x-auto rounded-lg border border-border bg-muted p-4 font-mono text-sm",
838
+ // `overflow-y-hidden` pairs with `overflow-x-auto` on purpose — see the note in `lib/defaults.ts`.
839
+ "overflow-x-auto overflow-y-hidden rounded-lg border border-border bg-muted p-4 font-mono text-sm",
772
840
  className
773
841
  ),
774
842
  children: /* @__PURE__ */ jsx("code", { children: block.content })
@@ -948,7 +1016,8 @@ function DataviewBlock({ block, className }) {
948
1016
  "data-ncm-dataview": "",
949
1017
  "data-ncm-query-type": block.queryType,
950
1018
  className: cn(
951
- "overflow-x-auto rounded-lg border border-border bg-muted p-4 font-mono text-sm",
1019
+ // `overflow-y-hidden` pairs with `overflow-x-auto` on purpose — see the note in `lib/defaults.ts`.
1020
+ "overflow-x-auto overflow-y-hidden rounded-lg border border-border bg-muted p-4 font-mono text-sm",
952
1021
  className
953
1022
  ),
954
1023
  children: /* @__PURE__ */ jsx("code", { children: block.query })
@@ -1287,7 +1356,8 @@ function HtmlBlock({ block, className }) {
1287
1356
  "data-block-id": block.id,
1288
1357
  "data-ncm-html": "",
1289
1358
  className: cn(
1290
- "overflow-x-auto rounded-lg border border-border bg-muted p-4 font-mono text-sm",
1359
+ // `overflow-y-hidden` pairs with `overflow-x-auto` on purpose — see the note in `lib/defaults.ts`.
1360
+ "overflow-x-auto overflow-y-hidden rounded-lg border border-border bg-muted p-4 font-mono text-sm",
1291
1361
  className
1292
1362
  ),
1293
1363
  children: /* @__PURE__ */ jsx("code", { children: block.content })
@@ -1325,7 +1395,7 @@ function LatexDisplayBlock({ block, className }) {
1325
1395
  "data-ncm-type": block.type,
1326
1396
  "data-block-id": block.id,
1327
1397
  "data-ncm-latex-display": "",
1328
- className: cn("overflow-x-auto", className),
1398
+ className: cn("overflow-x-auto overflow-y-hidden", className),
1329
1399
  dangerouslySetInnerHTML: { __html: html }
1330
1400
  }
1331
1401
  );
@@ -1340,7 +1410,7 @@ function LatexDisplayBlock({ block, className }) {
1340
1410
  "data-ncm-type": block.type,
1341
1411
  "data-block-id": block.id,
1342
1412
  "data-ncm-latex-display": "",
1343
- className: cn("overflow-x-auto rounded-lg border border-border bg-muted p-4 font-mono text-sm", className),
1413
+ className: cn("overflow-x-auto overflow-y-hidden rounded-lg border border-border bg-muted p-4 font-mono text-sm", className),
1344
1414
  children: /* @__PURE__ */ jsx("code", { children: block.content })
1345
1415
  }
1346
1416
  );
@@ -1460,7 +1530,7 @@ function MathBlock({ block, className }) {
1460
1530
  "data-ncm-type": block.type,
1461
1531
  "data-block-id": block.id,
1462
1532
  "data-ncm-math": "",
1463
- className: cn("overflow-x-auto", className),
1533
+ className: cn("overflow-x-auto overflow-y-hidden", className),
1464
1534
  dangerouslySetInnerHTML: { __html: html }
1465
1535
  }
1466
1536
  );
@@ -1476,7 +1546,8 @@ function MathBlock({ block, className }) {
1476
1546
  "data-block-id": block.id,
1477
1547
  "data-ncm-math": "",
1478
1548
  className: cn(
1479
- "overflow-x-auto rounded-lg border border-border bg-muted p-4 font-mono text-sm",
1549
+ // `overflow-y-hidden` pairs with `overflow-x-auto` on purpose — see the note in `lib/defaults.ts`.
1550
+ "overflow-x-auto overflow-y-hidden rounded-lg border border-border bg-muted p-4 font-mono text-sm",
1480
1551
  className
1481
1552
  ),
1482
1553
  children: /* @__PURE__ */ jsx("code", { children: block.content })
@@ -1494,7 +1565,8 @@ function MermaidBlock({ block, className }) {
1494
1565
  "data-ncm-mermaid": "",
1495
1566
  "data-ncm-diagram-type": block.diagramType,
1496
1567
  className: cn(
1497
- "overflow-x-auto rounded-lg border border-border bg-muted p-4 font-mono text-sm",
1568
+ // `overflow-y-hidden` pairs with `overflow-x-auto` on purpose — see the note in `lib/defaults.ts`.
1569
+ "overflow-x-auto overflow-y-hidden rounded-lg border border-border bg-muted p-4 font-mono text-sm",
1498
1570
  className
1499
1571
  ),
1500
1572
  children: /* @__PURE__ */ jsx("code", { children: block.content })
@@ -1555,7 +1627,7 @@ function TableBlock({ block, className }) {
1555
1627
  "data-ncm-type": block.type,
1556
1628
  "data-block-id": block.id,
1557
1629
  "data-ncm-table": "",
1558
- className: cn("w-full overflow-x-auto", className),
1630
+ className: cn("w-full overflow-x-auto overflow-y-hidden", className),
1559
1631
  children: /* @__PURE__ */ jsxs("table", { role: "table", "data-ncm-table-element": "", children: [
1560
1632
  /* @__PURE__ */ jsx("thead", { children: /* @__PURE__ */ jsx("tr", { "data-ncm-table-row": "", children: block.headers.map((header, index) => /* @__PURE__ */ jsx("th", { "data-ncm-table-header": "", children: /* @__PURE__ */ jsx(InlineContent, { content: header }) }, index)) }) }),
1561
1633
  /* @__PURE__ */ jsx("tbody", { children: block.rows.map((row, rowIndex) => {
@@ -1641,7 +1713,7 @@ function FallbackBlock({ block, className }) {
1641
1713
  "data-block-id": block.id,
1642
1714
  "data-ncm-fallback": "",
1643
1715
  className: cn(
1644
- "overflow-x-auto rounded-lg border border-border bg-muted p-4 font-mono text-sm",
1716
+ "overflow-x-auto overflow-y-hidden rounded-lg border border-border bg-muted p-4 font-mono text-sm",
1645
1717
  className
1646
1718
  )
1647
1719
  },
@@ -1697,7 +1769,7 @@ function CustomCodeBlock({ block, className }) {
1697
1769
  "data-block-id": block.id,
1698
1770
  "data-ncm-custom-code": "",
1699
1771
  "data-ncm-language": block.language,
1700
- className: cn("overflow-x-auto", className),
1772
+ className: cn("overflow-x-auto overflow-y-hidden", className),
1701
1773
  children: [
1702
1774
  /* @__PURE__ */ jsx("div", { className: "mb-2 text-xs font-medium text-muted-foreground", children: block.language }),
1703
1775
  /* @__PURE__ */ jsx("pre", { children: /* @__PURE__ */ jsx("code", { children: block.content }) })