@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
@@ -3,8 +3,8 @@ import { createContext, createElement, useMemo, useContext, useRef, Component, u
3
3
  import { clsx } from 'clsx';
4
4
  import { twMerge } from 'tailwind-merge';
5
5
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
6
- import { extractExtension, embedTypeFromExtension } from '@nuucognition/mesh-core';
7
6
  import { parseNcm, serializeInlineContent, isTypedNestedList } from '@nuucognition/ncm';
7
+ import { extractExtension, embedTypeFromExtension } from '@nuucognition/mesh-core';
8
8
 
9
9
  // src/components/block-shell.tsx
10
10
  function cn(...inputs) {
@@ -121,6 +121,52 @@ function tryParseInlineRef(input) {
121
121
  }
122
122
  return { token: { type: "inline_ref", kind: "footnote", content: inner.trim() }, length };
123
123
  }
124
+ function runLength(input, index, char) {
125
+ let end = index;
126
+ while (end < input.length && input[end] === char) end++;
127
+ return end - index;
128
+ }
129
+ function tryParseEmphasis(input) {
130
+ const char = input[0];
131
+ if (char !== "*" && char !== "_") return null;
132
+ const open = Math.min(3, runLength(input, 0, char));
133
+ const need = open;
134
+ let i = open;
135
+ while (i < input.length) {
136
+ const ch = input[i];
137
+ if (ch === "`") {
138
+ const close = input.indexOf("`", i + 1);
139
+ if (close === -1) break;
140
+ i = close + 1;
141
+ continue;
142
+ }
143
+ if (ch !== char) {
144
+ i++;
145
+ continue;
146
+ }
147
+ const run = runLength(input, i, char);
148
+ const closes = need === 1 ? run % 2 === 1 : run >= need;
149
+ if (!closes) {
150
+ i += run;
151
+ continue;
152
+ }
153
+ const extra = run - need;
154
+ const inner = input.slice(open, i + extra);
155
+ if (inner.length === 0) return null;
156
+ const length = i + run;
157
+ const children = parseInline(inner);
158
+ let token;
159
+ if (need === 3) {
160
+ token = { type: "bold", children: [{ type: "italic", children }] };
161
+ } else if (need === 2) {
162
+ token = { type: "bold", children };
163
+ } else {
164
+ token = { type: "italic", children };
165
+ }
166
+ return { token, length };
167
+ }
168
+ return null;
169
+ }
124
170
  function parseInline(content) {
125
171
  if (!content) return [];
126
172
  const tokens = [];
@@ -192,17 +238,11 @@ function parseInline(content) {
192
238
  remaining = remaining.slice(linkMatch[0].length);
193
239
  continue;
194
240
  }
195
- const boldMatch = remaining.match(/^\*\*(.+?)\*\*|^__(.+?)__/);
196
- if (boldMatch) {
197
- const innerContent = boldMatch[1] ?? boldMatch[2];
198
- if (innerContent !== void 0) {
199
- tokens.push({
200
- type: "bold",
201
- children: parseInline(innerContent)
202
- });
203
- remaining = remaining.slice(boldMatch[0].length);
204
- continue;
205
- }
241
+ const emphasis = tryParseEmphasis(remaining);
242
+ if (emphasis) {
243
+ tokens.push(emphasis.token);
244
+ remaining = remaining.slice(emphasis.length);
245
+ continue;
206
246
  }
207
247
  const strikeMatch = remaining.match(/^~~(.+?)~~/);
208
248
  if (strikeMatch && strikeMatch[1] !== void 0) {
@@ -213,18 +253,6 @@ function parseInline(content) {
213
253
  remaining = remaining.slice(strikeMatch[0].length);
214
254
  continue;
215
255
  }
216
- const italicMatch = remaining.match(/^\*([^*]+)\*|^_([^_]+)_/);
217
- if (italicMatch) {
218
- const innerContent = italicMatch[1] ?? italicMatch[2];
219
- if (innerContent !== void 0) {
220
- tokens.push({
221
- type: "italic",
222
- children: parseInline(innerContent)
223
- });
224
- remaining = remaining.slice(italicMatch[0].length);
225
- continue;
226
- }
227
- }
228
256
  const nextSpecial = findNextSpecialChar(remaining);
229
257
  if (nextSpecial > 0) {
230
258
  tokens.push({ type: "text", content: remaining.slice(0, nextSpecial) });
@@ -375,48 +403,81 @@ function collectListStrings(items) {
375
403
  }
376
404
  return out;
377
405
  }
378
- function countInlineRefs(tokens, acc) {
406
+ function normalizeReferenceContent(content) {
407
+ return content.replace(/\s+/g, " ").trim().toLowerCase();
408
+ }
409
+ var BIBLIOGRAPHY_ENTRY_RE = /^\s*\[(\d+)\]\s+([\s\S]+?)\s*$/;
410
+ function collectBibliography(blocks) {
411
+ const entries = /* @__PURE__ */ new Map();
412
+ for (const block of blocks) {
413
+ if (block.type !== "paragraph") continue;
414
+ const content = block.content ?? "";
415
+ const match = BIBLIOGRAPHY_ENTRY_RE.exec(content);
416
+ if (!match || !match[1] || !match[2]) continue;
417
+ const key = normalizeReferenceContent(match[2]);
418
+ if (key && !entries.has(key)) entries.set(key, Number(match[1]));
419
+ }
420
+ return entries;
421
+ }
422
+ function walkInlineRefs(tokens, visit) {
379
423
  for (const token of tokens) {
380
424
  if (token.type === "inline_ref") {
381
- acc[token.kind] += 1;
425
+ visit(token);
382
426
  } else if ("children" in token) {
383
- countInlineRefs(token.children, acc);
427
+ walkInlineRefs(token.children, visit);
384
428
  }
385
429
  }
386
430
  }
387
- function countBlock(block) {
388
- const acc = { ref: 0, footnote: 0 };
389
- for (const str of collectInlineStrings(block)) {
390
- if (str) countInlineRefs(parseInline(str), acc);
391
- }
392
- return acc;
393
- }
394
- function computeReferenceBases(blocks) {
395
- const bases = /* @__PURE__ */ new Map();
396
- const acc = { ref: 0, footnote: 0 };
431
+ function computeReferenceNumbering(blocks) {
432
+ const bibliography = collectBibliography(blocks);
433
+ let maxBibliography = 0;
434
+ for (const n of bibliography.values()) maxBibliography = Math.max(maxBibliography, n);
435
+ const numbering = { ref: /* @__PURE__ */ new Map(), footnote: /* @__PURE__ */ new Map() };
436
+ let nextRef = maxBibliography;
437
+ let nextFootnote = 0;
397
438
  for (const block of blocks) {
398
- bases.set(block.id, { ...acc });
399
- const counts = countBlock(block);
400
- acc.ref += counts.ref;
401
- acc.footnote += counts.footnote;
439
+ for (const str of collectInlineStrings(block)) {
440
+ if (!str) continue;
441
+ walkInlineRefs(parseInline(str), (token) => {
442
+ const key = normalizeReferenceContent(token.content);
443
+ const map = numbering[token.kind];
444
+ if (map.has(key)) return;
445
+ if (token.kind === "ref") {
446
+ const fromBibliography = bibliography.get(key);
447
+ map.set(key, fromBibliography ?? ++nextRef);
448
+ } else {
449
+ map.set(key, ++nextFootnote);
450
+ }
451
+ });
452
+ }
402
453
  }
403
- return bases;
454
+ return numbering;
404
455
  }
405
- var ReferenceBasesContext = createContext(null);
456
+ var ReferenceNumberingContext = createContext(null);
406
457
  var BlockReferenceScopeContext = createContext(null);
407
458
  function NcmReferencesProvider({ blocks, children }) {
408
- const bases = useMemo(() => computeReferenceBases(blocks), [blocks]);
409
- return /* @__PURE__ */ jsx(ReferenceBasesContext.Provider, { value: bases, children });
459
+ const numbering = useMemo(() => computeReferenceNumbering(blocks), [blocks]);
460
+ return /* @__PURE__ */ jsx(ReferenceNumberingContext.Provider, { value: numbering, children });
410
461
  }
411
- function BlockReferenceScope({ blockId, children }) {
412
- const bases = useContext(ReferenceBasesContext);
413
- const base = (blockId ? bases?.get(blockId) : void 0) ?? { ref: 0, footnote: 0 };
462
+ function BlockReferenceScope({ children }) {
463
+ const numbering = useContext(ReferenceNumberingContext);
464
+ const local = useRef({ ref: /* @__PURE__ */ new Map(), footnote: /* @__PURE__ */ new Map() });
414
465
  const counter = useRef({ ref: 0, footnote: 0 });
415
- counter.current = { ref: 0, footnote: 0 };
416
466
  const value = {
467
+ numberFor(kind, content) {
468
+ const key = normalizeReferenceContent(content);
469
+ const fromDocument = numbering?.[kind].get(key);
470
+ if (fromDocument !== void 0) return fromDocument;
471
+ const map = local.current[kind];
472
+ const existing = map.get(key);
473
+ if (existing !== void 0) return existing;
474
+ counter.current[kind] += 1;
475
+ map.set(key, counter.current[kind]);
476
+ return counter.current[kind];
477
+ },
417
478
  next(kind) {
418
479
  counter.current[kind] += 1;
419
- return base[kind] + counter.current[kind];
480
+ return counter.current[kind];
420
481
  }
421
482
  };
422
483
  return /* @__PURE__ */ jsx(BlockReferenceScopeContext.Provider, { value, children });
@@ -424,7 +485,7 @@ function BlockReferenceScope({ blockId, children }) {
424
485
  function useBlockReferenceScope() {
425
486
  return useContext(BlockReferenceScopeContext);
426
487
  }
427
- var INERT_SCOPE = { next: () => 0 };
488
+ var INERT_SCOPE = { numberFor: () => 0, next: () => 0 };
428
489
  function InertReferenceScope({ children }) {
429
490
  return /* @__PURE__ */ jsx(BlockReferenceScopeContext.Provider, { value: INERT_SCOPE, children });
430
491
  }
@@ -715,7 +776,7 @@ function TokenRenderer({ token, disableBionic, disableBeeline }) {
715
776
  );
716
777
  }
717
778
  case "inline_ref": {
718
- const number = refScope?.next(token.kind) ?? 0;
779
+ const number = refScope?.numberFor(token.kind, token.content) ?? 0;
719
780
  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 });
720
781
  }
721
782
  default:
@@ -781,8 +842,32 @@ function renderReadingText(content, opts) {
781
842
  if (cursor < content.length) pieces.push(content.slice(cursor));
782
843
  return /* @__PURE__ */ jsx(Fragment, { children: pieces });
783
844
  }
845
+ function quoteBlocks(block) {
846
+ try {
847
+ const doc = parseNcm(block.content, {
848
+ preset: "ncm-full",
849
+ allowMissingFrontmatter: true,
850
+ // Attribution off, as the NUU Editor's bridge keeps it: the promoter's
851
+ // trigger is any last quote line starting with `-`, so a nested quote
852
+ // ending in a list item would lose that item to a `— Author` line.
853
+ blockOptions: { extensions: { attribution: false } }
854
+ });
855
+ const blocks = doc.blocks ?? [];
856
+ if (blocks.length === 0 || blocks.some((child) => child.type === "frontmatter")) {
857
+ return null;
858
+ }
859
+ return blocks.map((child, index) => ({ ...child, id: `${block.id}.${index + 1}`, stableId: void 0 }));
860
+ } catch {
861
+ return null;
862
+ }
863
+ }
864
+ function QuotedLines({ content }) {
865
+ const lines = content.split("\n");
866
+ 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)) });
867
+ }
784
868
  function BlockquoteBlock({ block, className }) {
785
- const lines = block.content.split("\n");
869
+ const multiline = block.content.includes("\n");
870
+ const blocks = useMemo(() => multiline ? quoteBlocks(block) : null, [block, multiline]);
786
871
  return /* @__PURE__ */ jsx(
787
872
  "blockquote",
788
873
  {
@@ -792,7 +877,9 @@ function BlockquoteBlock({ block, className }) {
792
877
  "data-block-id": block.id,
793
878
  "data-ncm-blockquote": "",
794
879
  className,
795
- 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))
880
+ children: !multiline ? /* @__PURE__ */ jsx(InlineContent, { content: block.content }) : blocks === null ? /* @__PURE__ */ jsx(QuotedLines, { content: block.content }) : blocks.map(
881
+ (child) => child.type === "paragraph" ? /* @__PURE__ */ jsx(QuotedLines, { content: child.content ?? "" }, blockKey(child)) : /* @__PURE__ */ jsx(NcmBlock, { block: child }, blockKey(child))
882
+ )
796
883
  }
797
884
  );
798
885
  }
@@ -807,7 +894,8 @@ function ChartBlock({ block, className }) {
807
894
  "data-ncm-chart": "",
808
895
  "data-ncm-chart-type": block.chartType ?? "unknown",
809
896
  className: cn(
810
- "overflow-x-auto rounded-lg border border-border bg-muted p-4 font-mono text-sm",
897
+ // `overflow-y-hidden` pairs with `overflow-x-auto` on purpose — see the note in `lib/defaults.ts`.
898
+ "overflow-x-auto overflow-y-hidden rounded-lg border border-border bg-muted p-4 font-mono text-sm",
811
899
  className
812
900
  ),
813
901
  children: /* @__PURE__ */ jsx("code", { children: block.content })
@@ -987,7 +1075,8 @@ function DataviewBlock({ block, className }) {
987
1075
  "data-ncm-dataview": "",
988
1076
  "data-ncm-query-type": block.queryType,
989
1077
  className: cn(
990
- "overflow-x-auto rounded-lg border border-border bg-muted p-4 font-mono text-sm",
1078
+ // `overflow-y-hidden` pairs with `overflow-x-auto` on purpose — see the note in `lib/defaults.ts`.
1079
+ "overflow-x-auto overflow-y-hidden rounded-lg border border-border bg-muted p-4 font-mono text-sm",
991
1080
  className
992
1081
  ),
993
1082
  children: /* @__PURE__ */ jsx("code", { children: block.query })
@@ -1326,7 +1415,8 @@ function HtmlBlock({ block, className }) {
1326
1415
  "data-block-id": block.id,
1327
1416
  "data-ncm-html": "",
1328
1417
  className: cn(
1329
- "overflow-x-auto rounded-lg border border-border bg-muted p-4 font-mono text-sm",
1418
+ // `overflow-y-hidden` pairs with `overflow-x-auto` on purpose — see the note in `lib/defaults.ts`.
1419
+ "overflow-x-auto overflow-y-hidden rounded-lg border border-border bg-muted p-4 font-mono text-sm",
1330
1420
  className
1331
1421
  ),
1332
1422
  children: /* @__PURE__ */ jsx("code", { children: block.content })
@@ -1364,7 +1454,7 @@ function LatexDisplayBlock({ block, className }) {
1364
1454
  "data-ncm-type": block.type,
1365
1455
  "data-block-id": block.id,
1366
1456
  "data-ncm-latex-display": "",
1367
- className: cn("overflow-x-auto", className),
1457
+ className: cn("overflow-x-auto overflow-y-hidden", className),
1368
1458
  dangerouslySetInnerHTML: { __html: html }
1369
1459
  }
1370
1460
  );
@@ -1379,7 +1469,7 @@ function LatexDisplayBlock({ block, className }) {
1379
1469
  "data-ncm-type": block.type,
1380
1470
  "data-block-id": block.id,
1381
1471
  "data-ncm-latex-display": "",
1382
- className: cn("overflow-x-auto rounded-lg border border-border bg-muted p-4 font-mono text-sm", className),
1472
+ className: cn("overflow-x-auto overflow-y-hidden rounded-lg border border-border bg-muted p-4 font-mono text-sm", className),
1383
1473
  children: /* @__PURE__ */ jsx("code", { children: block.content })
1384
1474
  }
1385
1475
  );
@@ -1499,7 +1589,7 @@ function MathBlock({ block, className }) {
1499
1589
  "data-ncm-type": block.type,
1500
1590
  "data-block-id": block.id,
1501
1591
  "data-ncm-math": "",
1502
- className: cn("overflow-x-auto", className),
1592
+ className: cn("overflow-x-auto overflow-y-hidden", className),
1503
1593
  dangerouslySetInnerHTML: { __html: html }
1504
1594
  }
1505
1595
  );
@@ -1515,7 +1605,8 @@ function MathBlock({ block, className }) {
1515
1605
  "data-block-id": block.id,
1516
1606
  "data-ncm-math": "",
1517
1607
  className: cn(
1518
- "overflow-x-auto rounded-lg border border-border bg-muted p-4 font-mono text-sm",
1608
+ // `overflow-y-hidden` pairs with `overflow-x-auto` on purpose — see the note in `lib/defaults.ts`.
1609
+ "overflow-x-auto overflow-y-hidden rounded-lg border border-border bg-muted p-4 font-mono text-sm",
1519
1610
  className
1520
1611
  ),
1521
1612
  children: /* @__PURE__ */ jsx("code", { children: block.content })
@@ -1533,7 +1624,8 @@ function MermaidBlock({ block, className }) {
1533
1624
  "data-ncm-mermaid": "",
1534
1625
  "data-ncm-diagram-type": block.diagramType,
1535
1626
  className: cn(
1536
- "overflow-x-auto rounded-lg border border-border bg-muted p-4 font-mono text-sm",
1627
+ // `overflow-y-hidden` pairs with `overflow-x-auto` on purpose — see the note in `lib/defaults.ts`.
1628
+ "overflow-x-auto overflow-y-hidden rounded-lg border border-border bg-muted p-4 font-mono text-sm",
1537
1629
  className
1538
1630
  ),
1539
1631
  children: /* @__PURE__ */ jsx("code", { children: block.content })
@@ -1594,7 +1686,7 @@ function TableBlock({ block, className }) {
1594
1686
  "data-ncm-type": block.type,
1595
1687
  "data-block-id": block.id,
1596
1688
  "data-ncm-table": "",
1597
- className: cn("w-full overflow-x-auto", className),
1689
+ className: cn("w-full overflow-x-auto overflow-y-hidden", className),
1598
1690
  children: /* @__PURE__ */ jsxs("table", { role: "table", "data-ncm-table-element": "", children: [
1599
1691
  /* @__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)) }) }),
1600
1692
  /* @__PURE__ */ jsx("tbody", { children: block.rows.map((row, rowIndex) => {
@@ -1680,7 +1772,7 @@ function FallbackBlock({ block, className }) {
1680
1772
  "data-block-id": block.id,
1681
1773
  "data-ncm-fallback": "",
1682
1774
  className: cn(
1683
- "overflow-x-auto rounded-lg border border-border bg-muted p-4 font-mono text-sm",
1775
+ "overflow-x-auto overflow-y-hidden rounded-lg border border-border bg-muted p-4 font-mono text-sm",
1684
1776
  className
1685
1777
  )
1686
1778
  },
@@ -1736,7 +1828,7 @@ function CustomCodeBlock({ block, className }) {
1736
1828
  "data-block-id": block.id,
1737
1829
  "data-ncm-custom-code": "",
1738
1830
  "data-ncm-language": block.language,
1739
- className: cn("overflow-x-auto", className),
1831
+ className: cn("overflow-x-auto overflow-y-hidden", className),
1740
1832
  children: [
1741
1833
  /* @__PURE__ */ jsx("div", { className: "mb-2 text-xs font-medium text-muted-foreground", children: block.language }),
1742
1834
  /* @__PURE__ */ jsx("pre", { children: /* @__PURE__ */ jsx("code", { children: block.content }) })