@tooee/renderers 0.1.22 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/dist/CodeView.d.ts +11 -11
  2. package/dist/CodeView.d.ts.map +1 -1
  3. package/dist/CodeView.js +2 -20
  4. package/dist/CodeView.js.map +1 -1
  5. package/dist/DocumentBindings.d.ts +16 -0
  6. package/dist/DocumentBindings.d.ts.map +1 -0
  7. package/dist/DocumentBindings.js +2 -0
  8. package/dist/DocumentBindings.js.map +1 -0
  9. package/dist/MarkdownView.d.ts +23 -28
  10. package/dist/MarkdownView.d.ts.map +1 -1
  11. package/dist/MarkdownView.js +4 -110
  12. package/dist/MarkdownView.js.map +1 -1
  13. package/dist/RowDocumentRenderable.d.ts +2 -0
  14. package/dist/RowDocumentRenderable.d.ts.map +1 -1
  15. package/dist/RowDocumentRenderable.js +11 -0
  16. package/dist/RowDocumentRenderable.js.map +1 -1
  17. package/dist/Table.d.ts +9 -10
  18. package/dist/Table.d.ts.map +1 -1
  19. package/dist/Table.js +3 -20
  20. package/dist/Table.js.map +1 -1
  21. package/dist/index.d.ts +6 -2
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +3 -1
  24. package/dist/index.js.map +1 -1
  25. package/dist/markdown-blocks.d.ts +41 -0
  26. package/dist/markdown-blocks.d.ts.map +1 -0
  27. package/dist/markdown-blocks.js +246 -0
  28. package/dist/markdown-blocks.js.map +1 -0
  29. package/dist/source.d.ts +83 -0
  30. package/dist/source.d.ts.map +1 -0
  31. package/dist/source.js +114 -0
  32. package/dist/source.js.map +1 -0
  33. package/package.json +15 -9
  34. package/src/CodeView.tsx +15 -41
  35. package/src/DocumentBindings.ts +16 -0
  36. package/src/MarkdownView.tsx +33 -150
  37. package/src/RowDocumentRenderable.ts +10 -0
  38. package/src/Table.tsx +13 -33
  39. package/src/index.ts +12 -2
  40. package/src/markdown-blocks.ts +296 -0
  41. package/src/source.ts +177 -0
