konoma 0.19.0

Terminal file browser built for AI pair-programming — full-screen previews (Markdown, images, PDF, CSV), git suite, and an agent-watch mode that follows your AI's edits (macOS and Linux)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
use super::*;

/// Whether a span is a Markdown link (blue underline). Since konoma's StyleSheet.link()=underline+blue draws them,
/// these two conditions decide it (code/headings/rules have different colors, so no false positives).
pub(super) fn is_link_span(span: &Span<'static>) -> bool {
    use ratatui::style::{Color, Modifier};
    span.style.add_modifier.contains(Modifier::UNDERLINED) && span.style.fg == Some(Color::Blue)
}

/// Scan collapsed decorated lines for Tab-cycle items (links / task checkboxes / code-block
/// headers) in document order. `targets` are the link URLs recovered by `collapse_links`, in the
/// same order the link spans appear. `fence_ords` are the **source** ordinals of the rendered
/// mermaid placements in order (from `ImagePlacement::fence_ord`) — the k-th sentinel takes the
/// k-th value. Shared by the cache build and the test-only decorate path.
/// Slugify a heading's text the way GitHub does for `#anchor` links: lowercase, drop punctuation
/// except `-`/`_`, spaces → `-` (consecutive spaces → consecutive `-`, not collapsed). Unicode
/// letters/digits are kept (CJK headings anchor too). Used to match `[x](#slug)` link targets.
pub(super) fn github_slug(text: &str) -> String {
    let mut s = String::new();
    for c in text.trim().chars() {
        if c.is_alphanumeric() {
            s.extend(c.to_lowercase());
        } else if c == ' ' {
            s.push('-');
        } else if c == '-' || c == '_' {
            s.push(c);
        }
    }
    s
}

