@memorysmithapp/markdown-renderer 0.11.2 → 0.12.0
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 +3 -1
- package/dist/DataTable.js +118 -3
- package/dist/Markdown.d.ts +17 -1
- package/dist/Markdown.js +21 -4
- package/dist/NoteBody.js +48 -5
- package/dist/appendices.d.ts +51 -0
- package/dist/appendices.js +102 -0
- package/dist/diagnose.d.ts +2 -1
- package/dist/diagnose.js +10 -1
- package/dist/edit/NoteEditor.js +36 -5
- package/dist/edit/StyleDialog.d.ts +16 -0
- package/dist/edit/StyleDialog.js +64 -0
- package/dist/edit/analyze.d.ts +20 -1
- package/dist/edit/analyze.js +30 -1
- package/dist/edit/blocks.d.ts +11 -0
- package/dist/edit/blocks.js +24 -2
- package/dist/edit/heights.d.ts +1 -1
- package/dist/edit/heights.js +3 -0
- package/dist/edit/live.js +68 -2
- package/dist/edit/style.d.ts +29 -0
- package/dist/edit/style.js +71 -0
- package/dist/edit/table.d.ts +1 -1
- package/dist/edit/table.js +4 -1
- package/dist/host.d.ts +31 -1
- package/dist/host.js +7 -0
- package/dist/index.d.ts +3 -1
- package/dist/justified.d.ts +25 -0
- package/dist/justified.js +96 -0
- package/dist/note-style.d.ts +38 -0
- package/dist/note-style.js +53 -0
- package/dist/source.js +3 -2
- package/dist/strings.d.ts +36 -5
- package/dist/strings.js +20 -4
- package/dist/style-block.d.ts +78 -0
- package/dist/style-block.js +178 -0
- package/dist/table-block.d.ts +14 -0
- package/dist/theme.d.ts +12 -1
- package/dist/theme.js +8 -6
- package/package.json +2 -2
- package/structure.css +107 -0
- package/theme-default.css +132 -14
package/README.md
CHANGED
|
@@ -25,6 +25,8 @@ import 'katex/dist/katex.min.css';
|
|
|
25
25
|
noteAddress={addressOf(note)} // the note drawn, so ![[its own name]] reaches the file of that name
|
|
26
26
|
loadNoteAt={(address) => fetchTheNoteAt(address)} // …or another note of that name, told apart by address
|
|
27
27
|
queryTable={(request) => queryYourTable(request)} // the rows a ```table block asks for: one page, the count, the total
|
|
28
|
+
openTableRow={(view) => showInYourDialog(view)} // a row clicked, whole: its columns and each value as drawn
|
|
29
|
+
noteStyle={{ font: 'serif', text: 'justify' }} // the notebook's style: face, and alignment of headings, text and tables
|
|
28
30
|
writable
|
|
29
31
|
onToggleTask={(ordinal) => save(toggleTaskAt(latest(), ordinal))} // a promise locks the item under "Updating…" until it settles
|
|
30
32
|
theme="dark"
|
|
@@ -32,7 +34,7 @@ import 'katex/dist/katex.min.css';
|
|
|
32
34
|
/>;
|
|
33
35
|
```
|
|
34
36
|
|
|
35
|
-
Every prop is optional, and every default is the safe one: with no `resolve` every link is pending, with no `keeps` no embed reaches a file. A note comes before a file of the same name, and an embed never reaches the note it is written in: give `noteAddress` for a note you keep, and leave it out for a guidance, a template or a draft, which excludes nothing. A file `fileOf` describes is drawn by its media type, never its name: an image, an audio or a video player, and anything else a card with its type, size, description, *Open* where a browser shows it and *Download*; give `renderAttachment` instead to draw files yourself. A `table` block is drawn as its table through `queryTable`: it hands you the table, the columns, the filter as written, the order, the page and its size and the column to sum, you run the filter and answer the table's columns with their kinds, the page's rows — a number as its digits, so none is rounded — the row count, the total and when you read them; `runTableQuery` answers from rows you hold in memory. A reader sorts by a header for themselves, which asks you again; a block that cannot be answered says why in its place; without `queryTable` the block is a code block. To print, set `printing`, wait until no `[aria-busy="true"]` is left under the note, then print: every block has then asked for every row, and carries its header on each page, its total and a line saying what was read and when. A print command does not wait for an answer, so a note printed with no `printing` carries the page each table was on. In edit mode, `renderLink`, `renderAttachment` and a theme's slots may be drawn in a root of their own, outside your React tree, so what they return must not read your context: a router's `<Link>` throws there, and the block shows its Markdown under a notice instead. The stylesheet styles nothing outside `.markdown`. The faces of the default theme are Inter and Space Grotesk, which the page loads.
|
|
37
|
+
Every prop is optional, and every default is the safe one: with no `resolve` every link is pending, with no `keeps` no embed reaches a file. A note comes before a file of the same name, and an embed never reaches the note it is written in: give `noteAddress` for a note you keep, and leave it out for a guidance, a template or a draft, which excludes nothing. A file `fileOf` describes is drawn by its media type, never its name: an image, an audio or a video player, and anything else a card with its type, size, description, *Open* where a browser shows it and *Download*; give `renderAttachment` instead to draw files yourself. A `table` block is drawn as its table through `queryTable`: it hands you the table, the columns, the filter as written, the order, the page and its size and the column to sum, you run the filter and answer the table's columns with their kinds, the page's rows — a number as its digits, so none is rounded — the row count, the total and when you read them; `runTableQuery` answers from rows you hold in memory. On the screen a cell holds one line, the columns keep their widths as the pages turn and the footer its place, and a click on a row, or Enter, shows it whole: in your own dialog through `openTableRow`, else in a native `<dialog>`; the footer says which rows of how many it shows, in `strings.tableShowing`, with Previous and Next, which the `--markdown-pager-*` properties style as your own buttons. A reader sorts by a header for themselves, which asks you again; a block that cannot be answered says why in its place; without `queryTable` the block is a code block. To print, set `printing`, wait until no `[aria-busy="true"]` is left under the note, then print: every block has then asked for every row, and carries its header on each page, its total and a line saying what was read and when. A print command does not wait for an answer, so a note printed with no `printing` carries the page each table was on. Set `tablesAsAppendices` beside `printing` and every table of the note, a `table` block or a Markdown table, prints as an appendix at its end, titled in `strings.tableAppendix`, with a link in `strings.tableAppendixReference` in its place; each appendix is a `section.table-appendix` whose id the link names, so `target-counter()` can number its page and a named `page:` can turn it. In edit mode, `renderLink`, `renderAttachment` and a theme's slots may be drawn in a root of their own, outside your React tree, so what they return must not read your context: a router's `<Link>` throws there, and the block shows its Markdown under a notice instead. The stylesheet styles nothing outside `.markdown`. The faces of the default theme are Inter and Space Grotesk, which the page loads.
|
|
36
38
|
|
|
37
39
|
## Themes
|
|
38
40
|
|
package/dist/DataTable.js
CHANGED
|
@@ -23,12 +23,18 @@ import { useEffect, useMemo, useRef, useState } from 'react';
|
|
|
23
23
|
import { useHost } from './host.js';
|
|
24
24
|
import { checkTableBlock, fold, readTableBlock, } from './table-block.js';
|
|
25
25
|
export function DataTable({ source, fallback }) {
|
|
26
|
-
const { queryTable, strings, printing } = useHost();
|
|
26
|
+
const { queryTable, openTableRow, strings, printing } = useHost();
|
|
27
27
|
const block = useMemo(() => readTableBlock(source), [source]);
|
|
28
28
|
const readable = block.table !== null && block.problems.length === 0;
|
|
29
29
|
// Read through a ref: a host that gives its functions anew on each render asks nothing again (#99).
|
|
30
30
|
const query = useRef(queryTable);
|
|
31
31
|
query.current = queryTable;
|
|
32
|
+
const openRow = useRef(openTableRow);
|
|
33
|
+
openRow.current = openTableRow;
|
|
34
|
+
// The row a reader opened, where the host shows none itself (#163).
|
|
35
|
+
const [opened, setOpened] = useState(null);
|
|
36
|
+
// Each column's width on the screen, by its folded name, fixed by the first answer (#163).
|
|
37
|
+
const widths = useRef(new Map());
|
|
32
38
|
const [page, setPage] = useState(1);
|
|
33
39
|
// The reader's order, from a header: theirs alone, and gone when the note is opened again.
|
|
34
40
|
const [order, setOrder] = useState(null);
|
|
@@ -37,6 +43,8 @@ export function DataTable({ source, fallback }) {
|
|
|
37
43
|
useEffect(() => {
|
|
38
44
|
setPage(1);
|
|
39
45
|
setOrder(null);
|
|
46
|
+
setOpened(null);
|
|
47
|
+
widths.current = new Map();
|
|
40
48
|
}, [source]);
|
|
41
49
|
useEffect(() => {
|
|
42
50
|
if (!readable || block.table === null || !query.current)
|
|
@@ -72,6 +80,7 @@ export function DataTable({ source, fallback }) {
|
|
|
72
80
|
}
|
|
73
81
|
const columns = drawnColumns(answer.schema.columns, block.columns);
|
|
74
82
|
const pages = Math.max(1, Math.ceil(answer.count / block.page));
|
|
83
|
+
const shown = Math.min(page, pages);
|
|
75
84
|
const current = order ?? block.sort ?? [];
|
|
76
85
|
const sortBy = (column) => {
|
|
77
86
|
const first = current[0];
|
|
@@ -80,11 +89,74 @@ export function DataTable({ source, fallback }) {
|
|
|
80
89
|
setPage(1);
|
|
81
90
|
};
|
|
82
91
|
const totalAt = block.total === null ? -1 : columns.findIndex((column) => fold(column.name) === fold(block.total));
|
|
83
|
-
|
|
92
|
+
// On the screen each column keeps the width the first answer gave it, so turning a page or
|
|
93
|
+
// sorting moves none (#163); paper takes the widths its values need.
|
|
94
|
+
const layout = printing ? null : layoutOf(columns, answer, totalAt, strings, widths.current);
|
|
95
|
+
// A shorter last page keeps the height of a full one, so the footer stays where it was (#163).
|
|
96
|
+
const fillers = printing || pages < 2 ? 0 : Math.max(0, block.page - answer.rows.length);
|
|
97
|
+
const open = (row) => {
|
|
98
|
+
const view = { table: block.table ?? '', columns, row, values: columns.map((column) => formatValue(row[column.name] ?? null, column, strings)) };
|
|
99
|
+
if (openRow.current)
|
|
100
|
+
openRow.current(view);
|
|
101
|
+
else
|
|
102
|
+
setOpened(view);
|
|
103
|
+
};
|
|
104
|
+
return (_jsxs("div", { className: printing ? 'data-table data-table-printing' : 'data-table', "aria-busy": state.kind === 'loading', children: [_jsx("div", { className: "data-table-scroll", children: _jsxs("table", { style: layout ? { minWidth: layout.minWidth } : undefined, children: [layout && (_jsx("colgroup", { children: layout.widths.map((width, index) => (_jsx("col", { style: width ? { width } : undefined }, columns[index].name))) })), _jsx("thead", { children: _jsx("tr", { children: columns.map((column) => {
|
|
84
105
|
const first = current[0];
|
|
85
106
|
const sorted = first !== undefined && fold(first.column) === fold(column.name) ? (first.descending ? 'descending' : 'ascending') : undefined;
|
|
86
107
|
return (_jsx("th", { "aria-sort": sorted, className: alignOf(column), children: printing ? (_jsx("span", { className: "data-table-sort", children: column.name })) : (_jsxs("button", { type: "button", className: "data-table-sort", title: strings.tableSortBy(column.name), onClick: () => sortBy(column), children: [column.name, sorted && _jsx("span", { "aria-hidden": "true", children: sorted === 'ascending' ? ' ▲' : ' ▼' })] })) }, column.name));
|
|
87
|
-
}) }) }),
|
|
108
|
+
}) }) }), _jsxs("tbody", { children: [answer.rows.length === 0 ? (_jsx("tr", { children: _jsx("td", { className: "data-table-empty", colSpan: columns.length, children: strings.tableEmpty }) })) : (answer.rows.map((row, index) => printing ? (_jsx("tr", { children: columns.map((column) => (_jsx("td", { className: alignOf(column), children: formatValue(row[column.name] ?? null, column, strings) }, column.name))) }, index)) : (_jsx("tr", { className: "data-table-row", tabIndex: 0, onClick: () => {
|
|
109
|
+
// Text the reader selected in a cell is theirs to copy, and opens nothing.
|
|
110
|
+
if (!window.getSelection?.()?.isCollapsed && window.getSelection()?.toString())
|
|
111
|
+
return;
|
|
112
|
+
open(row);
|
|
113
|
+
}, onKeyDown: (event) => {
|
|
114
|
+
if (event.key !== 'Enter' || event.target !== event.currentTarget)
|
|
115
|
+
return;
|
|
116
|
+
event.preventDefault();
|
|
117
|
+
open(row);
|
|
118
|
+
}, children: columns.map((column) => {
|
|
119
|
+
const value = formatValue(row[column.name] ?? null, column, strings);
|
|
120
|
+
return (_jsx("td", { className: alignOf(column), title: value || undefined, children: value }, column.name));
|
|
121
|
+
}) }, index)))), Array.from({ length: fillers }, (_, index) => (_jsx("tr", { className: "data-table-filler", "aria-hidden": "true", children: _jsx("td", { colSpan: columns.length, children: '\u00a0' }) }, `filler-${index}`)))] }), totalAt !== -1 && answer.total !== null && (_jsx("tfoot", { children: _jsx("tr", { children: columns.map((column, index) => (_jsx("td", { className: alignOf(column), children: index === totalAt ? formatNumber(answer.total, strings.locale) : index === 0 ? strings.tableTotal : null }, column.name))) }) }))] }) }), printing ? (_jsx("p", { className: "data-table-printed", children: strings.tablePrinted({ table: block.table ?? '', filter: block.whereText, count: answer.count, readAt: formatDate(answer.readAt, strings.locale) ?? answer.readAt }) })) : (_jsxs("div", { className: "data-table-footer", children: [_jsx("span", { className: "data-table-count", children: strings.tableShowing(answer.count === 0 ? 0 : (shown - 1) * block.page + 1, Math.min(shown * block.page, answer.count), answer.count) }), pages > 1 && (_jsxs("span", { className: "data-table-pages", children: [_jsxs("button", { type: "button", className: "data-table-page", disabled: shown <= 1, onClick: () => setPage(shown - 1), title: strings.tablePrevious, children: [_jsx("span", { className: "data-table-page-arrow", "aria-hidden": "true", children: "\u2039" }), _jsx("span", { className: "data-table-page-word", children: strings.tablePrevious })] }), _jsxs("button", { type: "button", className: "data-table-page", disabled: shown >= pages, onClick: () => setPage(shown + 1), title: strings.tableNext, children: [_jsx("span", { className: "data-table-page-word", children: strings.tableNext }), _jsx("span", { className: "data-table-page-arrow", "aria-hidden": "true", children: "\u203A" })] })] }))] })), opened && _jsx(RowDialog, { view: opened, onClose: () => setOpened(null) })] }));
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* A row shown whole where the host gives no `openTableRow` (#163): a native modal dialog with each
|
|
125
|
+
* column's name and its value as the table draws it, wrapping as long as it needs. Escape, the
|
|
126
|
+
* button and a click outside it close it.
|
|
127
|
+
*/
|
|
128
|
+
function RowDialog({ view, onClose }) {
|
|
129
|
+
const { strings } = useHost();
|
|
130
|
+
const dialog = useRef(null);
|
|
131
|
+
const close = useRef(onClose);
|
|
132
|
+
close.current = onClose;
|
|
133
|
+
useEffect(() => {
|
|
134
|
+
const element = dialog.current;
|
|
135
|
+
if (!element)
|
|
136
|
+
return;
|
|
137
|
+
const closed = () => close.current();
|
|
138
|
+
element.addEventListener('close', closed);
|
|
139
|
+
// A browser without modal dialogs shows it in place, open.
|
|
140
|
+
if (typeof element.showModal === 'function') {
|
|
141
|
+
if (!element.open)
|
|
142
|
+
element.showModal();
|
|
143
|
+
}
|
|
144
|
+
else
|
|
145
|
+
element.setAttribute('open', '');
|
|
146
|
+
return () => element.removeEventListener('close', closed);
|
|
147
|
+
}, []);
|
|
148
|
+
const dismiss = () => {
|
|
149
|
+
if (typeof dialog.current?.close === 'function' && dialog.current.open)
|
|
150
|
+
dialog.current.close();
|
|
151
|
+
else
|
|
152
|
+
close.current();
|
|
153
|
+
};
|
|
154
|
+
return (_jsx("dialog", { ref: dialog, className: "data-table-dialog", "aria-label": view.table, onClick: (event) => event.target === event.currentTarget && dismiss(), onKeyDown: (event) => {
|
|
155
|
+
if (event.key !== 'Escape')
|
|
156
|
+
return;
|
|
157
|
+
event.preventDefault();
|
|
158
|
+
dismiss();
|
|
159
|
+
}, children: _jsxs("div", { className: "data-table-dialog-body", children: [_jsx("p", { className: "data-table-dialog-title", children: view.table }), _jsx("dl", { children: view.columns.map((column, index) => (_jsxs("div", { children: [_jsx("dt", { children: column.name }), _jsx("dd", { className: alignOf(column), children: view.values[index] })] }, column.name))) }), _jsx("button", { type: "button", className: "data-table-dialog-close", onClick: dismiss, children: strings.close })] }) }));
|
|
88
160
|
}
|
|
89
161
|
/** What is wrong with a block, in place of its table: each mistake, with the line that makes it. */
|
|
90
162
|
function TableProblems({ block, source, problems }) {
|
|
@@ -98,6 +170,49 @@ function drawnColumns(schema, named) {
|
|
|
98
170
|
return [...schema];
|
|
99
171
|
return named.map((name) => schema.find((column) => fold(column.name) === fold(name))).filter((column) => column !== undefined);
|
|
100
172
|
}
|
|
173
|
+
/**
|
|
174
|
+
* How a block lays its columns out on the screen (#163): at the width of the block, a date, a number
|
|
175
|
+
* and a yes or no as wide as their drawn values need, and the text and list columns sharing the
|
|
176
|
+
* rest. A column's width is fixed the first time it is drawn and kept in `known`, so no page and no
|
|
177
|
+
* order moves it; a longer value later ends in an ellipsis, as a long text does, and its row shows
|
|
178
|
+
* it whole. Below the width the fixed columns and a least share for each text column take, the
|
|
179
|
+
* block scrolls sideways.
|
|
180
|
+
*/
|
|
181
|
+
function layoutOf(columns, answer, totalAt, strings, known) {
|
|
182
|
+
const sizes = columns.map((column, index) => {
|
|
183
|
+
const key = fold(column.name);
|
|
184
|
+
if (!known.has(key))
|
|
185
|
+
known.set(key, characters(column, answer, index === totalAt, strings));
|
|
186
|
+
return known.get(key) ?? null;
|
|
187
|
+
});
|
|
188
|
+
const fixed = sizes.filter((size) => size !== null);
|
|
189
|
+
const shared = sizes.length - fixed.length;
|
|
190
|
+
const sum = fixed.reduce((total, size) => total + size, 0);
|
|
191
|
+
return {
|
|
192
|
+
widths: sizes.map((size) => (size === null ? null : `calc(${size}ch + ${CELL_SPACE})`)),
|
|
193
|
+
minWidth: `calc(${sum}ch + ${fixed.length} * ${CELL_SPACE} + ${shared} * ${TEXT_LEAST})`,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
/** What a cell takes beside its text: its padding and its rule. */
|
|
197
|
+
const CELL_SPACE = '1.25rem';
|
|
198
|
+
/** The least a text column keeps before the block scrolls sideways. */
|
|
199
|
+
const TEXT_LEAST = '8rem';
|
|
200
|
+
/** How many characters wide a column's drawn values are, or `null` for a column that shares what is left. */
|
|
201
|
+
function characters(column, answer, isTotal, strings) {
|
|
202
|
+
if (column.kind === 'text' || column.kind === 'list')
|
|
203
|
+
return null;
|
|
204
|
+
const drawn = answer.rows.map((row) => formatValue(row[column.name] ?? null, column, strings));
|
|
205
|
+
if (isTotal && answer.total !== null)
|
|
206
|
+
drawn.push(formatNumber(answer.total, strings.locale));
|
|
207
|
+
if (column.kind === 'boolean')
|
|
208
|
+
drawn.push(strings.yes, strings.no);
|
|
209
|
+
if (column.kind === 'date' && !drawn.some((value) => value !== ''))
|
|
210
|
+
drawn.push(formatDate('2000-12-28', strings.locale) ?? '');
|
|
211
|
+
// The header shows its name and the arrow of an order, and may end in an ellipsis past a few words.
|
|
212
|
+
const header = Math.min([...column.name].length + 2, 16);
|
|
213
|
+
// A letter of a date or a word is about as wide as a digit, some a little wider.
|
|
214
|
+
return Math.max(header, Math.ceil(Math.max(4, ...drawn.map((value) => [...value].length)) * 1.1));
|
|
215
|
+
}
|
|
101
216
|
const alignOf = (column) => column.kind === 'number' ? 'data-table-number' : column.kind === 'date' ? 'data-table-date' : undefined;
|
|
102
217
|
/** A value as the reader's locale writes it, and as text. */
|
|
103
218
|
export function formatValue(value, column, strings) {
|
package/dist/Markdown.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { type AnchorHTMLAttributes, type ImgHTMLAttributes, type ReactNode } from 'react';
|
|
2
|
+
import { type TableAppendix } from './appendices.js';
|
|
3
|
+
import { type StyleRange } from './style-block.js';
|
|
2
4
|
import { type FootnoteRef } from './footnotes.js';
|
|
3
5
|
import type { TaskSave } from './task-saves.js';
|
|
4
6
|
/**
|
|
@@ -30,6 +32,20 @@ export interface MarkdownProps {
|
|
|
30
32
|
readonly footnotes?: {
|
|
31
33
|
readonly refs: readonly FootnoteRef[];
|
|
32
34
|
} | 'section';
|
|
35
|
+
/**
|
|
36
|
+
* The tables of this run printed as appendices (#165, `appendices.ts`): the number its first
|
|
37
|
+
* table takes, and the note's appendices, whose references take the tables' places.
|
|
38
|
+
*/
|
|
39
|
+
readonly appendices?: {
|
|
40
|
+
readonly first: number;
|
|
41
|
+
readonly appendices: readonly TableAppendix[];
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* The `:::{style}` blocks this run holds, by its own lines, when the note they belong to was read
|
|
45
|
+
* whole (`NoteBody`), its fences already blanked. Left out, the run finds its own: an embedded
|
|
46
|
+
* note, which is drawn on its own.
|
|
47
|
+
*/
|
|
48
|
+
readonly styles?: readonly StyleRange[];
|
|
33
49
|
}
|
|
34
50
|
/**
|
|
35
51
|
* What react-markdown hands a component beside the attributes: the hast
|
|
@@ -52,5 +68,5 @@ type WithNode<T> = T & {
|
|
|
52
68
|
*/
|
|
53
69
|
export declare function MarkdownImage({ alt, node: _node, ...rest }: WithNode<ImgHTMLAttributes<HTMLImageElement>>): import("react").JSX.Element;
|
|
54
70
|
export declare function MarkdownAnchor({ href, children, node: _node, ...rest }: WithNode<AnchorHTMLAttributes<HTMLAnchorElement>>): string | number | bigint | boolean | import("react").JSX.Element | Iterable<ReactNode> | Promise<string | number | bigint | boolean | import("react").ReactPortal | import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>> | Iterable<ReactNode> | null | undefined> | null | undefined;
|
|
55
|
-
export declare function Markdown({ children, source, onToggleTask, taskSave, writable, footnotes }: MarkdownProps): import("react").JSX.Element;
|
|
71
|
+
export declare function Markdown({ children, source, onToggleTask, taskSave, writable, footnotes, appendices, styles }: MarkdownProps): import("react").JSX.Element;
|
|
56
72
|
export {};
|
package/dist/Markdown.js
CHANGED
|
@@ -17,6 +17,9 @@ import { choiceTargetOf, LinkChoice } from './LinkChoice.js';
|
|
|
17
17
|
import { toUnixNewlines } from './source.js';
|
|
18
18
|
import { MermaidDiagram } from './MermaidDiagram.js';
|
|
19
19
|
import { readingPlugins } from './reading.js';
|
|
20
|
+
import { remarkTableReferences } from './appendices.js';
|
|
21
|
+
import { justifyColumns, remarkJustifiedColumns } from './justified.js';
|
|
22
|
+
import { findStyleBlocks, rangesFor, remarkStyleBlocks } from './style-block.js';
|
|
20
23
|
import { CLOBBER_PREFIX, rehypeFootnoteRun, rehypeFootnoteSection } from './footnotes.js';
|
|
21
24
|
import { drawSlot, headings, InBlock, MarkdownBlockquote, MarkdownDiv, MarkdownSection, MarkdownTable, useInBlock, useSlot } from './slots.js';
|
|
22
25
|
import { ordinalAt } from './tasklist.js';
|
|
@@ -377,14 +380,28 @@ function dropBox(children) {
|
|
|
377
380
|
return children;
|
|
378
381
|
return children.filter((child) => !(isValidElement(child) && child.type === 'input'));
|
|
379
382
|
}
|
|
380
|
-
export function Markdown({ children, source, onToggleTask, taskSave, writable = false, footnotes }) {
|
|
383
|
+
export function Markdown({ children, source, onToggleTask, taskSave, writable = false, footnotes, appendices, styles }) {
|
|
381
384
|
const text = toUnixNewlines(children);
|
|
382
|
-
const { strings } = useHost();
|
|
385
|
+
const { strings, queryTable } = useHost();
|
|
383
386
|
const calloutTitle = (kind) => Object.hasOwn(strings.callouts, kind) ? strings.callouts[kind] : undefined;
|
|
387
|
+
// A style block's fences and options leave the text, every offset kept, and its lines are wrapped once parsed (§7.16).
|
|
388
|
+
const styled = useMemo(() => {
|
|
389
|
+
if (styles)
|
|
390
|
+
return { text, ranges: styles };
|
|
391
|
+
const found = findStyleBlocks(text);
|
|
392
|
+
return { text: found.text, ranges: rangesFor(found.blocks, 1, found.text.split('\n').length) };
|
|
393
|
+
}, [text, styles]);
|
|
394
|
+
// A justified column, `;---;`, is read as a centred one and turned justified once parsed (§4.1).
|
|
395
|
+
const justified = useMemo(() => justifyColumns(styled.text), [styled.text]);
|
|
384
396
|
return (
|
|
385
397
|
// The pass that draws a cut note's footnotes continues the note rather than opening a run,
|
|
386
398
|
// so its section keeps the space a section takes at the end of a note.
|
|
387
|
-
_jsx("div", { className: footnotes === 'section' ? 'markdown footnotes-run' : 'markdown', children: _jsx(ReactMarkdown, { remarkPlugins:
|
|
399
|
+
_jsx("div", { className: footnotes === 'section' ? 'markdown footnotes-run' : 'markdown', children: _jsx(ReactMarkdown, { remarkPlugins: [
|
|
400
|
+
...readingPlugins(calloutTitle, strings.parameter),
|
|
401
|
+
[remarkJustifiedColumns, { columns: justified.columns }],
|
|
402
|
+
[remarkStyleBlocks, { ranges: styled.ranges, locale: strings.locale }],
|
|
403
|
+
...(appendices ? [[remarkTableReferences, { ...appendices, blocks: queryTable !== null, words: strings.tableAppendixReference }]] : []),
|
|
404
|
+
],
|
|
388
405
|
// Raw HTML is NOT enabled, and its absence is the point: no
|
|
389
406
|
// `rehype-raw` is loaded, so a note carrying `<script>` is text. It is
|
|
390
407
|
// a security boundary rather than a rendering preference, because a
|
|
@@ -422,5 +439,5 @@ export function Markdown({ children, source, onToggleTask, taskSave, writable =
|
|
|
422
439
|
section: MarkdownSection,
|
|
423
440
|
...headings,
|
|
424
441
|
li: (props) => (_jsx(TaskItem, { ...props, source: source ?? text, toggleTask: onToggleTask, taskSave: taskSave, writable: writable })),
|
|
425
|
-
}, children: text }) }));
|
|
442
|
+
}, children: justified.text }) }));
|
|
426
443
|
}
|
package/dist/NoteBody.js
CHANGED
|
@@ -12,9 +12,12 @@ import { readProperties } from './properties.js';
|
|
|
12
12
|
import { Properties } from './PropertiesBlock.js';
|
|
13
13
|
import { useTaskSaves } from './task-saves.js';
|
|
14
14
|
import { taskBoxes } from './tasklist.js';
|
|
15
|
+
import { justifies } from './note-style.js';
|
|
15
16
|
import { rootStyle } from './theme.js';
|
|
16
17
|
import { Transclusion } from './Transclusion.js';
|
|
17
18
|
import { splitEmbeds } from './embeds.js';
|
|
19
|
+
import { appendixId, planAppendices } from './appendices.js';
|
|
20
|
+
import { findStyleBlocks, rangesFor, styleAt, blockAttributes } from './style-block.js';
|
|
18
21
|
import { embedsFile } from './embed-target.js';
|
|
19
22
|
import { wikilinkAddress } from './wikilink-address.js';
|
|
20
23
|
/** The editor, loaded the first time a note is edited, so a host that only draws never loads it. */
|
|
@@ -103,7 +106,11 @@ function NoteView({ markdown, writable = false, onToggleTask, corner, switched =
|
|
|
103
106
|
// is parsed on its own, as on MemorySmith's reading page.
|
|
104
107
|
// A note answers an embed before a file of its name does (§5.8, #148).
|
|
105
108
|
const file = embedsFile(resolved);
|
|
106
|
-
|
|
109
|
+
// The note's `:::{style}` blocks (§7.16), read on the whole body because one may hold an embed
|
|
110
|
+
// and so reach across runs: their fences and options leave the text, every line kept, and each
|
|
111
|
+
// run is told which of its lines a block holds.
|
|
112
|
+
const styled = useMemo(() => findStyleBlocks(body), [body]);
|
|
113
|
+
const segments = splitEmbeds(styled.text, file).map((segment) => segment.kind === 'text'
|
|
107
114
|
? {
|
|
108
115
|
...segment,
|
|
109
116
|
// One resolver for every run: one note is a link, several are the
|
|
@@ -152,13 +159,41 @@ function NoteView({ markdown, writable = false, onToggleTask, corner, switched =
|
|
|
152
159
|
const footnotes = useMemo(() => planFootnotes(runs, runs.length < segments.length, readingPlugins(() => undefined)),
|
|
153
160
|
// The runs and whether an embed cut them are the whole input.
|
|
154
161
|
[runs.join('\u0000'), runs.length < segments.length]);
|
|
162
|
+
// The tables printed as appendices at the end of the note (#165): planned over every run, as
|
|
163
|
+
// the footnotes are, so their numbers run across the embeds that cut the note.
|
|
164
|
+
const appendixRuns = resolved.printing && resolved.tablesAsAppendices;
|
|
165
|
+
const blocks = resolved.queryTable !== null;
|
|
166
|
+
const appendices = useMemo(() => (appendixRuns ? planAppendices(runs, blocks) : null),
|
|
167
|
+
// The runs and whether a `table` block is a table are the whole input.
|
|
168
|
+
[appendixRuns, blocks, runs.join('\u0000')]);
|
|
155
169
|
let run = 0;
|
|
156
|
-
|
|
170
|
+
// The line of the body each segment starts on: a run keeps every line of its text, and an embed
|
|
171
|
+
// stands within one. Where that does not add up, no block is drawn rather than a wrong one.
|
|
172
|
+
const startLines = [];
|
|
173
|
+
let line = 1;
|
|
174
|
+
for (const segment of segments) {
|
|
175
|
+
startLines.push(line);
|
|
176
|
+
if ('rendered' in segment)
|
|
177
|
+
line += segment.text.split('\n').length - 1;
|
|
178
|
+
}
|
|
179
|
+
const lined = styled.blocks.length > 0 && line === styled.text.split('\n').length;
|
|
180
|
+
return (_jsx(HostProvider, { value: resolved, children: _jsxs("div", { className: `markdown markdown-theme-${resolved.theme.name}${corner ? ' has-note-corner' : ''}${switched ? ' has-mode-switch' : ''}${corner && buttons === 'responsive' ? ' has-responsive-buttons' : ''}`, "data-theme": resolved.scheme, style: rootStyle(resolved.theme, resolved.scheme, resolved.noteStyle),
|
|
181
|
+
// Justified text hyphenates in the reader's language, which the browser has to be told (#164).
|
|
182
|
+
lang: justifies(resolved.noteStyle) ? resolved.strings.locale : undefined, children: [corner, _jsx(Properties, { properties: properties }), segments.map((segment, index) => {
|
|
157
183
|
if (!('rendered' in segment)) {
|
|
158
|
-
|
|
184
|
+
const embed = _jsx(Transclusion, { target: segment.target, anchor: segment.anchor, source: segment.source }, index);
|
|
185
|
+
// An embed a style block holds is drawn in that block's style.
|
|
186
|
+
const style = lined ? styleAt(styled.blocks, startLines[index]) : {};
|
|
187
|
+
if (Object.keys(style).length === 0)
|
|
188
|
+
return embed;
|
|
189
|
+
const { className, style: css, ...attributes } = blockAttributes(style, resolved.strings.locale);
|
|
190
|
+
// The attributes as the page writes them: `dataText` is `data-text`.
|
|
191
|
+
const written = Object.fromEntries(Object.entries(attributes).map(([name, value]) => [name.replace(/^data([A-Z])/, (_, first) => `data-${first.toLowerCase()}`), value]));
|
|
192
|
+
return (_jsx("div", { className: className.join(' '), style: cssObject(css), ...written, children: embed }, index));
|
|
159
193
|
}
|
|
160
194
|
const base = seen;
|
|
161
195
|
seen += taskBoxes(segment.rendered).length;
|
|
196
|
+
const first = appendices?.first[run];
|
|
162
197
|
const planned = footnotes?.runs[run++];
|
|
163
198
|
return (_jsx(Markdown, {
|
|
164
199
|
// The text the ordinal is counted over, stated rather than left to
|
|
@@ -166,6 +201,14 @@ function NoteView({ markdown, writable = false, onToggleTask, corner, switched =
|
|
|
166
201
|
// `mappable` above is what makes it safe to write back through.
|
|
167
202
|
// A planned run is the same text with definitions appended after
|
|
168
203
|
// it, so every box keeps the offset it had.
|
|
169
|
-
source: planned?.text ?? segment.rendered, onToggleTask: onToggleTask && ((ordinal, checked) => saves.toggle(base + ordinal, checked)), taskSave: (ordinal) => saves.stateOf(base + ordinal), writable: writable && mappable, ...(planned ? { footnotes: { refs: planned.refs } } : {}), children: planned?.text ?? segment.rendered }, index));
|
|
170
|
-
}), footnotes && _jsx(Markdown, { footnotes: "section", children: footnotes.section }), hosts.length > 0 && _jsx("p", { className: "remote-notice", children: resolved.strings.remoteImages(hosts) }), writable && onToggleTask && (_jsx("p", { className: "visually-hidden", role: "status", "aria-live": "polite", children: saves.announcement && resolved.strings[saves.announcement] }))] }) }));
|
|
204
|
+
source: planned?.text ?? segment.rendered, onToggleTask: onToggleTask && ((ordinal, checked) => saves.toggle(base + ordinal, checked)), taskSave: (ordinal) => saves.stateOf(base + ordinal), writable: writable && mappable, ...(planned ? { footnotes: { refs: planned.refs } } : {}), ...(appendices && first !== undefined ? { appendices: { first, appendices: appendices.appendices } } : {}), styles: lined ? rangesFor(styled.blocks, startLines[index], startLines[index] + segment.text.split('\n').length - 1) : NO_STYLES, children: planned?.text ?? segment.rendered }, index));
|
|
205
|
+
}), footnotes && _jsx(Markdown, { footnotes: "section", children: footnotes.section }), appendices && appendices.appendices.length > 0 && (_jsx("div", { className: "table-appendices", children: appendices.appendices.map((appendix) => (_jsxs("section", { id: appendixId(appendix.number), className: "table-appendix", "data-table-kind": appendix.table === null ? 'markdown' : 'block', children: [_jsx("h2", { className: "table-appendix-title", children: resolved.strings.tableAppendix(appendix) }), _jsx(Markdown, { children: appendix.source })] }, appendix.number))) })), hosts.length > 0 && _jsx("p", { className: "remote-notice", children: resolved.strings.remoteImages(hosts) }), writable && onToggleTask && (_jsx("p", { className: "visually-hidden", role: "status", "aria-live": "polite", children: saves.announcement && resolved.strings[saves.announcement] }))] }) }));
|
|
206
|
+
}
|
|
207
|
+
/** A run with no style block: given, so it does not look for one in text whose fences are gone. */
|
|
208
|
+
const NO_STYLES = [];
|
|
209
|
+
/** A block's custom properties, as the `style` of a React element. */
|
|
210
|
+
function cssObject(css) {
|
|
211
|
+
if (!css)
|
|
212
|
+
return undefined;
|
|
213
|
+
return Object.fromEntries(css.split(';').map((pair) => [pair.slice(0, pair.indexOf(':')), pair.slice(pair.indexOf(':') + 1)]));
|
|
171
214
|
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The tables of a printed note as appendices at its end (#165, memorysmithapp/memorysmithapp#494).
|
|
3
|
+
*
|
|
4
|
+
* When the host prints with `tablesAsAppendices`, each table of the note — a Markdown table, and a
|
|
5
|
+
* `table` block where the host reaches the tables — gives its place to a reference to its appendix,
|
|
6
|
+
* and the appendices follow the note, in the order the tables appear, numbered in one sequence
|
|
7
|
+
* across both kinds. A Markdown table has no name, so it is also counted among the Markdown tables,
|
|
8
|
+
* which is what its title says in place of a name.
|
|
9
|
+
*
|
|
10
|
+
* The note is drawn in runs, each parsed on its own (`NoteBody`), so the plan is made once over every
|
|
11
|
+
* run, as the footnotes' is, and each run is told the number its first table takes. The run finds
|
|
12
|
+
* its tables again with the same predicate, in the same order, while it is drawn, and puts the
|
|
13
|
+
* reference in their place; the appendix draws the table again from its own Markdown, which prints
|
|
14
|
+
* as a table of its kind prints. The note's own tables only: an embedded note draws its tables in
|
|
15
|
+
* place, as it draws everything else.
|
|
16
|
+
*/
|
|
17
|
+
/** A table printed as an appendix, as the host's words receive it. */
|
|
18
|
+
export interface TableAppendix {
|
|
19
|
+
/** Its place among the appendices of the note, from 1. */
|
|
20
|
+
readonly number: number;
|
|
21
|
+
/** The table a `table` block names, or `null` for a Markdown table. */
|
|
22
|
+
readonly table: string | null;
|
|
23
|
+
/** Its place among the note's Markdown tables, from 1, or `null` for a `table` block. */
|
|
24
|
+
readonly markdownTable: number | null;
|
|
25
|
+
}
|
|
26
|
+
/** An appendix and the Markdown that draws its table. */
|
|
27
|
+
export interface PlannedAppendix extends TableAppendix {
|
|
28
|
+
readonly source: string;
|
|
29
|
+
}
|
|
30
|
+
export interface AppendixPlan {
|
|
31
|
+
/** The number the first table of each run takes, in the order of the runs. */
|
|
32
|
+
readonly first: readonly number[];
|
|
33
|
+
readonly appendices: readonly PlannedAppendix[];
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The appendices of a note drawn in `runs`: each table found in each run, numbered across them.
|
|
37
|
+
* `blocks` is whether a `table` block is drawn as a table, which it is where the host reaches tables.
|
|
38
|
+
*/
|
|
39
|
+
export declare function planAppendices(runs: readonly string[], blocks: boolean): AppendixPlan;
|
|
40
|
+
/** The id of an appendix, which its reference points at and a host's `target-counter()` reads. */
|
|
41
|
+
export declare const appendixId: (number: number) => string;
|
|
42
|
+
/**
|
|
43
|
+
* Puts a reference to its appendix in place of each table of a run: the paragraph `table-reference`,
|
|
44
|
+
* holding a link to the appendix in the host's words.
|
|
45
|
+
*/
|
|
46
|
+
export declare function remarkTableReferences(options: {
|
|
47
|
+
readonly first: number;
|
|
48
|
+
readonly blocks: boolean;
|
|
49
|
+
readonly appendices: readonly TableAppendix[];
|
|
50
|
+
readonly words: (appendix: TableAppendix) => string;
|
|
51
|
+
}): (tree: unknown) => void;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The tables of a printed note as appendices at its end (#165, memorysmithapp/memorysmithapp#494).
|
|
3
|
+
*
|
|
4
|
+
* When the host prints with `tablesAsAppendices`, each table of the note — a Markdown table, and a
|
|
5
|
+
* `table` block where the host reaches the tables — gives its place to a reference to its appendix,
|
|
6
|
+
* and the appendices follow the note, in the order the tables appear, numbered in one sequence
|
|
7
|
+
* across both kinds. A Markdown table has no name, so it is also counted among the Markdown tables,
|
|
8
|
+
* which is what its title says in place of a name.
|
|
9
|
+
*
|
|
10
|
+
* The note is drawn in runs, each parsed on its own (`NoteBody`), so the plan is made once over every
|
|
11
|
+
* run, as the footnotes' is, and each run is told the number its first table takes. The run finds
|
|
12
|
+
* its tables again with the same predicate, in the same order, while it is drawn, and puts the
|
|
13
|
+
* reference in their place; the appendix draws the table again from its own Markdown, which prints
|
|
14
|
+
* as a table of its kind prints. The note's own tables only: an embedded note draws its tables in
|
|
15
|
+
* place, as it draws everything else.
|
|
16
|
+
*/
|
|
17
|
+
import remarkGfm from 'remark-gfm';
|
|
18
|
+
import remarkParse from 'remark-parse';
|
|
19
|
+
import { unified } from 'unified';
|
|
20
|
+
import { justifyColumns } from './justified.js';
|
|
21
|
+
import { isTableBlock, readTableBlock } from './table-block.js';
|
|
22
|
+
/** Whether a node is a table the note draws: a Markdown table, or a `table` block where tables are reached. */
|
|
23
|
+
function isAppendixTable(node, blocks) {
|
|
24
|
+
return node.type === 'table' || (blocks && node.type === 'code' && isTableBlock(node.lang, node.meta));
|
|
25
|
+
}
|
|
26
|
+
/** Each table of a tree, in the order they are read, never one inside another's place. */
|
|
27
|
+
function tablesOf(tree, blocks) {
|
|
28
|
+
const found = [];
|
|
29
|
+
const walk = (parent) => {
|
|
30
|
+
(parent.children ?? []).forEach((node, index) => {
|
|
31
|
+
if (isAppendixTable(node, blocks))
|
|
32
|
+
found.push({ node, parent, index });
|
|
33
|
+
else
|
|
34
|
+
walk(node);
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
walk(tree);
|
|
38
|
+
return found;
|
|
39
|
+
}
|
|
40
|
+
const parser = unified().use(remarkParse).use(remarkGfm, { singleTilde: false });
|
|
41
|
+
/**
|
|
42
|
+
* The appendices of a note drawn in `runs`: each table found in each run, numbered across them.
|
|
43
|
+
* `blocks` is whether a `table` block is drawn as a table, which it is where the host reaches tables.
|
|
44
|
+
*/
|
|
45
|
+
export function planAppendices(runs, blocks) {
|
|
46
|
+
const first = [];
|
|
47
|
+
const appendices = [];
|
|
48
|
+
let markdownTables = 0;
|
|
49
|
+
for (const text of runs) {
|
|
50
|
+
first.push(appendices.length + 1);
|
|
51
|
+
// Read as the run reads it, a justified column among them (§4.1).
|
|
52
|
+
for (const { node } of tablesOf(parser.parse(justifyColumns(text).text), blocks)) {
|
|
53
|
+
const number = appendices.length + 1;
|
|
54
|
+
if (node.type === 'code') {
|
|
55
|
+
appendices.push({ number, table: readTableBlock(node.value ?? '').table, markdownTable: null, source: fenced(node.value ?? '') });
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
markdownTables += 1;
|
|
59
|
+
appendices.push({ number, table: null, markdownTable: markdownTables, source: sliceOf(text, node) });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return { first, appendices };
|
|
64
|
+
}
|
|
65
|
+
/** A `table` block's lines in a fence of their own, longer than any run of backticks they hold. */
|
|
66
|
+
function fenced(value) {
|
|
67
|
+
const longest = Math.max(0, ...[...value.matchAll(/`+/g)].map((run) => run[0].length));
|
|
68
|
+
const fence = '`'.repeat(Math.max(3, longest + 1));
|
|
69
|
+
return `${fence}table\n${value}\n${fence}\n`;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* A Markdown table's lines, out of the quote, callout or list item that holds it: each line after
|
|
73
|
+
* the first loses the prefix of its container, which is as wide as the table's first column is far
|
|
74
|
+
* from the margin.
|
|
75
|
+
*/
|
|
76
|
+
function sliceOf(text, node) {
|
|
77
|
+
const start = node.position?.start.offset ?? 0;
|
|
78
|
+
const end = node.position?.end.offset ?? text.length;
|
|
79
|
+
const indent = (node.position?.start.column ?? 1) - 1;
|
|
80
|
+
const lines = text.slice(start, end).split('\n');
|
|
81
|
+
return `${lines.map((line, index) => (index === 0 || !/^[ \t>]*$/.test(line.slice(0, indent)) ? line : line.slice(indent))).join('\n')}\n`;
|
|
82
|
+
}
|
|
83
|
+
/** The id of an appendix, which its reference points at and a host's `target-counter()` reads. */
|
|
84
|
+
export const appendixId = (number) => `table-appendix-${number}`;
|
|
85
|
+
/**
|
|
86
|
+
* Puts a reference to its appendix in place of each table of a run: the paragraph `table-reference`,
|
|
87
|
+
* holding a link to the appendix in the host's words.
|
|
88
|
+
*/
|
|
89
|
+
export function remarkTableReferences(options) {
|
|
90
|
+
return (tree) => {
|
|
91
|
+
tablesOf(tree, options.blocks).forEach(({ parent, index }, offset) => {
|
|
92
|
+
const appendix = options.appendices[options.first - 1 + offset];
|
|
93
|
+
if (!appendix || !parent.children)
|
|
94
|
+
return;
|
|
95
|
+
parent.children[index] = {
|
|
96
|
+
type: 'paragraph',
|
|
97
|
+
data: { hName: 'p', hProperties: { className: ['table-reference'] } },
|
|
98
|
+
children: [{ type: 'link', url: `#${appendixId(appendix.number)}`, children: [{ type: 'text', value: options.words(appendix) }] }],
|
|
99
|
+
};
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
}
|
package/dist/diagnose.d.ts
CHANGED
|
@@ -12,9 +12,10 @@
|
|
|
12
12
|
* reads a diagram as the page would draw it, loaded only when a note carries one.
|
|
13
13
|
*/
|
|
14
14
|
import type { NoteHost } from './host.js';
|
|
15
|
+
import { type StyleNoticeCode } from './style-block.js';
|
|
15
16
|
import { type TableNoticeCode, type TableSchema } from './table-block.js';
|
|
16
17
|
/** The codes of §7.13, which `spec.json` declares as `notices`. */
|
|
17
|
-
export type NoticeCode = 'fence-unclosed' | 'math-unclosed' | 'formula-invalid' | 'diagram-invalid' | 'frontmatter-unclosed' | 'name-missing' | 'name-unusable' | 'reserved-not-a-list' | 'aliases-missing' | 'tags-missing' | 'heading-first-level' | 'heading-skipped-level' | 'footnote-undefined' | 'footnote-unreferenced' | 'raw-html' | 'strikethrough-single-tilde' | 'link-pending' | 'embed-pending' | TableNoticeCode;
|
|
18
|
+
export type NoticeCode = 'fence-unclosed' | 'math-unclosed' | 'formula-invalid' | 'diagram-invalid' | 'frontmatter-unclosed' | 'name-missing' | 'name-unusable' | 'reserved-not-a-list' | 'aliases-missing' | 'tags-missing' | 'heading-first-level' | 'heading-skipped-level' | 'footnote-undefined' | 'footnote-unreferenced' | 'raw-html' | 'strikethrough-single-tilde' | 'link-pending' | 'embed-pending' | TableNoticeCode | StyleNoticeCode;
|
|
18
19
|
/** `notice`: the page will differ from what was typed. `hint`: nothing is drawn wrong. */
|
|
19
20
|
export type NoticeSeverity = 'notice' | 'hint';
|
|
20
21
|
/** One place in a note, by a code of §7.13. Lines and columns count from 1 over the whole note. */
|
package/dist/diagnose.js
CHANGED
|
@@ -17,6 +17,7 @@ import katex from 'katex';
|
|
|
17
17
|
import remarkParse from 'remark-parse';
|
|
18
18
|
import { unified } from 'unified';
|
|
19
19
|
import { bodyStart, codeRegions, hideComments, insideCode, toUnixNewlines } from './source.js';
|
|
20
|
+
import { findStyleBlocks } from './style-block.js';
|
|
20
21
|
import { hasParameterReference } from './parameters.js';
|
|
21
22
|
import { readProperties } from './properties.js';
|
|
22
23
|
import { readingPlugins } from './reading.js';
|
|
@@ -47,6 +48,9 @@ export const NOTICES = {
|
|
|
47
48
|
'table-column-unknown': { severity: 'notice', section: '7.15', message: 'The table has no column of this name, so the block says so in place of the table.' },
|
|
48
49
|
'table-filter-invalid': { severity: 'notice', section: '7.15', message: 'This filter cannot be answered, so the block says so in place of the table.' },
|
|
49
50
|
'table-pending': { severity: 'hint', section: '7.15', message: 'The notebook keeps no table of this name yet, so the block says so in its place.' },
|
|
51
|
+
'style-key-unknown': { severity: 'notice', section: '7.16', message: 'This line of a style block is not one of its keys, or one written twice, so it sets nothing.' },
|
|
52
|
+
'style-value-invalid': { severity: 'notice', section: '7.16', message: 'This value is not one its key takes, so it sets nothing.' },
|
|
53
|
+
'style-unclosed': { severity: 'notice', section: '7.16', message: 'This style block is never closed by a :::, so it runs to the end of the note.' },
|
|
50
54
|
};
|
|
51
55
|
/** A footnote reference left as text, which is one with no definition (§7.12). */
|
|
52
56
|
const UNDEFINED_REFERENCE = /\[\^[^\]\s]+\]/g;
|
|
@@ -102,7 +106,12 @@ export async function diagnose(markdown, options = {}) {
|
|
|
102
106
|
frontmatter(text, split, report);
|
|
103
107
|
// A comment is never drawn, so nothing it holds raises a notice; positions are mapped back over it.
|
|
104
108
|
const hidden = hideComments(text.slice(split));
|
|
105
|
-
|
|
109
|
+
// A style block's fences and options are no content (§7.16): they leave the text, every offset
|
|
110
|
+
// kept, so a line of options is no heading and what the block holds is the note's own.
|
|
111
|
+
const styled = findStyleBlocks(hidden.text);
|
|
112
|
+
for (const problem of styled.problems)
|
|
113
|
+
report(problem.code, split + hidden.original(problem.at), split + hidden.original(problem.end, true));
|
|
114
|
+
const body = styled.text;
|
|
106
115
|
const processor = unified().use(remarkParse).use(readingPlugins(() => undefined));
|
|
107
116
|
const parsed = processor.parse(body);
|
|
108
117
|
// The text as the parser found it, before a plugin cut a run of it and dropped its position.
|