@@ -0,0 +1,296 @@
1
+ import { marked, type Token, type Tokens } from "marked"
2
+ import { SourceIndex, type DocumentRowSource } from "./source.js"
3
+
4
+ // ---------------------------------------------------------------------------
5
+ // Flat block model
6
+ // ---------------------------------------------------------------------------
7
+
8
+ /**
9
+ * A flattened block — all blocks exist at the top level with an indent.
10
+ * Nested structures (lists containing code blocks, etc.) are flattened into
11
+ * sibling blocks with appropriate indentation.
12
+ */
13
+ export interface FlatBlock {
14
+ token: Token
15
+ indent: number
16
+ bullet?: string // "- " or "1. " for list item lines
17
+ checked?: boolean // undefined = not a checkbox, true/false = checkbox state
18
+
19
+ /** Provenance in the Markdown input; `null` only if resolution genuinely failed. */
20
+ source: DocumentRowSource | null
21
+ }
22
+
23
+ export interface FlattenMarkdownOptions {
24
+ sourceId?: string
25
+ }
26
+
27
+ /** Empty synthetic token stamped onto bullet-only rows (nested-list/block/empty items). */
28
+ function syntheticTextToken(): Token {
29
+ return { type: "text", raw: "", text: "", tokens: [] } as unknown as Token
30
+ }
31
+
32
+ /**
33
+ * The visible/searchable text for a block. Non-synthetic blocks use the token's
34
+ * `raw`; synthetic bullet rows (empty raw) fall back to the visible
35
+ * bullet/checkbox text, then to the resolved source text. This keeps default
36
+ * search and copy in agreement with the source mapping on synthetic rows.
37
+ */
38
+ export function getFlatBlockText(block: FlatBlock): string {
39
+ const raw = "raw" in block.token && typeof block.token.raw === "string" ? block.token.raw : ""
40
+ if (raw.length > 0) return raw
41
+ if (block.bullet !== undefined) {
42
+ const checkbox = block.checked !== undefined ? (block.checked ? "[x] " : "[ ] ") : ""
43
+ return block.bullet + checkbox
44
+ }
45
+ return block.source?.primary.text ?? ""
46
+ }
47
+
48
+ // ---------------------------------------------------------------------------
49
+ // Markdown provenance resolver
50
+ // ---------------------------------------------------------------------------
51
+
52
+ /**
53
+ * Deterministic sequential source resolution. Marked exposes `raw` strings but
54
+ * no positions, so we locate each token's exact slice at or after a monotonic
55
+ * cursor, constrained to its parent list-item span. Advancing the cursor past
56
+ * every match is what makes repeated identical blocks resolve to distinct
57
+ * occurrences instead of backtracking to an earlier one.
58
+ */
59
+ class MarkdownResolver {
60
+ readonly index: SourceIndex
61
+ cursor = 0
62
+
63
+ constructor(
64
+ readonly markdown: string,
65
+ sourceId?: string,
66
+ ) {
67
+ this.index = new SourceIndex(markdown, sourceId)
68
+ }
69
+
70
+ /** Resolve `raw` at/after the cursor within `[cursor, bound]`, then advance. */
71
+ resolveRaw(raw: string, bound: number): DocumentRowSource | null {
72
+ const match = this.find(raw, bound)
73
+ if (!match) return null
74
+ this.cursor = match.end
75
+ return { primary: this.index.span(match.start, match.end) }
76
+ }
77
+
78
+ /** Find `raw`'s offset range without advancing the cursor. */
79
+ locate(raw: string, bound: number): { start: number; end: number } | null {
80
+ return this.find(raw, bound)
81
+ }
82
+
83
+ /**
84
+ * Locate `raw` at or after the cursor within `[cursor, bound]`. marked
85
+ * normalizes `\r\n` to `\n` inside multi-line token `raw`, so the exact slice
86
+ * can be absent from a CRLF source. When the exact scan fails we retry a
87
+ * newline-tolerant match (a needle `\n` matches a source `\r\n` or `\n`) so
88
+ * offsets keep addressing the original `\r\n` string without normalizing it.
89
+ */
90
+ private find(raw: string, bound: number): { start: number; end: number } | null {
91
+ if (raw.length === 0) return null
92
+ const md = this.markdown
93
+
94
+ const exact = md.indexOf(raw, this.cursor)
95
+ if (exact !== -1 && exact + raw.length <= bound)
96
+ return { start: exact, end: exact + raw.length }
97
+
98
+ if (!raw.includes("\n")) return null
99
+
100
+ const firstSegment = raw.slice(0, raw.indexOf("\n"))
101
+ if (firstSegment.length === 0) return null
102
+
103
+ let from = this.cursor
104
+ for (;;) {
105
+ const start = md.indexOf(firstSegment, from)
106
+ if (start === -1 || start >= bound) return null
107
+ const end = this.matchFlexibleNewlines(raw, start, bound)
108
+ if (end !== -1) return { start, end }
109
+ from = start + 1
110
+ }
111
+ }
112
+
113
+ /** Match `raw` from `start`, letting a needle `\n` consume a source `\r\n`. */
114
+ private matchFlexibleNewlines(raw: string, start: number, bound: number): number {
115
+ const md = this.markdown
116
+ const length = md.length
117
+ let h = start
118
+ let n = 0
119
+ while (n < raw.length) {
120
+ const code = raw.charCodeAt(n)
121
+ if (code === 10 /* \n */) {
122
+ if (h + 1 < length && md.charCodeAt(h) === 13 && md.charCodeAt(h + 1) === 10) {
123
+ h += 2
124
+ } else if (h < length && md.charCodeAt(h) === 10) {
125
+ h += 1
126
+ } else {
127
+ return -1
128
+ }
129
+ n += 1
130
+ } else {
131
+ if (h < length && md.charCodeAt(h) === code) {
132
+ h += 1
133
+ n += 1
134
+ } else {
135
+ return -1
136
+ }
137
+ }
138
+ }
139
+ return h <= bound ? h : -1
140
+ }
141
+
142
+ /** Zero-consumption marker span for a synthetic bullet on the item's first line. */
143
+ markerSource(itemStart: number, itemEnd: number): DocumentRowSource {
144
+ const nl = this.markdown.indexOf("\n", itemStart)
145
+ const lineEnd = nl === -1 ? itemEnd : Math.min(nl, itemEnd)
146
+ const firstLine = this.markdown.slice(itemStart, lineEnd)
147
+ const match = firstLine.match(/^(\s*)(?:[-*+]|\d+[.)])[ \t]*(?:\[[ xX]\])?/)
148
+ if (!match) return { primary: this.index.span(itemStart, itemStart, false) }
149
+ const markerStart = itemStart + match[1]!.length
150
+ let spanEnd = itemStart + match[0].length
151
+ while (spanEnd > markerStart) {
152
+ const code = this.markdown.charCodeAt(spanEnd - 1)
153
+ if (code !== 32 && code !== 9) break // space / tab
154
+ spanEnd--
155
+ }
156
+ return { primary: this.index.span(markerStart, spanEnd, false) }
157
+ }
158
+ }
159
+
160
+ // ---------------------------------------------------------------------------
161
+ // Flattening (shared by mapped and unmapped forms)
162
+ // ---------------------------------------------------------------------------
163
+
164
+ function bulletFor(list: Tokens.List, itemIndex: number): string {
165
+ return list.ordered ? `${itemIndex + (list.start || 1)}. ` : "- "
166
+ }
167
+
168
+ function flattenList(
169
+ list: Tokens.List,
170
+ indent: number,
171
+ out: FlatBlock[],
172
+ res: MarkdownResolver | null,
173
+ bound: number,
174
+ ): void {
175
+ for (let i = 0; i < list.items.length; i++) {
176
+ const item = list.items[i]!
177
+ const bullet = bulletFor(list, i)
178
+
179
+ let itemStart: number | null = null
180
+ let itemEnd = bound
181
+ if (res) {
182
+ const loc = res.locate(item.raw, bound)
183
+ if (loc) {
184
+ itemStart = loc.start
185
+ itemEnd = loc.end
186
+ res.cursor = loc.start
187
+ }
188
+ }
189
+
190
+ flattenListItem(item, indent, bullet, out, res, itemStart, itemEnd)
191
+
192
+ if (res && itemStart !== null) res.cursor = itemEnd
193
+ }
194
+ }
195
+
196
+ function flattenListItem(
197
+ item: Tokens.ListItem,
198
+ indent: number,
199
+ bullet: string,
200
+ out: FlatBlock[],
201
+ res: MarkdownResolver | null,
202
+ itemStart: number | null,
203
+ itemEnd: number,
204
+ ): void {
205
+ const checked = item.checked != null ? item.checked : undefined
206
+ const childTokens = item.tokens || []
207
+ let bulletUsed = false
208
+
209
+ const emitBulletMarker = () => {
210
+ out.push({
211
+ token: syntheticTextToken(),
212
+ indent,
213
+ bullet,
214
+ checked,
215
+ source: res && itemStart !== null ? res.markerSource(itemStart, itemEnd) : null,
216
+ })
217
+ bulletUsed = true
218
+ }
219
+
220
+ for (const token of childTokens) {
221
+ if (token.type === "space" || token.type === "checkbox") continue
222
+
223
+ if (token.type === "text" || token.type === "paragraph") {
224
+ // Inline content — attach the bullet to the first one.
225
+ out.push({
226
+ token,
227
+ indent,
228
+ bullet: bulletUsed ? undefined : bullet,
229
+ checked: bulletUsed ? undefined : checked,
230
+ source: res ? res.resolveRaw(token.raw ?? "", itemEnd) : null,
231
+ })
232
+ bulletUsed = true
233
+ } else if (token.type === "list") {
234
+ if (!bulletUsed) emitBulletMarker()
235
+ flattenList(token as Tokens.List, indent + bullet.length, out, res, itemEnd)
236
+ } else {
237
+ // Block content (code, table, blockquote, hr, etc.).
238
+ if (!bulletUsed) emitBulletMarker()
239
+ out.push({
240
+ token,
241
+ indent: indent + bullet.length,
242
+ source: res ? res.resolveRaw(token.raw ?? "", itemEnd) : null,
243
+ })
244
+ }
245
+ }
246
+
247
+ // List item had no content tokens — still emit the bullet.
248
+ if (!bulletUsed) emitBulletMarker()
249
+ }
250
+
251
+ function flattenWalk(
252
+ tokens: Token[],
253
+ indent: number,
254
+ out: FlatBlock[],
255
+ res: MarkdownResolver | null,
256
+ bound: number,
257
+ ): void {
258
+ for (const token of tokens) {
259
+ if (token.type === "space") continue
260
+ if (token.type === "list") {
261
+ flattenList(token as Tokens.List, indent, out, res, bound)
262
+ } else {
263
+ out.push({ token, indent, source: res ? res.resolveRaw(token.raw ?? "", bound) : null })
264
+ }
265
+ }
266
+ }
267
+
268
+ // ---------------------------------------------------------------------------
269
+ // Public entry points
270
+ // ---------------------------------------------------------------------------
271
+
272
+ /**
273
+ * Lex, flatten, and source-map Markdown in one operation. Every returned block
274
+ * carries a `source` anchor (or `null` when a marked edge case leaves a token
275
+ * genuinely unresolvable), in the exact order used for navigation/rendering.
276
+ */
277
+ export function flattenMarkdown(markdown: string, options?: FlattenMarkdownOptions): FlatBlock[] {
278
+ const res = new MarkdownResolver(markdown, options?.sourceId)
279
+ const out: FlatBlock[] = []
280
+ flattenWalk(marked.lexer(markdown), 0, out, res, markdown.length)
281
+ return out
282
+ }
283
+
284
+ /**
285
+ * The unmapped low-level form: flatten already-lexed tokens with no source
286
+ * provenance (every block's `source` is `null`).
287
+ *
288
+ * @deprecated Prefer `flattenMarkdown(source)` for source-backed rows. Tokens
289
+ * carry no positions and the original source is unavailable here, so offsets
290
+ * cannot be reconstructed.
291
+ */
292
+ export function flattenTokens(tokens: Token[]): FlatBlock[] {
293
+ const out: FlatBlock[] = []
294
+ flattenWalk(tokens, 0, out, null, 0)
295
+ return out
296
+ }
package/src/source.ts ADDED
@@ -0,0 +1,177 @@
1
+ import type { Key } from "react"
2
+
3
+ // ---------------------------------------------------------------------------
4
+ // Source-coordinate types
5
+ // ---------------------------------------------------------------------------
6
+
7
+ /** A point in the original source. Offsets and columns are UTF-16 code-unit indices. */
8
+ export interface SourcePoint {
9
+ offset: number
10
+ line: number
11
+ column: number
12
+ }
13
+
14
+ /**
15
+ * A half-open source range: `[start, end)`.
16
+ *
17
+ * `start`/`end` carry the line/column of each boundary, while `lastLine` is the
18
+ * inclusive physical line the anchor actually touches. `lastLine` avoids the
19
+ * common error where a range ending at column 0 after a newline appears to
20
+ * cover the following line.
21
+ */
22
+ export interface SourceSpan {
23
+ sourceId?: string
24
+ start: SourcePoint
25
+ end: SourcePoint
26
+ lastLine: number
27
+
28
+ /** Exact original substring in `[start.offset, end.offset)`. */
29
+ text: string
30
+
31
+ /** Complete physical source lines touched by the span, without newline normalization. */
32
+ lineText: string
33
+ }
34
+
35
+ /** A rendered row can be synthetic, have one source, or relate two/more sources. */
36
+ export interface DocumentRowSource {
37
+ primary: SourceSpan
38
+ related?: readonly SourceSpan[]
39
+ }
40
+
41
+ /** The live anchor a document controller resolves for a rendered/navigation row. */
42
+ export interface DocumentRowAnchor<T> {
43
+ row: T
44
+ index: number
45
+ key: Key
46
+
47
+ /** The same semantic text used by default search and copy. */
48
+ text: string
49
+
50
+ /** `null` for generated rows with no meaningful source coordinate. */
51
+ source: DocumentRowSource | null
52
+ }
53
+
54
+ // ---------------------------------------------------------------------------
55
+ // Line index and span construction
56
+ // ---------------------------------------------------------------------------
57
+
58
+ /** Offsets of the first character of every physical line. */
59
+ function buildLineStarts(text: string): number[] {
60
+ const starts = [0]
61
+ for (let i = 0; i < text.length; i++) {
62
+ if (text.charCodeAt(i) === 10 /* \n */) starts.push(i + 1)
63
+ }
64
+ return starts
65
+ }
66
+
67
+ /**
68
+ * A CRLF-aware line-start index over an original source string. A `\r\n` is one
69
+ * line break (the break lands on the `\n`); offsets still address the original
70
+ * `\r\n` string, so the `\r` counts as a column on the preceding line.
71
+ */
72
+ export class SourceIndex {
73
+ readonly lineStarts: number[]
74
+
75
+ constructor(
76
+ readonly text: string,
77
+ readonly sourceId?: string,
78
+ ) {
79
+ this.lineStarts = buildLineStarts(text)
80
+ }
81
+
82
+ /** The physical line index (0-based) containing `offset`, via binary search. */
83
+ lineAt(offset: number): number {
84
+ const starts = this.lineStarts
85
+ let lo = 0
86
+ let hi = starts.length - 1
87
+ while (lo < hi) {
88
+ const mid = (lo + hi + 1) >> 1
89
+ if (starts[mid]! <= offset) lo = mid
90
+ else hi = mid - 1
91
+ }
92
+ return lo
93
+ }
94
+
95
+ point(offset: number): SourcePoint {
96
+ const line = this.lineAt(offset)
97
+ return { offset, line, column: offset - this.lineStarts[line]! }
98
+ }
99
+
100
+ /** End offset of a line's content, excluding its `\n`/`\r\n` delimiter. */
101
+ lineContentEnd(line: number): number {
102
+ const starts = this.lineStarts
103
+ const nextStart = line + 1 < starts.length ? starts[line + 1]! : this.text.length
104
+ let end = nextStart
105
+ if (end > starts[line]! && this.text.charCodeAt(end - 1) === 10) {
106
+ end--
107
+ if (end > starts[line]! && this.text.charCodeAt(end - 1) === 13 /* \r */) end--
108
+ }
109
+ return end
110
+ }
111
+
112
+ /**
113
+ * Build a span for `[rawStart, rawEnd)`. When `trimTrailingNewlines` is set,
114
+ * terminal `\n`/`\r\n` sequences are removed from the *display* end so the
115
+ * inclusive `lastLine` stays on the content rather than a following blank
116
+ * line. `text` and `lineText` preserve original characters.
117
+ */
118
+ span(rawStart: number, rawEnd: number, trimTrailingNewlines = true): SourceSpan {
119
+ let end = rawEnd
120
+ if (trimTrailingNewlines) {
121
+ while (end > rawStart && this.text.charCodeAt(end - 1) === 10) {
122
+ end--
123
+ if (end > rawStart && this.text.charCodeAt(end - 1) === 13) end--
124
+ }
125
+ }
126
+ if (end < rawStart) end = rawStart
127
+
128
+ const start = this.point(rawStart)
129
+ const endPoint = this.point(end)
130
+ const lastLine = end > rawStart ? this.lineAt(end - 1) : start.line
131
+ const span: SourceSpan = {
132
+ start,
133
+ end: endPoint,
134
+ lastLine,
135
+ text: this.text.slice(rawStart, end),
136
+ lineText: this.text.slice(this.lineStarts[start.line]!, this.lineContentEnd(lastLine)),
137
+ }
138
+ if (this.sourceId !== undefined) span.sourceId = this.sourceId
139
+ return span
140
+ }
141
+ }
142
+
143
+ // ---------------------------------------------------------------------------
144
+ // Source-line identity mapping (code / plain text)
145
+ // ---------------------------------------------------------------------------
146
+
147
+ export interface SourceLineRow {
148
+ text: string
149
+ source: DocumentRowSource
150
+ }
151
+
152
+ /**
153
+ * One navigation row per physical source line. Matches the current View
154
+ * behavior: an empty source yields one empty row, and a trailing newline yields
155
+ * a final empty row. Each row's primary span covers the line contents but not
156
+ * its newline delimiter; CRLF is one line break while offsets keep addressing
157
+ * the original `\r\n` string.
158
+ */
159
+ export function sourceLines(source: string, options?: { sourceId?: string }): SourceLineRow[] {
160
+ const index = new SourceIndex(source, options?.sourceId)
161
+ const rows: SourceLineRow[] = []
162
+ for (let line = 0; line < index.lineStarts.length; line++) {
163
+ const start = index.lineStarts[line]!
164
+ const end = index.lineContentEnd(line)
165
+ rows.push({
166
+ text: source.slice(start, end),
167
+ source: { primary: index.span(start, end, false) },
168
+ })
169
+ }
170
+ return rows
171
+ }
172
+
173
+ /** Adapter for `sourceLines()` rows: identity mapping in row/line space. */
174
+ export const sourceLineAdapter = {
175
+ getText: (row: SourceLineRow): string => row.text,
176
+ getSource: (row: SourceLineRow): DocumentRowSource => row.source,
177
+ }