/// Build the in-page anchor map (slug → decorated logical-line index) from the rendered lines, in
/// document order, with GitHub's duplicate-slug disambiguation (`slug`, `slug-1`, `slug-2`, …).
pub(super) fn compute_md_anchors(lines: &[Line<'static>]) -> Vec<(String, usize)> {
    use std::collections::HashMap;
    let mut anchors = Vec::new();
    let mut counts: HashMap<String, usize> = HashMap::new();
    for (i, line) in lines.iter().enumerate() {
        let Some(text) = crate::preview::markdown::heading_text(line) else {
            continue;
        };
        let base = github_slug(&text);
        if base.is_empty() {
            continue;
        }
        let n = counts.entry(base.clone()).or_insert(0);
        let slug = if *n == 0 {
            base.clone()
        } else {
            format!("{base}-{n}")
        };
        *n += 1;
        anchors.push((slug, i));
    }
    anchors
}

pub(super) fn build_md_items(
    lines: &[Line<'static>],
    targets: &[String],
    fence_ords: &[usize],
) -> Vec<MdItem> {
    let mut items = Vec::new();
    let mut k = 0usize;
    for (li, line) in lines.iter().enumerate() {
        for span in &line.spans {
            if is_link_span(span) {
                let target = targets.get(k).cloned().unwrap_or_default();
                items.push(MdItem {
                    line: li,
                    kind: MdItemKind::Link { target },
                });
                k += 1;
            } else if crate::preview::markdown::is_task_span(span) {
                if let Some(state) =
                    crate::preview::markdown::task_span_state(span.content.as_ref())
                {
                    items.push(MdItem {
                        line: li,
                        kind: MdItemKind::Task { state },
                    });
                }
            } else if crate::preview::markdown::is_code_header_span(span) {
                items.push(MdItem {
                    line: li,
                    kind: MdItemKind::CodeBlock,
                });
            } else if crate::preview::markdown::is_mermaid_header_span(span) {
                let seen = items
                    .iter()
                    .filter(|it| matches!(it.kind, MdItemKind::MermaidFence { .. }))
                    .count();
                // 序数は placement 由来の**ソース順**(loading/失敗フェンス込みの通し番号)。
                // 描画済み番兵の個数で代用すると、上流に番兵を出さないフェンス(テキスト降格/
                // loading 中)があるとき Enter のソース再抽出とずれて**別の図**が開く。
                // fence_ords が無い経路(テスト互換)のみ従来の計数にフォールバック。
                let ordinal = fence_ords.get(seen).copied().unwrap_or(seen);
                items.push(MdItem {
                    line: li,
                    kind: MdItemKind::MermaidFence { ordinal },
                });
            } else if crate::preview::markdown::is_details_header_span(span) {
                let ordinal = items
                    .iter()
                    .filter(|it| matches!(it.kind, MdItemKind::Details { .. }))
                    .count();
                items.push(MdItem {
                    line: li,
                    kind: MdItemKind::Details { ordinal },
                });
            }
        }
    }
    items
}

/// Apply the focus inversion to one decorated line. `ordinal` = index of the focused marker span
/// within this line (counting link/task/code-header spans in order); `whole_line`=code-block
/// header (all spans inverted — one line, a clear focus cue). Other lines pass through untouched.
pub(super) fn invert_focused_line(
    line: &Line<'static>,
    ordinal: usize,
    whole_line: bool,
) -> Line<'static> {
    use ratatui::style::Modifier;
    let style = line.style;
    let mut seen = 0usize;
    let spans = line
        .spans
        .iter()
        .cloned()
        .map(|mut span| {
            if whole_line {
                span.style = span.style.add_modifier(Modifier::REVERSED);
            } else if is_link_span(&span)
                || crate::preview::markdown::is_task_span(&span)
                || crate::preview::markdown::is_code_header_span(&span)
                || crate::preview::markdown::is_mermaid_header_span(&span)
                || crate::preview::markdown::is_details_header_span(&span)
            {
                if seen == ordinal {
                    span.style = span.style.add_modifier(Modifier::REVERSED);
                }
                seen += 1;
            }
            span
        })
        .collect::<Vec<_>>();
    Line::from(spans).style(style)
}

/// Fold the accompanying URL of the "label (URL)" that tui-markdown emits, leaving **only the label** in link style (blue underline,
/// with a leading link icon when `icons=true`). The URLs are collected in order into `targets` and returned (hidden destinations).
/// Pattern: `[label]` `" ("` `[URL(blue underline)]` `")"`. Spans that do not match are passed through unchanged.
pub(super) fn collapse_links(
    lines: Vec<Line<'static>>,
    icons: bool,
) -> (Vec<Line<'static>>, Vec<String>) {
    use ratatui::style::{Color, Modifier, Style};
    let link_style = Style::new()
        .fg(Color::Blue)
        .add_modifier(Modifier::UNDERLINED);
    let mut out = Vec::with_capacity(lines.len());
    let mut targets = Vec::new();
    for line in lines {
        let style = line.style;
        let spans = line.spans;
        let n = spans.len();
        let mut new: Vec<Span<'static>> = Vec::with_capacity(n);
        let mut i = 0;
        while i < n {
            // 表セル由来の「ラベル+隠しターゲット」ペア(markdown::render_table が生成):
            // ラベルはそのまま残し(幅を変えない=表の桁揃え維持のためアイコンも付けない)、
            // 隠しスパンを取り除いて URL を targets へ回収する(Tab/Enter は通常リンクと同じ)。
            if i + 1 < n
                && is_link_span(&spans[i])
                && crate::preview::markdown::is_hidden_link_target(&spans[i + 1])
            {
                targets.push(spans[i + 1].content.to_string());
                new.push(spans[i].clone());
                i += 2;
                continue;
            }
            let is_link_pattern = i + 2 < n
                && spans[i + 1].content.as_ref() == " ("
                && is_link_span(&spans[i + 2])
                && spans.get(i + 3).is_some_and(|s| s.content.starts_with(')'));
            if is_link_pattern {
                let label = spans[i].content.as_ref();
                targets.push(spans[i + 2].content.to_string());
                let text = if icons {
                    format!("{} {label}", crate::ui::icons::link_icon())
                } else {
                    label.to_string()
                };
                new.push(Span::styled(text, link_style));
                // 閉じ括弧 ")" の後ろに続く文字(句読点等)は保持する。
                let closer = spans[i + 3].content.as_ref();
                if closer.len() > 1 {
                    new.push(Span::styled(closer[1..].to_string(), spans[i + 3].style));
                }
                i += 4;
            } else {
                new.push(spans[i].clone());
                i += 1;
            }
        }
        out.push(Line::from(new).style(style));
    }
    (out, targets)
}

/// Trim trailing punctuation from a matched URL run, GFM-style: drop a trailing run of
/// `? ! . , : * _ ~ ' "` and any `)` that has no matching `(` inside the run (iteratively).
fn trim_url_end(s: &str) -> &str {
    let mut end = s.len();
    loop {
        let sub = &s[..end];
        let Some(last) = sub.chars().last() else {
            break;
        };
        if "?!.,:*_~'\"".contains(last) {
            end -= last.len_utf8();
            continue;
        }
        if last == ')' && sub.matches(')').count() > sub.matches('(').count() {
            end -= 1;
            continue;
        }
        break;
    }
    &s[..end]
}

/// A character that ends a bare-URL run: whitespace, `<`, or CJK/fullwidth punctuation. The last set
/// matters for konoma's CJK audience — Japanese text often abuts a URL with no ASCII space
/// (`https://x.example、と`), and GFM's "stop only at whitespace/`<`" would swallow the trailing
/// characters into the link. Stopping at these keeps the URL clean.
fn is_url_stop(c: char) -> bool {
    c.is_whitespace()
        || c == '<'
        || matches!(
            c,
            '' | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
        )
}

/// Match a bare URL autolink at the start of `rest` (`http://…`, `https://…`, or `www.…`). Returns
/// the matched byte length and the target to open (`www.` gains an `http://` prefix). The run stops
/// at whitespace / `<` / CJK punctuation (`is_url_stop`), then trailing punctuation is trimmed. None
/// if `rest` does not start a URL.
fn match_bare_url(rest: &str) -> Option<(usize, String)> {
    let is_www = rest.starts_with("www.");
    if !(is_www || rest.starts_with("http://") || rest.starts_with("https://")) {
        return None;
    }
    let run_end = rest.find(is_url_stop).unwrap_or(rest.len());
    let run = trim_url_end(&rest[..run_end]);
    // Need a host after the scheme (or after `www.` a further alphanumeric char).
    let valid = if is_www {
        run.len() > 4 && run[4..].contains(|c: char| c.is_ascii_alphanumeric())
    } else {
        run.find("://").is_some_and(|p| run.len() > p + 3)
    };
    if !valid {
        return None;
    }
    let target = if is_www {
        format!("http://{run}")
    } else {
        run.to_string()
    };
    Some((run.len(), target))
}

fn is_email_local(c: char) -> bool {
    c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '%' | '+' | '-')
}
fn is_email_domain(c: char) -> bool {
    c.is_ascii_alphanumeric() || matches!(c, '.' | '-')
}

