@skillre/dsh-plugin-sticky-wall 0.8.2 → 0.9.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.en.md +6 -4
- package/README.md +10 -6
- package/compatibility.json +359 -1
- package/lib/client.js +1272 -396
- package/lib/layout.js +94 -8
- package/lib/markdown-parse.d.ts +93 -0
- package/lib/markdown-parse.js +586 -0
- package/package.json +1 -1
package/lib/layout.js
CHANGED
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
* value the Host persists and the value the Client previews.
|
|
22
22
|
*/
|
|
23
23
|
import { CENTRE_RESERVE, LAYOUT_LIMITS, WIDTHS, WIDTH_PX, } from './types.js';
|
|
24
|
+
import { parseBlocks } from './markdown-parse.js';
|
|
24
25
|
/** Golden angle: the phyllotaxis constant that keeps a spiral from lining up. */
|
|
25
26
|
const GOLDEN_ANGLE = Math.PI * (3 - Math.sqrt(5));
|
|
26
27
|
/** Breathing room kept between two tidied cards, in world pixels. */
|
|
@@ -147,14 +148,7 @@ function columns(text) {
|
|
|
147
148
|
*/
|
|
148
149
|
export function estimateHeight(note, width) {
|
|
149
150
|
const columnsPerLine = columnsPerLineFor(width);
|
|
150
|
-
let lines =
|
|
151
|
-
for (const paragraph of note.body.split('\n')) {
|
|
152
|
-
if (paragraph.trim() === '') {
|
|
153
|
-
lines += 0.4;
|
|
154
|
-
continue;
|
|
155
|
-
}
|
|
156
|
-
lines += Math.max(1, Math.ceil(columns(paragraph.replace(/^[#>*\-+\d. ]+/u, '')) / columnsPerLine));
|
|
157
|
-
}
|
|
151
|
+
let lines = bodyLines(note.body, columnsPerLine);
|
|
158
152
|
// The title wraps like any other text, and it is capped at LIMITS.title (120)
|
|
159
153
|
// characters — which is a dozen lines at the small preset, not the flat 1.4
|
|
160
154
|
// this used to charge. Under-counting it was a real overlap bug: a 120-char
|
|
@@ -177,6 +171,98 @@ export function estimateHeight(note, width) {
|
|
|
177
171
|
const estimated = Math.round(58 + clamped * 19);
|
|
178
172
|
return Math.min(estimated, MAX_CARD_HEIGHT[width]);
|
|
179
173
|
}
|
|
174
|
+
/**
|
|
175
|
+
* Lines a note body occupies, measured on the SAME structure the Client renders.
|
|
176
|
+
*
|
|
177
|
+
* The previous version split the source on newlines and stripped a marker
|
|
178
|
+
* prefix with a regex. That was wrong in three ways that all pushed the estimate
|
|
179
|
+
* BELOW the rendered height, which is what makes a tidied wall overlap:
|
|
180
|
+
*
|
|
181
|
+
* - a nested list renders as several `<ul>`s with their own margins, but the
|
|
182
|
+
* source is just more lines of the same text;
|
|
183
|
+
* - a fenced code block renders with padding, a border and a fixed 1.45 line
|
|
184
|
+
* height, none of which a source line count sees;
|
|
185
|
+
* - a table renders as rows with cell padding, not as `| a | b |` text.
|
|
186
|
+
*
|
|
187
|
+
* Using the real parser is what keeps the estimate honest. `estimateHeight` runs
|
|
188
|
+
* on the Host, which is why the parser lives in a React-free module.
|
|
189
|
+
*/
|
|
190
|
+
function bodyLines(body, columnsPerLine) {
|
|
191
|
+
let lines = 0;
|
|
192
|
+
for (const block of parseBlocks(body)) {
|
|
193
|
+
lines += blockLines(block, columnsPerLine);
|
|
194
|
+
}
|
|
195
|
+
return lines;
|
|
196
|
+
}
|
|
197
|
+
/** Lines one block occupies, including the block spacing around it. */
|
|
198
|
+
function blockLines(block, columnsPerLine) {
|
|
199
|
+
switch (block.kind) {
|
|
200
|
+
case 'rule':
|
|
201
|
+
// A hairline plus its vertical margins, rounded to a line budget.
|
|
202
|
+
return 1.2;
|
|
203
|
+
case 'code':
|
|
204
|
+
case 'indentedCode': {
|
|
205
|
+
// Code does not wrap (it scrolls), so the count is its own line count, plus
|
|
206
|
+
// the block's padding and border — about one extra line of chrome.
|
|
207
|
+
const count = block.text === '' ? 1 : block.text.split('\n').length;
|
|
208
|
+
return count * 0.95 + 1.3;
|
|
209
|
+
}
|
|
210
|
+
case 'table': {
|
|
211
|
+
// Header + body rows, each with cell padding; a table is taller per row
|
|
212
|
+
// than prose because of the borders.
|
|
213
|
+
const rows = block.rows.length + 1;
|
|
214
|
+
return rows * 1.25 + 0.6;
|
|
215
|
+
}
|
|
216
|
+
case 'heading':
|
|
217
|
+
// One line plus its own top/bottom margins.
|
|
218
|
+
return Math.max(1, Math.ceil(columns(block.text) / columnsPerLine)) + 0.55;
|
|
219
|
+
case 'quote':
|
|
220
|
+
case 'paragraph':
|
|
221
|
+
return paragraphLines(block.kind === 'quote' ? blockText(block) : block.text, columnsPerLine, 0) +
|
|
222
|
+
(block.kind === 'quote' ? 0.5 : 0.5);
|
|
223
|
+
case 'list': {
|
|
224
|
+
let lines = 0.5;
|
|
225
|
+
for (const item of block.items) {
|
|
226
|
+
// The marker eats horizontal room, and a nested list is indented further.
|
|
227
|
+
const marker = 2;
|
|
228
|
+
lines += paragraphLines(item.text, Math.max(8, columnsPerLine - marker), 0);
|
|
229
|
+
for (const child of item.children)
|
|
230
|
+
lines += blockLines(child, Math.max(8, columnsPerLine - 2));
|
|
231
|
+
lines += 0.12;
|
|
232
|
+
}
|
|
233
|
+
return lines;
|
|
234
|
+
}
|
|
235
|
+
default:
|
|
236
|
+
return 1;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
/** The plain text of a quote, flattened so its wrapping can be estimated. */
|
|
240
|
+
function blockText(block) {
|
|
241
|
+
const parts = [];
|
|
242
|
+
for (const child of block.kind === 'quote' ? block.children : []) {
|
|
243
|
+
if (child.kind === 'paragraph' || child.kind === 'heading')
|
|
244
|
+
parts.push(child.text);
|
|
245
|
+
else if (child.kind === 'list')
|
|
246
|
+
parts.push(child.items.map((item) => item.text).join(' '));
|
|
247
|
+
else if (child.kind === 'code' || child.kind === 'indentedCode')
|
|
248
|
+
parts.push(child.text);
|
|
249
|
+
else if (child.kind === 'table')
|
|
250
|
+
parts.push(child.header.join(' '));
|
|
251
|
+
}
|
|
252
|
+
return parts.join('\n');
|
|
253
|
+
}
|
|
254
|
+
/** Lines one run of text occupies at a given column budget. */
|
|
255
|
+
function paragraphLines(text, columnsPerLine, extra) {
|
|
256
|
+
let lines = extra;
|
|
257
|
+
for (const part of text.split('\n')) {
|
|
258
|
+
if (part.trim() === '') {
|
|
259
|
+
lines += 0.4;
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
lines += Math.max(1, Math.ceil(columns(part) / columnsPerLine));
|
|
263
|
+
}
|
|
264
|
+
return lines;
|
|
265
|
+
}
|
|
180
266
|
/** Columns of text that fit on one line of a card at the given width. */
|
|
181
267
|
function columnsPerLineFor(width, density = 1) {
|
|
182
268
|
// 7.1px per column at the card's 13px type size, minus the horizontal padding.
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Markdown block and inline parser, shared by both halves of the plugin.
|
|
3
|
+
*
|
|
4
|
+
* This module is deliberately React-free and DOM-free: the Client renders what
|
|
5
|
+
* it produces, and the Host's `estimateHeight` (src/layout.ts) walks the same
|
|
6
|
+
* structure to predict a card's rendered height. Keeping ONE parser is what
|
|
7
|
+
* makes the estimate and the render agree — a second, string-based guess is
|
|
8
|
+
* exactly how a tidied wall ends up with overlapping cards.
|
|
9
|
+
*
|
|
10
|
+
* The supported subset is a sticky note's, not a document's: ATX headings,
|
|
11
|
+
* paragraphs, emphasis/code/strike, fenced and indented code, nested ordered and
|
|
12
|
+
* unordered lists with `- [ ]` checkboxes, nested block quotes, rules, pipe
|
|
13
|
+
* tables with column alignment, and http(s)/mailto links.
|
|
14
|
+
*
|
|
15
|
+
* ## The two invariants that keep this parser honest
|
|
16
|
+
*
|
|
17
|
+
* 1. **Indentation is structure, never noise.** Every block marker is matched
|
|
18
|
+
* against the line with its leading whitespace removed, so ` > quote` and
|
|
19
|
+
* `> quote` are the same construct. Matching the raw line for one and the
|
|
20
|
+
* trimmed line for the other is what used to make this parser loop forever on
|
|
21
|
+
* an indented quote.
|
|
22
|
+
* 2. **Every step advances.** {@link parseOne} returns the index to resume at,
|
|
23
|
+
* and {@link parseRange} forces `index + 1` if a step ever returns a
|
|
24
|
+
* non-advancing index. A render loop that stops advancing is not a visible
|
|
25
|
+
* bug on a sticky note: it is a frozen sidebar.
|
|
26
|
+
*/
|
|
27
|
+
/** Column width of one nesting level. The editor's Tab inserts exactly this. */
|
|
28
|
+
export declare const INDENT_WIDTH = 2;
|
|
29
|
+
/** One table cell's horizontal alignment, as declared by the divider row. */
|
|
30
|
+
export type TableAlign = 'left' | 'center' | 'right' | null;
|
|
31
|
+
/** One list item: its own text plus any blocks nested under it. */
|
|
32
|
+
export interface ListItem {
|
|
33
|
+
/** The item's text, without its marker. May contain `\n` for continuation lines. */
|
|
34
|
+
text: string;
|
|
35
|
+
/** Nested blocks: a deeper list, a paragraph, a code block, another quote. */
|
|
36
|
+
children: Block[];
|
|
37
|
+
}
|
|
38
|
+
/** One parsed block of a note body. */
|
|
39
|
+
export type Block = {
|
|
40
|
+
kind: 'heading';
|
|
41
|
+
level: 1 | 2 | 3 | 4 | 5 | 6;
|
|
42
|
+
text: string;
|
|
43
|
+
} | {
|
|
44
|
+
kind: 'paragraph';
|
|
45
|
+
text: string;
|
|
46
|
+
} | {
|
|
47
|
+
kind: 'code';
|
|
48
|
+
text: string;
|
|
49
|
+
language: string;
|
|
50
|
+
} | {
|
|
51
|
+
kind: 'indentedCode';
|
|
52
|
+
text: string;
|
|
53
|
+
} | {
|
|
54
|
+
kind: 'quote';
|
|
55
|
+
children: Block[];
|
|
56
|
+
} | {
|
|
57
|
+
kind: 'list';
|
|
58
|
+
ordered: boolean;
|
|
59
|
+
start: number;
|
|
60
|
+
items: ListItem[];
|
|
61
|
+
} | {
|
|
62
|
+
kind: 'rule';
|
|
63
|
+
} | {
|
|
64
|
+
kind: 'table';
|
|
65
|
+
header: string[];
|
|
66
|
+
rows: string[][];
|
|
67
|
+
align: TableAlign[];
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Parse a note body into blocks.
|
|
71
|
+
*
|
|
72
|
+
* Exported so the parser is unit-testable without a DOM, and so a future
|
|
73
|
+
* "copy as plain text" action can reuse the same structure.
|
|
74
|
+
*/
|
|
75
|
+
export declare function parseBlocks(source: string): Block[];
|
|
76
|
+
/**
|
|
77
|
+
* One plain-text digest of a note, for lists, aria-labels and search.
|
|
78
|
+
* Strips Markdown syntax: no `##`, no `**`, no `- [ ]`, no ``` fences, no pipes.
|
|
79
|
+
*
|
|
80
|
+
* The FIRST block that yields visible text wins, because a digest is one line:
|
|
81
|
+
* a rule carries none, and an empty heading or paragraph is what a half-typed
|
|
82
|
+
* note looks like. Picking the first *visible* block means a note that opens
|
|
83
|
+
* with `---` still summarises its actual first line instead of returning ''.
|
|
84
|
+
*/
|
|
85
|
+
export declare function plainTextSummary(source: string, max?: number): string;
|
|
86
|
+
/**
|
|
87
|
+
* Normalize a link target.
|
|
88
|
+
*
|
|
89
|
+
* Only http(s) and mailto survive. A `javascript:` or `data:` URL degrades to
|
|
90
|
+
* plain text, so a note pasted from somewhere hostile cannot become a live
|
|
91
|
+
* script URL in the shell's DOM.
|
|
92
|
+
*/
|
|
93
|
+
export declare function safeHref(raw: string): string | undefined;
|