Skip to content

fix: render markdown links that include a title attribute - #131

Closed
masfour7 wants to merge 4 commits into
mainfrom
fix/links-with-title
Closed

fix: render markdown links that include a title attribute#131
masfour7 wants to merge 4 commits into
mainfrom
fix/links-with-title

Conversation

@masfour7

@masfour7 masfour7 commented May 6, 2026

Copy link
Copy Markdown
Collaborator

Problem

Markdown links with a title attribute are not rendered — the raw syntax appears as plain text:

// Expected: a clickable "Click Here" link
// Actual: raw "[Click Here](https://foo.com \"page title\")" shown as text
GptMarkdown('[Click Here](https://foo.com "page title")');

This is valid CommonMark syntax. Reported in #122, fix approach described in #121.

Root Cause

Two issues in ATagMd inside lib/markdown_component.dart:

  1. Regex too strict — the pattern [^\\s]* stops matching at the first space, so url "title" was never captured.
  2. Title not stripped — even if the regex had matched, the raw title string would have been included in the URL passed to onLinkTap.

Fix

1. Broaden the regex to allow any character except ) inside the link parentheses:

// Before
RegExp get exp => RegExp(r"(?<!\!)[.*]\([^\s]*\)");

// After — [^)] allows spaces, quotes, anything except the closing paren
RegExp get exp => RegExp(r"(?<!\!)[.*?]\([^)]*\)");

2. Strip the title from the URL after extraction:

// Before
final url = text.substring(urlStart, urlEnd).trim();

// After
final rawUrl = text.substring(urlStart, urlEnd).trim();
// Strip optional title attribute: [text](url "title") or [text](url 'title')
final url = rawUrl.replaceFirst(RegExp(r'\s+["\'\'][^\"\'']*["\'\']\s*$'), '').trim();

The URL passed to onLinkTap is now clean with no title string appended.

Closes #122
Closes #121

@masfour7
masfour7 requested a review from saminsohag May 6, 2026 22:54
@saminsohag

Copy link
Copy Markdown
Collaborator

This pull request contains issues in the code.

Screenshot 2026-05-08 at 4 12 53 PM

@laithsiam1 laithsiam1 closed this Jul 28, 2026
@laithsiam1
laithsiam1 deleted the fix/links-with-title branch July 28, 2026 07:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Links with Title not rendered - fixed by PR 121

3 participants