/// Match a bare email autolink at the start of `rest` (`local@domain.tld`). Returns the matched byte
/// length and a `mailto:` target. Requires a non-empty local part, an `@`, and a domain with a dot
/// whose last label is ≥2 ASCII letters. Trailing `.`/`-` are trimmed from the domain.
fn match_bare_email(rest: &str) -> Option<(usize, String)> {
    let local_len: usize = rest
        .chars()
        .take_while(|&c| is_email_local(c))
        .map(char::len_utf8)
        .sum();
    if local_len == 0 || !rest[local_len..].starts_with('@') {
        return None;
    }
    let after_at = &rest[local_len + 1..];
    let mut dom_len: usize = after_at
        .chars()
        .take_while(|&c| is_email_domain(c))
        .map(char::len_utf8)
        .sum();
    // Trim trailing '.'/'-' from the domain.
    while dom_len > 0 && matches!(after_at.as_bytes()[dom_len - 1], b'.' | b'-') {
        dom_len -= 1;
    }
    let domain = &after_at[..dom_len];
    let tld_ok = domain
        .rsplit_once('.')
        .is_some_and(|(_, tld)| tld.len() >= 2 && tld.chars().all(|c| c.is_ascii_alphabetic()));
    if !tld_ok {
        return None;
    }
    let end = local_len + 1 + dom_len;
    Some((end, format!("mailto:{}", &rest[..end])))
}

