@memorysmithapp/markdown-renderer 0.13.1 → 0.13.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/README.md CHANGED
@@ -73,7 +73,7 @@ const acme = defineTheme({
73
73
  - **Your CSS** is scoped by `.markdown-theme-<name>`, and reaches the note and nothing else.
74
74
  - **Slots** — `callout`, `calloutIcon`, `table`, `codeBlock`, `inlineCode`, `heading`, `blockquote`, `embed`, `footnotes`, `taskBox`, `wikilink`, `diagram`, `properties`, `modeSwitch` — are called with the notation already read. A slot decides how a block looks, never what it means: the links it draws go where the host resolved them, a task box reports through the `toggle` it is given, and no raw HTML ever reaches it.
75
75
 
76
- A ` ```contents ` block is the note's table of contents (§7.17): its headings at the top level, nested, each a link to the heading, which carries its slug as its id, prefixed `user-content-`, and as `data-heading`, so a link written as `#deliberações` lands on it too. The block's lines set the title and the depth, `screen` and `print` groups for each medium, or `none`, and `place: start` or `end` to draw it there instead of where it is written. `contents` gives the same settings for a note with no block, and the defaults of one that has a block; with `printing` only `print` is drawn, and otherwise a drawing for one medium is marked `data-media`, which the stylesheet shows only there. Printed, each entry is a link a paginator numbers with `target-counter()`. Inside an embed a block draws nothing. A block also lists `figures:`, `tables:` and `attachments:`: an image alone in its paragraph, remote or a kept file `fileOf` says is an image; each table, a `table` block by its `title:` key and a Markdown table by a `Table: …` paragraph after it, which is drawn as its caption whatever lists it; and each kept file the note embeds. An entry is the title written, else the file's description, else its name, and where a medium lists a kind, each of its items carries a line under it with its number, in `strings.figureNumber` and `strings.tableNumber`.
76
+ A ` ```contents ` block is the note's table of contents (§7.17): its headings at the top level, nested, each a link to the heading, which carries its slug as its id, prefixed `user-content-`, and as `data-heading`, so a link written as `#deliberações` lands on it too. The block's lines set the title and the depth, `screen` and `print` groups for each medium, or `none`, and `place: start` or `end` to draw it there instead of where it is written. `contents` gives the same settings for a note with no block, and the defaults of one that has a block; with `printing` only `print` is drawn, and otherwise a drawing for one medium is marked `data-media`, which the stylesheet shows only there. Printed, each entry is a link a paginator numbers with `target-counter()`. Inside an embed a block draws nothing. A block also lists `figures:`, `tables:` and `attachments:`: an image alone in its paragraph, remote or a kept file `fileOf` says is an image; each table, a `table` block by its `title:` key and a Markdown table by a `Table: …` paragraph after it, which is drawn as its caption whatever lists it; and each kept file the note embeds. An entry is the title written, else the file's description, else its name, and where a medium lists a kind, each of its items carries a line under it with its number, in `strings.figureNumber` and `strings.tableNumber`. A list with no entry is not drawn, its title included, and a contents with no entry in any list draws nothing.
77
77
 
78
78
  The frontmatter is the note's too. The default theme draws it as MemorySmith does: `name` as the title, and the other entries in a block that folds, each key with the icon of its value's shape, lists as chips and dates in `strings.locale`, with a `[[wikilink]]`, a Markdown link or a web address in a value drawn as a link. A theme decides otherwise through the `properties` slot, which may draw nothing, as a host that shows properties elsewhere does:
79
79
 
@@ -112,7 +112,12 @@ function Tables({ tables, appendices }) {
112
112
  function Attachments({ attachments }) {
113
113
  return (_jsx("ol", { className: "contents-entries", children: attachments.map((attachment) => (_jsx("li", { className: "contents-item", children: _jsx(InPageAnchor, { href: `#${attachment.id}`, className: "contents-entry", children: _jsx(FileLabel, { name: attachment.name, title: attachment.title }) }) }, attachment.id))) }));
114
114
  }
115
- /** One drawn contents: each of its lists under its title. */
115
+ /**
116
+ * One drawn contents: each of its lists that has an entry, under its title (#181). A list with no
117
+ * entry is not drawn, title included, and a contents none of whose lists has one draws nothing, as
118
+ * `none` does; while a figure's file is not known yet it is an empty `nav`, hidden and busy, so a host
119
+ * that prints waits for the figures that may still come.
120
+ */
116
121
  function ContentsNav({ plan, medium }) {
117
122
  const { strings } = useHost();
118
123
  const scope = useContext(ContentsContext);
@@ -124,11 +129,20 @@ function ContentsNav({ plan, medium }) {
124
129
  tables: strings.contentsTables,
125
130
  attachments: strings.contentsAttachments,
126
131
  };
132
+ const entries = {
133
+ headings: scope.headings.length,
134
+ figures: scope.items.figures.length,
135
+ tables: scope.items.tables.length,
136
+ attachments: scope.items.attachments.length,
137
+ };
138
+ const lists = plan.lists.filter((list) => entries[list.kind] > 0);
127
139
  // A figure is known once its file is: until then the contents is busy, and a host that prints waits.
128
140
  const busy = scope.items.pending && plan.lists.some((list) => list.kind === 'figures');
129
- return (_jsx("nav", { className: "contents", "data-media": medium ?? undefined, "aria-busy": busy || undefined, children: plan.lists.map((list) => {
141
+ if (lists.length === 0 && !busy)
142
+ return null;
143
+ return (_jsx("nav", { className: "contents", "data-media": medium ?? undefined, "aria-busy": busy || undefined, hidden: lists.length === 0 || undefined, children: lists.map((list) => {
130
144
  const title = list.title ?? titles[list.kind];
131
- return (_jsxs("section", { className: "contents-list", "data-list": list.kind, "aria-label": title, children: [_jsx("p", { className: "contents-title", children: title }), list.kind === 'headings' && scope.headings.length > 0 && _jsx(Headings, { branches: branches(scope.headings, list.depth) }), list.kind === 'figures' && scope.items.figures.length > 0 && _jsx(Figures, { figures: scope.items.figures }), list.kind === 'tables' && scope.items.tables.length > 0 && _jsx(Tables, { tables: scope.items.tables, appendices: scope.appendices }), list.kind === 'attachments' && scope.items.attachments.length > 0 && _jsx(Attachments, { attachments: scope.items.attachments })] }, list.kind));
145
+ return (_jsxs("section", { className: "contents-list", "data-list": list.kind, "aria-label": title, children: [_jsx("p", { className: "contents-title", children: title }), list.kind === 'headings' && _jsx(Headings, { branches: branches(scope.headings, list.depth) }), list.kind === 'figures' && _jsx(Figures, { figures: scope.items.figures }), list.kind === 'tables' && _jsx(Tables, { tables: scope.items.tables, appendices: scope.appendices }), list.kind === 'attachments' && _jsx(Attachments, { attachments: scope.items.attachments })] }, list.kind));
132
146
  }) }));
133
147
  }
134
148
  /**
@@ -98,6 +98,8 @@ export type Construct = {
98
98
  readonly kind: 'table';
99
99
  readonly from: number;
100
100
  readonly to: number;
101
+ /** The `Table: …` paragraph that captions it, after a blank line (§7.17, #182), or `null`. */
102
+ readonly caption: Span | null;
101
103
  } | {
102
104
  readonly kind: 'listMarker';
103
105
  readonly from: number;
@@ -21,6 +21,7 @@ import { findStyleBlocks } from '../style-block.js';
21
21
  import { CALLOUT_MARKER } from '../remark-callouts.js';
22
22
  import { breaksDollarRule } from '../remark-ring.js';
23
23
  import { hasParameterReference, parameterReferences } from '../parameters.js';
24
+ import { CAPTION } from '../outline.js';
24
25
  /** The style blocks `findStyleBlocks` found in a text, by offsets into it. */
25
26
  export function styleSpans(text, found = findStyleBlocks(text)) {
26
27
  const starts = [0];
@@ -110,7 +111,7 @@ export function analyze(text) {
110
111
  found.push({ kind: 'wikilink', from, to, target: name, label, embed, parameter: hasParameterReference(name) });
111
112
  }
112
113
  const inWikilink = (span) => wikilinks.some((link) => span.from < link.to && span.to > link.from);
113
- const visit = (node, inCallout) => {
114
+ const visit = (node, inCallout, parent) => {
114
115
  const span = offsets(node);
115
116
  const children = node.children ?? [];
116
117
  if (span && !inComment(span.from)) {
@@ -225,7 +226,7 @@ export function analyze(text) {
225
226
  marker: { from: markerFrom, to: markerFrom + markerText.length },
226
227
  });
227
228
  for (const child of children)
228
- visit(child, true);
229
+ visit(child, true, node);
229
230
  return;
230
231
  }
231
232
  if (!inCallout) {
@@ -242,9 +243,13 @@ export function analyze(text) {
242
243
  }
243
244
  break;
244
245
  }
245
- case 'table':
246
- found.push({ kind: 'table', ...span });
246
+ case 'table': {
247
+ const next = parent?.children?.[parent.children.indexOf(node) + 1];
248
+ const first = next?.type === 'paragraph' ? next.children?.[0] : undefined;
249
+ const caption = next && first?.type === 'text' && CAPTION.test(first.value ?? '') ? offsets(next) : null;
250
+ found.push({ kind: 'table', ...span, caption });
247
251
  return;
252
+ }
248
253
  case 'listItem': {
249
254
  const marker = LIST_MARKER.exec(text.slice(span.from, span.to));
250
255
  if (marker) {
@@ -308,9 +313,9 @@ export function analyze(text) {
308
313
  }
309
314
  }
310
315
  for (const child of children)
311
- visit(child, inCallout);
316
+ visit(child, inCallout, node);
312
317
  };
313
- visit(tree, false);
318
+ visit(tree, false, null);
314
319
  found.sort((a, b) => a.from - b.from || b.to - a.to);
315
320
  return { constructs: found, bodyFrom };
316
321
  }
@@ -10,6 +10,7 @@
10
10
  import type { EditorView } from '@codemirror/view';
11
11
  import { type ReactNode } from 'react';
12
12
  import { type NoteStyle } from '../note-style.js';
13
+ import type { Span } from './analyze.js';
13
14
  import { type TextChange } from './table.js';
14
15
  /**
15
16
  * The opening of a style block while the author is elsewhere (`SPEC.md` §7.16, #166): what it sets,
@@ -76,19 +77,19 @@ export declare function ImageBlock({ view, source, children }: {
76
77
  * content only. The controls add and remove rows and columns and align the column of the cell last
77
78
  * focused.
78
79
  */
79
- export declare function TableBlock({ view, from, to }: {
80
+ export declare function TableBlock({ view, from, to, caption }: {
80
81
  view: EditorView;
81
82
  from: number;
82
83
  to: number;
84
+ caption?: Span | null;
83
85
  }): import("react").JSX.Element;
84
- /** The caption of the table that ends at `end`: where it is written, from the line break after the table, and its text. */
85
- export declare function tableCaptionAt(source: string, end: number): {
86
- from: number;
87
- to: number;
88
- text: string;
89
- } | null;
90
- /** The changes that set a table's caption to `text`: written, rewritten, or taken out when empty. */
91
- export declare function setTableCaption(source: string, end: number, text: string): TextChange[];
86
+ /** A caption as its field shows it: its paragraph without its keyword, its lines joined (#175, #182). */
87
+ export declare function captionText(source: string, caption: Span): string;
88
+ /**
89
+ * The changes that set the caption of the table that ends at `end`, whose `Table:` paragraph is
90
+ * `caption` or none: written after a blank line, rewritten, or taken out with that line when empty.
91
+ */
92
+ export declare function setTableCaption(source: string, end: number, caption: Span | null, text: string): TextChange[];
92
93
  /** Shows a block as its text: the cursor goes into it, and the editor draws its source. */
93
94
  export declare function reveal(view: EditorView, at: number): void;
94
95
  export declare function PropertiesBlock({ view, to }: {
@@ -19,7 +19,7 @@ import { splitEmbeds } from '../embeds.js';
19
19
  import { embedsFile } from '../embed-target.js';
20
20
  import { STYLE_KEYS } from '../note-style.js';
21
21
  import { wikilinkAddress } from '../wikilink-address.js';
22
- import { planItems, planOutline } from '../outline.js';
22
+ import { CAPTION, planItems, planOutline } from '../outline.js';
23
23
  import { useKeptFiles } from '../kept-files.js';
24
24
  import { readContents, resolveBlock } from '../contents.js';
25
25
  import { ContentsAt, ContentsContext, listedIn } from '../ContentsBlock.js';
@@ -640,7 +640,7 @@ const chosenCells = new WeakMap();
640
640
  * content only. The controls add and remove rows and columns and align the column of the cell last
641
641
  * focused.
642
642
  */
643
- export function TableBlock({ view, from, to }) {
643
+ export function TableBlock({ view, from, to, caption = null }) {
644
644
  const { strings } = useHost();
645
645
  const words = strings.editor;
646
646
  const source = view.state.doc.sliceString(0, view.state.doc.length, '\n');
@@ -777,35 +777,35 @@ export function TableBlock({ view, from, to }) {
777
777
  return (_jsxs("div", { className: "edit-block edit-table", children: [_jsxs("div", { className: "edit-block-controls", "data-ms-control": "", children: [_jsx(Control, { label: words.addRow, onClick: () => act((current) => addRow(current, focus.row)), children: ICONS.rowAdd }), _jsx(Control, { label: words.removeRow, onClick: () => act((current) => removeRow(current, focus.row)), children: ICONS.rowRemove }), _jsx(Control, { label: words.addColumn, onClick: () => act((current) => addColumn(current, focus.column)), children: ICONS.columnAdd }), _jsx(Control, { label: words.removeColumn, onClick: () => act((current) => removeColumn(current, focus.column)), children: ICONS.columnRemove }), _jsx(Control, { label: words.alignLeft, pressed: table.align[focus.column] === 'left', onClick: () => align(table.align[focus.column] === 'left' ? null : 'left'), children: ICONS.alignLeft }), _jsx(Control, { label: words.alignCenter, pressed: table.align[focus.column] === 'center', onClick: () => align(table.align[focus.column] === 'center' ? null : 'center'), children: ICONS.alignCenter }), _jsx(Control, { label: words.alignRight, pressed: table.align[focus.column] === 'right', onClick: () => align(table.align[focus.column] === 'right' ? null : 'right'), children: ICONS.alignRight }), _jsx(Control, { label: words.alignJustify, pressed: table.align[focus.column] === 'justify', onClick: () => align(table.align[focus.column] === 'justify' ? null : 'justify'), children: ICONS.alignJustify }), _jsx(Control, { label: words.source, onClick: () => {
778
778
  act(() => []);
779
779
  reveal(view, table.from);
780
- }, children: ICONS.source })] }), _jsx("div", { className: "markdown", children: _jsxs("table", { ref: root, children: [_jsx("thead", { children: _jsx("tr", { children: Array.from({ length: width }, (_, column) => cell(0, column)) }) }), bodyRows.length > 0 && (_jsx("tbody", { children: bodyRows.map((_, index) => (_jsx("tr", { children: Array.from({ length: width }, (__, column) => cell(index + 2, column)) }, index))) }))] }) }), _jsx(CaptionField, { view: view, end: table.to })] }));
780
+ }, children: ICONS.source })] }), _jsx("div", { className: "markdown", children: _jsxs("table", { ref: root, children: [_jsx("thead", { children: _jsx("tr", { children: Array.from({ length: width }, (_, column) => cell(0, column)) }) }), bodyRows.length > 0 && (_jsx("tbody", { children: bodyRows.map((_, index) => (_jsx("tr", { children: Array.from({ length: width }, (__, column) => cell(index + 2, column)) }, index))) }))] }) }), _jsx(CaptionField, { view: view, end: table.to, caption: caption })] }));
781
781
  }
782
- /** A Markdown table's caption (#175): the `Table:` paragraph after a blank line below its last row. */
783
- const CAPTION_AFTER = /^\n[ \t]*\n[ \t]*table:[ \t]*([^\n]*)/i;
784
- /** The caption of the table that ends at `end`: where it is written, from the line break after the table, and its text. */
785
- export function tableCaptionAt(source, end) {
786
- const at = end > 0 && source[end - 1] === '\n' ? end - 1 : end;
787
- const match = CAPTION_AFTER.exec(source.slice(at));
788
- return match ? { from: at, to: at + match[0].length, text: match[1].trim() } : null;
782
+ /** A caption as its field shows it: its paragraph without its keyword, its lines joined (#175, #182). */
783
+ export function captionText(source, caption) {
784
+ return source.slice(caption.from, caption.to).replace(CAPTION, '').replace(/[ \t]*\n[ \t]*/g, ' ').trim();
789
785
  }
790
- /** The changes that set a table's caption to `text`: written, rewritten, or taken out when empty. */
791
- export function setTableCaption(source, end, text) {
786
+ /**
787
+ * The changes that set the caption of the table that ends at `end`, whose `Table:` paragraph is
788
+ * `caption` or none: written after a blank line, rewritten, or taken out with that line when empty.
789
+ */
790
+ export function setTableCaption(source, end, caption, text) {
792
791
  const at = end > 0 && source[end - 1] === '\n' ? end - 1 : end;
793
- const caption = tableCaptionAt(source, end);
794
792
  const written = text.trim() === '' ? '' : `\n\nTable: ${text.trim()}`;
795
793
  if (caption)
796
- return caption.text === text.trim() ? [] : [{ from: caption.from, to: caption.to, insert: written }];
794
+ return captionText(source, caption) === text.trim() ? [] : [{ from: at, to: caption.to, insert: written }];
797
795
  return written ? [{ from: at, to: at, insert: written }] : [];
798
796
  }
799
- /** The field under a table that writes its caption, so the author never types `Table:` (#175). */
800
- function CaptionField({ view, end }) {
797
+ /**
798
+ * The field under every table that writes its caption, empty when it has none, so the author never
799
+ * types `Table:` (#175); the paragraph itself is drawn by the table's block, as this field (#182).
800
+ */
801
+ function CaptionField({ view, end, caption }) {
801
802
  const { strings } = useHost();
802
- const source = view.state.doc.sliceString(0, view.state.doc.length, '\n');
803
- const written = tableCaptionAt(source, end)?.text ?? '';
803
+ const written = caption ? captionText(view.state.doc.sliceString(0, view.state.doc.length, '\n'), caption) : '';
804
804
  const [value, setValue] = useState(written);
805
805
  useEffect(() => setValue(written), [written]);
806
806
  const commit = () => {
807
807
  const now = view.state.doc.sliceString(0, view.state.doc.length, '\n');
808
- write(view, setTableCaption(now, end, value));
808
+ write(view, setTableCaption(now, end, caption, value));
809
809
  };
810
810
  return (_jsxs("label", { className: "edit-table-caption", "data-ms-control": "", children: [_jsx("span", { children: strings.editor.tableCaption }), _jsx("input", { className: "edit-attach-input", value: value, onChange: (event) => setValue(event.target.value), onBlur: commit, onKeyDown: (event) => {
811
811
  if (event.key === 'Enter') {
@@ -115,9 +115,11 @@ export function estimateHeight(kind, source, metrics) {
115
115
  return Math.round(lineHeight + wrapped(text, metrics.width - 32, metrics) * lineHeight);
116
116
  }
117
117
  case 'table': {
118
- // A row per line but the delimiter row. The columns share the width by what they hold, so a
119
- // row wraps as the text of all its cells would in the width their padding leaves.
120
- const rows = lines.filter((line, index) => index !== 1 || !/^[\s|:-]+$/.test(line));
118
+ // A row per line but the delimiter row, up to the blank line before the caption the block
119
+ // covers (#182). The columns share the width by what they hold, so a row wraps as the text of
120
+ // all its cells would in the width their padding leaves.
121
+ const blank = lines.findIndex((line) => line.trim() === '');
122
+ const rows = (blank === -1 ? lines : lines.slice(0, blank)).filter((line, index) => index !== 1 || !/^[\s|:-]+$/.test(line));
121
123
  const cells = (row) => row.replace(/^\s*\|/, '').replace(/\|\s*$/, '').split('|').map((cell) => cell.trim());
122
124
  const columns = Math.max(1, ...rows.map((row) => cells(row).length));
123
125
  const width = metrics.width - 16 * columns;
package/dist/edit/live.js CHANGED
@@ -638,14 +638,19 @@ function build(state) {
638
638
  block(from, to, 'callout', `callout:${from}:${source}`, (view) => createElement(CalloutBlock, { view, text: source, marker }));
639
639
  }
640
640
  break;
641
- case 'table':
641
+ case 'table': {
642
642
  if (!alone(from, to))
643
643
  break;
644
- if (open)
645
- eachLine(from, to, 'cm-ms-table-source');
644
+ // Its `Table:` paragraph is drawn by the block, as the field under it (#182): the block
645
+ // covers it, and shows it among the table's lines when the author is on either.
646
+ const caption = construct.caption;
647
+ const end = caption ? linesOf(caption.from, caption.to).to : lines.to;
648
+ if (touches(lines.from, end))
649
+ eachLine(from, caption?.to ?? to, 'cm-ms-table-source');
646
650
  else
647
- block(from, to, 'table', `table:${from}:${source}`, (view) => createElement(TableBlock, { view, from, to }), false);
651
+ block(from, caption?.to ?? to, 'table', `table:${from}:${text.slice(lines.from, end)}`, (view) => createElement(TableBlock, { view, from, to, caption }), false);
648
652
  break;
653
+ }
649
654
  case 'mathBlock':
650
655
  if (!alone(from, to))
651
656
  break;
package/dist/outline.d.ts CHANGED
@@ -111,6 +111,8 @@ export interface KeptReference {
111
111
  readonly name: string;
112
112
  readonly title: string | null;
113
113
  }
114
+ /** The keyword of a Markdown table's caption (#175): `Table:`, Pandoc's, case folded. */
115
+ export declare const CAPTION: RegExp;
114
116
  /** The kept file an attachment link names, as `resolveWikilinks` writes it: `attachment:name|size`. */
115
117
  export declare function attachmentName(url: string): string | null;
116
118
  /** The kept file or remote image a node references, or `null`. */
package/dist/outline.js CHANGED
@@ -21,7 +21,7 @@ import { isPropertiesBlock } from './properties-block.js';
21
21
  import { keptImageName } from './kept-image.js';
22
22
  import { isTableBlock, readTableBlock } from './table-block.js';
23
23
  /** The keyword of a Markdown table's caption (#175): `Table:`, Pandoc's, case folded. */
24
- const CAPTION = /^table:[ \t]*/i;
24
+ export const CAPTION = /^table:[ \t]*/i;
25
25
  /** The kept file an attachment link names, as `resolveWikilinks` writes it: `attachment:name|size`. */
26
26
  export function attachmentName(url) {
27
27
  if (!url.startsWith('attachment:'))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@memorysmithapp/markdown-renderer",
3
- "version": "0.13.1",
3
+ "version": "0.13.2",
4
4
  "description": "A React component that draws a MemorySmith note exactly as MemorySmith's reading page does.",
5
5
  "keywords": [
6
6
  "markdown",
@@ -81,7 +81,7 @@
81
81
  "unified": "11.0.5"
82
82
  },
83
83
  "devDependencies": {
84
- "@memorysmithapp/markdown-spec": "0.13.1",
84
+ "@memorysmithapp/markdown-spec": "0.13.2",
85
85
  "@types/hast": "3.0.5",
86
86
  "@types/mdast": "4.0.4",
87
87
  "@types/react": "19.2.18",
package/theme-default.css CHANGED
@@ -2375,6 +2375,20 @@
2375
2375
  .markdown .edit-table table {
2376
2376
  width: 100%;
2377
2377
  }
2378
+ /* A table's caption is this field alone in the editor (#182): as wide as the
2379
+ block, so the caption reads whole. */
2380
+ .markdown .edit-table-caption {
2381
+ display: flex;
2382
+ align-items: center;
2383
+ gap: 0.5rem;
2384
+ margin-block-start: 0.35rem;
2385
+ color: var(--ms-text-soft);
2386
+ font-size: 0.9rem;
2387
+ }
2388
+ .markdown .edit-table-caption .edit-attach-input {
2389
+ flex: 1;
2390
+ min-width: 0;
2391
+ }
2378
2392
  .markdown .edit-cell {
2379
2393
  outline: none;
2380
2394
  }