Hmbown/CodeWhale · error

markdown link regex

Error message

markdown link regex

What it means

Compilation assertion for MARKDOWN_LINK_RE ([text](url "title")) used by parse_markdown to extract links. The expect fires only if the hardcoded literal fails to compile when first initialized — a developer error in the pattern text, not a failure triggered by the markdown content being parsed.

Source

Thrown at crates/tui/src/tools/web_run.rs:1400

    let decoded = decode_html_entities(&without_tags);

    let mut lines = Vec::new();
    for line in decoded.lines() {
        let trimmed = normalize_whitespace(line);
        if trimmed.is_empty() {
            continue;
        }
        for wrapped in wrap_line(&trimmed, ResponseLength::Medium.wrap_width()) {
            lines.push(wrapped);
        }
    }

    (lines, links, title)
}

fn parse_markdown(markdown: &str, base_url: &str) -> (Vec<String>, Vec<WebLink>) {
    let re = MARKDOWN_LINK_RE.get_or_init(|| {
        Regex::new(r#"\[([^\]]+)\]\(([^\s)]+)(?:\s+"[^"]*")?\)"#).expect("markdown link regex")
    });
    let mut links = Vec::new();
    let mut replaced = String::with_capacity(markdown.len());
    let mut last = 0;
    for capture in re.captures_iter(markdown) {
        let Some(full) = capture.get(0) else { continue };
        let Some(text) = capture.get(1) else { continue };
        let Some(target) = capture.get(2) else {
            continue;
        };
        replaced.push_str(&markdown[last..full.start()]);
        let id = links.len() + 1;
        let text = normalize_whitespace(text.as_str());
        let url = resolve_url(base_url, target.as_str());
        links.push(WebLink {
            id,
            url,
            text: text.clone(),

View on GitHub (pinned to 0c42157ee5)

Solutions

  1. Exercise parse_markdown in a unit test so the get_or_init runs under CI
  2. Keep the pattern literal; escape special characters correctly when modifying it
  3. Consider once_cell Lazy combined with a const-pattern check if more regexes are added
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/tui/src/tools/web_run.rs:1400 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of Hmbown/CodeWhale@0c42157ee5 (2026-08-20). Data as JSON: /api/errors/a3e89ff8eed30968. Report an issue: GitHub.