/// Find bare autolinkable spans (GFM autolink literals) in `text`: `http(s)://…`, `www.…`, and
/// `local@domain.tld` emails. Returns `(start_byte, end_byte, target)` in document order,
/// non-overlapping. A pragmatic subset of GFM §6.9: a match may start only at the beginning or after
/// a non-alphanumeric byte, the run stops at whitespace/`<`, and trailing punctuation (plus
/// unbalanced `)`) is trimmed. `www.` gets an `http://` prefix; emails a `mailto:` prefix.
pub(super) fn find_bare_links(text: &str) -> Vec<(usize, usize, String)> {
    let bytes = text.as_bytes();
    let n = bytes.len();
    let mut out: Vec<(usize, usize, String)> = Vec::new();
    let mut i = 0usize;
    while i < n {
        let boundary_ok = i == 0 || !bytes[i - 1].is_ascii_alphanumeric();
        if boundary_ok {
            let rest = &text[i..];
            if let Some((len, target)) = match_bare_url(rest).or_else(|| match_bare_email(rest)) {
                out.push((i, i + len, target));
                i += len.max(1);
                continue;
            }
        }
        i += text[i..].chars().next().map_or(1, char::len_utf8);
    }
    out
}

/// Post-pass over collapsed lines: turn bare URLs/emails in plain-text spans into link spans, and
/// interleave their targets into `targets` in document order (so `build_md_items` pairs each link
/// span with the right URL). Skips spans that already carry a link, a background (inline code / code
/// fence content), or a konoma sentinel (task / code-header / mermaid / hidden-target) — matching
/// GitHub, which never auto-links inside code.
pub(super) fn autolink_bare_urls(
    lines: Vec<Line<'static>>,
    in_targets: Vec<String>,
) -> (Vec<Line<'static>>, Vec<String>) {
    use ratatui::style::{Color, Modifier, Style};
    let link_style = Style::new()
        .fg(Color::Blue)
        .add_modifier(Modifier::UNDERLINED);
    let mut out_lines = Vec::with_capacity(lines.len());
    let mut out_targets = Vec::new();
    let mut ti = 0usize;
    for line in lines {
        // Code-block lines never auto-link (a URL in a fence stays literal). Keep as-is; for any
        // existing link span (none in practice) carry its target through 1:1 so the k-th link span
        // in `out_lines` still maps to `out_targets[k]` — no drift if a code line ever holds a link.
        if crate::preview::markdown::is_code_line(&line) {
            for span in &line.spans {
                if is_link_span(span) {
                    out_targets.push(in_targets.get(ti).cloned().unwrap_or_default());
                    ti += 1;
                }
            }
            out_lines.push(line);
            continue;
        }
        let style = line.style;
        let mut new: Vec<Span<'static>> = Vec::with_capacity(line.spans.len());
        for span in line.spans {
            // Existing link span → keep as-is; it owns the next input target (preserve order).
            if is_link_span(&span) {
                out_targets.push(in_targets.get(ti).cloned().unwrap_or_default());
                ti += 1;
                new.push(span);
                continue;
            }
            // Never auto-link inside code (inline code / bg-filled code) or over a konoma sentinel.
            if span.style.bg.is_some()
                || crate::preview::markdown::is_inline_code_span(&span)
                || crate::preview::markdown::is_task_span(&span)
                || crate::preview::markdown::is_code_header_span(&span)
                || crate::preview::markdown::is_mermaid_header_span(&span)
                || crate::preview::markdown::is_hidden_link_target(&span)
            {
                new.push(span);
                continue;
            }
            let text = span.content.into_owned();
            let matches = find_bare_links(&text);
            if matches.is_empty() {
                new.push(Span::styled(text, span.style));
                continue;
            }
            let mut pos = 0usize;
            for (s, e, url) in matches {
                if s > pos {
                    new.push(Span::styled(text[pos..s].to_string(), span.style));
                }
                new.push(Span::styled(text[s..e].to_string(), link_style));
                out_targets.push(url);
                pos = e;
            }
            if pos < text.len() {
                new.push(Span::styled(text[pos..].to_string(), span.style));
            }
        }
        out_lines.push(Line::from(new).style(style));
    }
    (out_lines, out_targets)
}

