@skillre/dsh-plugin-sticky-wall 0.9.1 → 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.
@@ -0,0 +1,586 @@
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 const INDENT_WIDTH = 2;
29
+ /** Leading whitespace, in columns, that starts an indented code block. */
30
+ const CODE_INDENT = 4;
31
+ /**
32
+ * Maximum block nesting depth this parser will descend into.
33
+ *
34
+ * Nesting is recursive (a quote holds blocks, a list item holds blocks), so an
35
+ * adversarial document — 4000 lines each opening one more `>`, which is 200 KB of
36
+ * entirely legal input — would otherwise recurse 4000 frames deep and cost
37
+ * quadratic time re-measuring the same tail at every level. Real notes nest three
38
+ * or four levels. Beyond this cap the remaining lines are kept as literal text,
39
+ * so nothing is lost: the note still renders, just without infinite indentation.
40
+ */
41
+ const MAX_DEPTH = 12;
42
+ /**
43
+ * Upper bound on the steps one parse may take.
44
+ *
45
+ * Not a product limit: it is the backstop that turns a hypothetical parser
46
+ * defect into a truncated render instead of a hung tab. One line can legitimately
47
+ * produce a handful of steps (a quote containing a list containing a quote), so
48
+ * the budget scales with the input rather than being a flat constant.
49
+ */
50
+ function stepBudget(lineCount) {
51
+ return Math.max(1_000, lineCount * 64);
52
+ }
53
+ /** A fence of three or more backticks or tildes, plus an optional info string. */
54
+ const FENCE = /^(`{3,}|~{3,})\s*(.*)$/;
55
+ const HEADING = /^(#{1,6})\s+(.*)$/;
56
+ const RULE = /^(?:[-*_][ \t]*){3,}$/;
57
+ const BULLET = /^([-*+])[ \t]+(.*)$/;
58
+ const ORDERED = /^(\d{1,9})[.)][ \t]+(.*)$/;
59
+ const QUOTE = /^>[ \t]?(.*)$/;
60
+ /** `- [ ] text` / `- [x] text`, matched against a list item's own text. */
61
+ const TASK = /^\[([ xX])\][ \t]+([\s\S]*)$/;
62
+ /** A table divider cell: `---`, `:---`, `---:`, `:---:`. */
63
+ const TABLE_DIVIDER = /^\|?[ \t]*:?-{2,}:?[ \t]*(?:\|[ \t]*:?-{2,}:?[ \t]*)*\|?[ \t]*$/;
64
+ /** Measure leading whitespace in columns and split the line. */
65
+ function toLine(raw) {
66
+ let width = 0;
67
+ let index = 0;
68
+ while (index < raw.length) {
69
+ const char = raw[index];
70
+ if (char === ' ')
71
+ width += 1;
72
+ else if (char === '\t')
73
+ width += INDENT_WIDTH;
74
+ else
75
+ break;
76
+ index += 1;
77
+ }
78
+ return { raw, indent: width, body: raw.slice(index) };
79
+ }
80
+ /** Remove up to `width` columns of leading whitespace. */
81
+ function stripColumns(raw, width) {
82
+ let consumed = 0;
83
+ let index = 0;
84
+ while (index < raw.length && consumed < width) {
85
+ const char = raw[index];
86
+ if (char === ' ') {
87
+ consumed += 1;
88
+ index += 1;
89
+ }
90
+ else if (char === '\t') {
91
+ consumed += INDENT_WIDTH;
92
+ index += 1;
93
+ }
94
+ else
95
+ break;
96
+ }
97
+ return raw.slice(index);
98
+ }
99
+ /** Whether a (whitespace-stripped) line opens a block of its own. */
100
+ function startsBlock(body) {
101
+ return (FENCE.test(body) ||
102
+ HEADING.test(body) ||
103
+ RULE.test(body) ||
104
+ BULLET.test(body) ||
105
+ ORDERED.test(body) ||
106
+ QUOTE.test(body) ||
107
+ TABLE_DIVIDER.test(body));
108
+ }
109
+ /** Split a table row on pipes, dropping the optional leading/trailing pipe. */
110
+ function splitRow(line) {
111
+ const trimmed = line.trim().replace(/^\|/, '').replace(/\|$/, '');
112
+ return trimmed.split('|').map((cell) => cell.trim());
113
+ }
114
+ /** Read a divider row's column alignment. */
115
+ function parseAlign(divider) {
116
+ return splitRow(divider).map((cell) => {
117
+ const left = cell.startsWith(':');
118
+ const right = cell.endsWith(':');
119
+ if (left && right)
120
+ return 'center';
121
+ if (right)
122
+ return 'right';
123
+ if (left)
124
+ return 'left';
125
+ return null;
126
+ });
127
+ }
128
+ /** Whether a table starts at `at`: a pipe row immediately followed by a divider. */
129
+ function tableAt(lines, at, to) {
130
+ if (at + 1 >= to)
131
+ return false;
132
+ const header = lines[at];
133
+ const divider = lines[at + 1];
134
+ if (header === undefined || divider === undefined)
135
+ return false;
136
+ return header.body.includes('|') && TABLE_DIVIDER.test(divider.body);
137
+ }
138
+ /**
139
+ * Parse exactly one block starting at `index`, pushing it onto `blocks` and
140
+ * returning the index to resume at.
141
+ *
142
+ * The contract is that the returned index is ALWAYS greater than `index` for
143
+ * every input this function accepts — that is what makes the caller's loop
144
+ * terminate. Callers must not pass an index whose line is blank; the caller
145
+ * consumes blank lines itself.
146
+ */
147
+ function parseOne(lines, index, to, blocks, depth) {
148
+ const line = lines[index];
149
+ if (line === undefined)
150
+ return index + 1;
151
+ // Nesting cap. Past MAX_DEPTH the remaining lines are emitted as one literal
152
+ // paragraph instead of recursing further: a note that deep is not content a
153
+ // person wrote, and quadratic time on 200 KB of `>>>>>>>>` would freeze the
154
+ // sidebar just as effectively as the old infinite loop.
155
+ if (depth >= MAX_DEPTH) {
156
+ const literal = [];
157
+ let cursor = index;
158
+ while (cursor < to) {
159
+ const candidate = lines[cursor];
160
+ if (candidate === undefined)
161
+ break;
162
+ literal.push(candidate.body);
163
+ cursor += 1;
164
+ }
165
+ const text = literal.join('\n').trim();
166
+ if (text !== '')
167
+ blocks.push({ kind: 'paragraph', text });
168
+ return to;
169
+ }
170
+ // ---- Fenced code --------------------------------------------------------
171
+ const fence = FENCE.exec(line.body);
172
+ if (fence !== null) {
173
+ const marker = fence[1] ?? '```';
174
+ const char = marker.charAt(0);
175
+ // The info string may carry attributes (`js title=x`); the first word is the
176
+ // language. Accepting the whole string as the language was a real defect:
177
+ // the fence stopped being recognised, so the code leaked out as a paragraph.
178
+ const info = (fence[2] ?? '').trim();
179
+ const language = info === '' ? '' : (info.split(/[\s{]/)[0] ?? '');
180
+ const body = [];
181
+ let cursor = index + 1;
182
+ while (cursor < to) {
183
+ const candidate = lines[cursor];
184
+ if (candidate === undefined)
185
+ break;
186
+ const closing = FENCE.exec(candidate.body);
187
+ if (closing !== null) {
188
+ const closingMarker = closing[1] ?? '';
189
+ if (closingMarker.charAt(0) === char && closingMarker.length >= marker.length)
190
+ break;
191
+ }
192
+ // Code indentation is content, so the raw line is used — minus the fence's
193
+ // own indent, so an indented fence keeps its code flush.
194
+ body.push(stripColumns(candidate.raw, line.indent));
195
+ cursor += 1;
196
+ }
197
+ blocks.push({ kind: 'code', text: body.join('\n'), language });
198
+ // An unterminated fence still renders as code; the user is mid-typing.
199
+ return cursor < to ? cursor + 1 : to;
200
+ }
201
+ // ---- Heading ------------------------------------------------------------
202
+ const heading = HEADING.exec(line.body);
203
+ if (heading !== null) {
204
+ const level = Math.min(6, (heading[1] ?? '#').length);
205
+ blocks.push({ kind: 'heading', level, text: (heading[2] ?? '').trim() });
206
+ return index + 1;
207
+ }
208
+ // ---- Horizontal rule ----------------------------------------------------
209
+ if (RULE.test(line.body)) {
210
+ blocks.push({ kind: 'rule' });
211
+ return index + 1;
212
+ }
213
+ // ---- Block quote (nested) ----------------------------------------------
214
+ if (QUOTE.test(line.body)) {
215
+ const inner = [];
216
+ let cursor = index;
217
+ while (cursor < to) {
218
+ const candidate = lines[cursor];
219
+ if (candidate === undefined)
220
+ break;
221
+ const match = QUOTE.exec(candidate.body);
222
+ if (match !== null) {
223
+ // Strip exactly one `>` level and re-measure: `> > x` becomes `> x`, so
224
+ // the recursive call sees the nested quote as a quote.
225
+ inner.push(toLine(match[1] ?? ''));
226
+ cursor += 1;
227
+ continue;
228
+ }
229
+ if (candidate.body === '') {
230
+ // A blank line only keeps the quote open when another quoted line
231
+ // follows; otherwise the quote has ended.
232
+ let probe = cursor;
233
+ while (probe < to && lines[probe]?.body === '')
234
+ probe += 1;
235
+ const next = lines[probe];
236
+ if (next !== undefined && QUOTE.test(next.body)) {
237
+ for (let blank = cursor; blank < probe; blank += 1)
238
+ inner.push(toLine(''));
239
+ cursor = probe;
240
+ continue;
241
+ }
242
+ }
243
+ break;
244
+ }
245
+ blocks.push({ kind: 'quote', children: parseRange(inner, 0, inner.length, depth + 1) });
246
+ // The first line matched QUOTE, so `cursor` is at least index + 1.
247
+ return Math.max(cursor, index + 1);
248
+ }
249
+ // ---- Table --------------------------------------------------------------
250
+ if (tableAt(lines, index, to)) {
251
+ const header = splitRow(line.body);
252
+ const divider = lines[index + 1];
253
+ const align = parseAlign(divider === undefined ? '' : divider.body);
254
+ const rows = [];
255
+ let cursor = index + 2;
256
+ while (cursor < to) {
257
+ const candidate = lines[cursor];
258
+ if (candidate === undefined)
259
+ break;
260
+ if (candidate.body === '' || !candidate.body.includes('|'))
261
+ break;
262
+ // A row that opens another block is not a table row.
263
+ if (startsBlock(candidate.body) && !TABLE_DIVIDER.test(candidate.body))
264
+ break;
265
+ rows.push(splitRow(candidate.body));
266
+ cursor += 1;
267
+ }
268
+ blocks.push({ kind: 'table', header, rows, align });
269
+ return cursor;
270
+ }
271
+ // ---- List (nested) ------------------------------------------------------
272
+ const bullet = BULLET.exec(line.body);
273
+ const ordered = bullet === null ? ORDERED.exec(line.body) : null;
274
+ if (bullet !== null || ordered !== null) {
275
+ const isOrdered = ordered !== null;
276
+ const start = isOrdered ? Number(ordered[1] ?? '1') : 1;
277
+ const baseIndent = line.indent;
278
+ const items = [];
279
+ let cursor = index;
280
+ while (cursor < to) {
281
+ const candidate = lines[cursor];
282
+ if (candidate === undefined)
283
+ break;
284
+ // A sibling item sits at exactly the list's own indent.
285
+ if (candidate.indent !== baseIndent)
286
+ break;
287
+ const marker = isOrdered ? ORDERED.exec(candidate.body) : BULLET.exec(candidate.body);
288
+ if (marker === null)
289
+ break;
290
+ let paragraph = marker[2] ?? '';
291
+ const nested = [];
292
+ /** Whether a blank line has separated the item's own text from its children. */
293
+ let separated = false;
294
+ let probe = cursor + 1;
295
+ while (probe < to) {
296
+ const next = lines[probe];
297
+ if (next === undefined)
298
+ break;
299
+ if (next.body === '') {
300
+ // A blank line ends the item unless indented content follows.
301
+ let ahead = probe;
302
+ while (ahead < to && lines[ahead]?.body === '')
303
+ ahead += 1;
304
+ const resumed = lines[ahead];
305
+ if (resumed !== undefined && resumed.indent >= baseIndent + INDENT_WIDTH) {
306
+ for (let blank = probe; blank < ahead; blank += 1)
307
+ nested.push('');
308
+ separated = true;
309
+ probe = ahead;
310
+ continue;
311
+ }
312
+ break;
313
+ }
314
+ // Anything indented under the marker belongs to this item.
315
+ if (next.indent < baseIndent + INDENT_WIDTH)
316
+ break;
317
+ const deindented = stripColumns(next.raw, baseIndent + INDENT_WIDTH);
318
+ const deLine = toLine(deindented);
319
+ if (!separated && nested.length === 0 && !startsBlock(deLine.body)) {
320
+ // A plain continuation line joins the item's own paragraph, exactly as
321
+ // CommonMark folds it in. Treating it as a separate block is what used
322
+ // to split `- item\n continued` into three blocks.
323
+ paragraph = paragraph === '' ? deLine.body : `${paragraph}\n${deLine.body}`;
324
+ probe += 1;
325
+ continue;
326
+ }
327
+ nested.push(deindented);
328
+ probe += 1;
329
+ }
330
+ items.push({
331
+ text: paragraph,
332
+ children: nested.length > 0 ? parseRange(nested.map(toLine), 0, nested.length, depth + 1) : [],
333
+ });
334
+ cursor = probe;
335
+ // A blank line may separate two items of the SAME list (a "loose" list).
336
+ // Skipping it keeps one list instead of producing one list per item.
337
+ let skip = cursor;
338
+ while (skip < to && lines[skip]?.body === '')
339
+ skip += 1;
340
+ const sibling = lines[skip];
341
+ if (sibling !== undefined && sibling.indent === baseIndent) {
342
+ const siblingMarker = isOrdered ? ORDERED.exec(sibling.body) : BULLET.exec(sibling.body);
343
+ if (siblingMarker !== null) {
344
+ cursor = skip;
345
+ continue;
346
+ }
347
+ }
348
+ break;
349
+ }
350
+ blocks.push({ kind: 'list', ordered: isOrdered, start, items });
351
+ // Each iteration consumed at least its own marker line.
352
+ return Math.max(cursor, index + 1);
353
+ }
354
+ // ---- Indented code ------------------------------------------------------
355
+ // Only at the top of a document or after a blank line, and only outside a
356
+ // list, so indentation that merely aligns text is never mistaken for code.
357
+ const previous = index > 0 ? lines[index - 1] : undefined;
358
+ if (line.indent >= CODE_INDENT && (previous === undefined || previous.body === '')) {
359
+ const body = [];
360
+ let cursor = index;
361
+ let lastContent = index;
362
+ while (cursor < to) {
363
+ const candidate = lines[cursor];
364
+ if (candidate === undefined)
365
+ break;
366
+ if (candidate.body === '') {
367
+ body.push('');
368
+ cursor += 1;
369
+ continue;
370
+ }
371
+ if (candidate.indent < CODE_INDENT)
372
+ break;
373
+ body.push(stripColumns(candidate.raw, CODE_INDENT));
374
+ lastContent = cursor;
375
+ cursor += 1;
376
+ }
377
+ while (body.length > 0 && body[body.length - 1] === '')
378
+ body.pop();
379
+ blocks.push({ kind: 'indentedCode', text: body.join('\n') });
380
+ return lastContent + 1;
381
+ }
382
+ // ---- Paragraph ----------------------------------------------------------
383
+ const body = [line.body];
384
+ let cursor = index + 1;
385
+ while (cursor < to) {
386
+ const next = lines[cursor];
387
+ if (next === undefined)
388
+ break;
389
+ if (next.body === '')
390
+ break;
391
+ if (startsBlock(next.body))
392
+ break;
393
+ // A pipe row that a divider follows is a table header, not prose.
394
+ if (tableAt(lines, cursor, to))
395
+ break;
396
+ body.push(next.body);
397
+ cursor += 1;
398
+ }
399
+ blocks.push({ kind: 'paragraph', text: body.join('\n') });
400
+ return cursor;
401
+ }
402
+ /**
403
+ * Parse a range of lines into blocks.
404
+ *
405
+ * The loop is deliberately defensive: {@link parseOne} must advance, and the
406
+ * `next <= index` guard forces progress even if a future edit breaks that
407
+ * contract. Silently truncating one block is infinitely better than freezing
408
+ * the sidebar, which is what the previous implementation did.
409
+ */
410
+ function parseRange(lines, from, to, depth) {
411
+ const blocks = [];
412
+ let index = from;
413
+ const budget = stepBudget(Math.max(1, to - from));
414
+ for (let step = 0; index < to; step += 1) {
415
+ if (step > budget)
416
+ break;
417
+ const line = lines[index];
418
+ if (line === undefined)
419
+ break;
420
+ if (line.body === '') {
421
+ index += 1;
422
+ continue;
423
+ }
424
+ const next = parseOne(lines, index, to, blocks, depth);
425
+ index = next > index ? next : index + 1;
426
+ }
427
+ return blocks;
428
+ }
429
+ /**
430
+ * Parse a note body into blocks.
431
+ *
432
+ * Exported so the parser is unit-testable without a DOM, and so a future
433
+ * "copy as plain text" action can reuse the same structure.
434
+ */
435
+ export function parseBlocks(source) {
436
+ const text = typeof source === 'string' ? source : '';
437
+ const lines = text.replace(/\r\n?/g, '\n').split('\n').map(toLine);
438
+ return parseRange(lines, 0, lines.length, 0);
439
+ }
440
+ /**
441
+ * Strip inline Markdown syntax while keeping the text it wraps.
442
+ *
443
+ * This is deliberately NOT a second parser. `parseBlocks` already decided what
444
+ * the block IS; this only has to erase the characters that are syntax rather
445
+ * than content, so a digest can never drift from what the card renders. Bare
446
+ * URLs are left alone on purpose: a URL is the one inline construct whose
447
+ * visible text is the whole token, so stripping it would delete information.
448
+ */
449
+ function stripInlineMarkdown(text) {
450
+ let out = text;
451
+ // Backslash escapes first: an escaped marker is literal content, and erasing
452
+ // the backslash later would turn `\*x\*` into emphasis in the digest.
453
+ out = out.replace(/\\([\\`*_{}[\]()#+\-.!>~|])/g, '$1');
454
+ // Images first: their alt text is the closest thing to content they carry.
455
+ out = out.replace(/!\[([^\]]*)\]\([^)\s]*\)/g, '$1');
456
+ // `[label](url)` becomes just the label.
457
+ out = out.replace(/\[([^\]]*)\]\([^)\s]*\)/g, '$1');
458
+ // Code spans: drop the backticks, keep what they wrap.
459
+ out = out.replace(/`+([^`]*)`+/g, '$1');
460
+ // Longest emphasis markers first, so `***x***` never loses only half a pair.
461
+ out = out.replace(/\*\*\*([^*]+)\*\*\*/g, '$1');
462
+ out = out.replace(/\*\*([^*]+)\*\*/g, '$1');
463
+ out = out.replace(/\*([^*]+)\*/g, '$1');
464
+ out = out.replace(/~~([^~]+)~~/g, '$1');
465
+ // Underscore emphasis only between word boundaries: `snake_case_name` is an
466
+ // identifier, not italics, and a digest of a code-heavy note must say so.
467
+ out = out.replace(/(^|[^A-Za-z0-9])___([^_]+)___(?![A-Za-z0-9])/g, '$1$2');
468
+ out = out.replace(/(^|[^A-Za-z0-9])__([^_]+)__(?![A-Za-z0-9])/g, '$1$2');
469
+ out = out.replace(/(^|[^A-Za-z0-9])_([^_]+)_(?![A-Za-z0-9])/g, '$1$2');
470
+ return out;
471
+ }
472
+ /** Collapse every whitespace run to one space and trim both edges. */
473
+ function flattenWhitespace(text) {
474
+ return text.replace(/\s+/g, ' ').trim();
475
+ }
476
+ /**
477
+ * Cut a digest to `limit` characters, appending the ellipsis.
478
+ *
479
+ * The ellipsis sits OUTSIDE the budget so `max` always means "characters of the
480
+ * note that survive", which is what a CSS-clamped row needs. The trailing space
481
+ * of a break-at-a-space cut is dropped so the result never reads "word …".
482
+ */
483
+ function truncateDigest(text, limit) {
484
+ if (text.length <= limit)
485
+ return text;
486
+ let cut = limit;
487
+ // `slice` counts UTF-16 units, so a cut can land between the two halves of an
488
+ // emoji and leave a lone surrogate that renders as a replacement glyph. Back
489
+ // off one unit when the boundary splits a pair.
490
+ const code = text.charCodeAt(cut - 1);
491
+ if (code >= 0xd800 && code <= 0xdbff)
492
+ cut -= 1;
493
+ return `${text.slice(0, cut).replace(/\s+$/, '')}…`;
494
+ }
495
+ /** The first visible text inside a block, or `''` when it carries none. */
496
+ function firstVisibleText(block) {
497
+ switch (block.kind) {
498
+ case 'rule':
499
+ return '';
500
+ case 'code':
501
+ case 'indentedCode':
502
+ // Code that opens a note is usually the point of the note, and one line is
503
+ // all a card or a row can honestly show.
504
+ return block.text.split('\n')[0] ?? '';
505
+ case 'table':
506
+ return block.header.join(' ');
507
+ case 'quote': {
508
+ for (const child of block.children) {
509
+ const text = firstVisibleText(child);
510
+ if (text !== '')
511
+ return text;
512
+ }
513
+ return '';
514
+ }
515
+ case 'list': {
516
+ for (const item of block.items) {
517
+ const own = item.text.replace(/^\[[ xX]\]\s*/, '');
518
+ if (own !== '')
519
+ return own;
520
+ // An item with only nested content still describes the note.
521
+ for (const child of item.children) {
522
+ const text = firstVisibleText(child);
523
+ if (text !== '')
524
+ return text;
525
+ }
526
+ }
527
+ return '';
528
+ }
529
+ case 'heading':
530
+ case 'paragraph':
531
+ default:
532
+ return block.text;
533
+ }
534
+ }
535
+ /**
536
+ * One plain-text digest of a note, for lists, aria-labels and search.
537
+ * Strips Markdown syntax: no `##`, no `**`, no `- [ ]`, no ``` fences, no pipes.
538
+ *
539
+ * The FIRST block that yields visible text wins, because a digest is one line:
540
+ * a rule carries none, and an empty heading or paragraph is what a half-typed
541
+ * note looks like. Picking the first *visible* block means a note that opens
542
+ * with `---` still summarises its actual first line instead of returning ''.
543
+ */
544
+ export function plainTextSummary(source, max = 90) {
545
+ // A caller asking for zero characters gets nothing: returning the ellipsis
546
+ // alone would claim the note has content it does not.
547
+ const limit = Number.isFinite(max) ? Math.floor(max) : 0;
548
+ if (limit <= 0)
549
+ return '';
550
+ const raw = typeof source === 'string' ? source : '';
551
+ let blocks;
552
+ try {
553
+ blocks = parseBlocks(raw);
554
+ }
555
+ catch {
556
+ // A digest is decoration on a list row, so it must never be the thing that
557
+ // breaks a render: degrade to the raw text rather than throwing.
558
+ return truncateDigest(flattenWhitespace(stripInlineMarkdown(raw)), limit);
559
+ }
560
+ for (const block of blocks) {
561
+ const candidate = firstVisibleText(block);
562
+ const digest = flattenWhitespace(stripInlineMarkdown(candidate));
563
+ // An empty heading/paragraph (or an empty first list item) has no visible
564
+ // text, so the search continues instead of stopping on ''.
565
+ if (digest !== '')
566
+ return truncateDigest(digest, limit);
567
+ }
568
+ return '';
569
+ }
570
+ /**
571
+ * Normalize a link target.
572
+ *
573
+ * Only http(s) and mailto survive. A `javascript:` or `data:` URL degrades to
574
+ * plain text, so a note pasted from somewhere hostile cannot become a live
575
+ * script URL in the shell's DOM.
576
+ */
577
+ export function safeHref(raw) {
578
+ const value = raw.trim();
579
+ if (value === '')
580
+ return undefined;
581
+ if (/^(?:https?|mailto):/i.test(value))
582
+ return value;
583
+ if (/^www\./i.test(value))
584
+ return `https://${value}`;
585
+ return undefined;
586
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@skillre/dsh-plugin-sticky-wall",
3
- "version": "0.9.1",
3
+ "version": "0.9.2",
4
4
  "description": "便签墙:DSH 侧栏的自由画布便签板,可拖动摆放、一键整理,正文按 Markdown 渲染",
5
5
  "icon": "./icon.svg",
6
6
  "type": "module",