/// Replace `:shortcode:` runs in `text` with Unicode emoji (gemoji names, e.g. `:rocket:` → 🚀).
/// Returns `None` when nothing changed (so the caller reuses the original span untouched). A
/// shortcode with no Unicode mapping (GitHub-custom like `:shipit:`) and non-shortcode `:` (times,
/// ratios) are left exactly as they were.
pub(super) fn replace_emoji_shortcodes(text: &str) -> Option<String> {
    if !text.contains(':') {
        return None;
    }
    let mut out = String::new();
    let mut changed = false;
    let mut rest = text;
    while let Some(start) = rest.find(':') {
        out.push_str(&rest[..start]);
        let after = &rest[start + 1..];
        if let Some(end) = after.find(':') {
            let code = &after[..end];
            let looks_like_code = !code.is_empty()
                && code
                    .chars()
                    .all(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '+' | '-'));
            if looks_like_code {
                if let Some(e) = emojis::get_by_shortcode(code) {
                    out.push_str(e.as_str());
                    changed = true;
                    rest = &after[end + 1..];
                    continue;
                }
            }
            // Not a known shortcode: keep this `:` and resume scanning right after it.
            out.push(':');
            rest = after;
        } else {
            out.push(':');
            out.push_str(after);
            rest = "";
        }
    }
    out.push_str(rest);
    changed.then_some(out)
}

/// Post-pass over decorated Markdown lines: convert `:shortcode:` emoji to Unicode in plain-text
/// spans. Skips spans that carry a background (inline code / code fence), a link, or a konoma
/// sentinel (task / code-header / mermaid / hidden-target) — matching GitHub, which never converts
/// shortcodes inside code, and keeping Tab-item spans byte-stable.
pub(super) fn substitute_emoji(lines: Vec<Line<'static>>) -> Vec<Line<'static>> {
    lines
        .into_iter()
        .map(|line| {
            // Code-block lines keep their `:shortcode:` literal (a fence is verbatim).
            if crate::preview::markdown::is_code_line(&line) {
                return line;
            }
            let style = line.style;
            let spans = line
                .spans
                .into_iter()
                .map(|span| {
                    if span.style.bg.is_some()
                        || is_link_span(&span)
                        || crate::preview::markdown::is_inline_code_span(&span)
                        || crate::preview::markdown::is_task_span(&span)
                        || crate::preview::markdown::is_code_header_span(&span)
                        || crate::preview::markdown::is_mermaid_header_span(&span)
                        || crate::preview::markdown::is_hidden_link_target(&span)
                    {
                        return span;
                    }
                    match replace_emoji_shortcodes(span.content.as_ref()) {
                        Some(new) => Span::styled(new, span.style),
                        None => span,
                    }
                })
                .collect::<Vec<_>>();
            Line::from(spans).style(style)
        })
        .collect()
}