2.2 KiB
2.2 KiB
Fix ChatGPT DOM extractor external link handling.
Observed defect: External links in ChatGPT responses are being extracted as plain text only.
Example bad output: Services such as:
Amazon Prime Video Apple TV Fandango at Home (Vudu)
Expected behavior: Preserve links as Markdown links when href is available:
Requirements:
-
Update src/chatgptExtractor.js.
-
Add Markdown link support for elements:
- Extract visible link text using the existing inline extractor.
- Extract href.
- If href is valid, output
[text](href). - If href is missing or unsafe, fall back to plain visible text.
-
Preserve surrounding inline formatting:
- Links inside paragraphs remain inline.
- Links inside lists remain list items.
- Links inside table cells remain Markdown links.
- Do not force every link onto its own line unless the original block structure does.
-
URL handling:
- Preserve http:// and https:// links.
- Preserve mailto: links if encountered.
- Ignore or flatten javascript:, data:, blob:, and empty href values.
- Decode or normalize only as needed for readable Markdown.
- Escape closing parentheses in URLs if necessary.
-
Link text handling:
- Trim excessive whitespace.
- If link text is empty, use the href as the visible text.
- Escape square brackets in link text.
- Do not duplicate the URL if ChatGPT already displays the raw URL as the link text.
-
Preserve existing behavior:
- Paragraphs remain paragraphs.
- Lists remain Markdown lists.
- Tables remain Markdown tables.
- Code blocks remain fenced code.
- Bold, italics, and inline code remain inline.
- Message order and role detection unchanged.
Suggested helper functions:
- linkToMarkdown(anchorNode, context)
- isSafeHref(href)
- escapeMarkdownLinkText(text)
- escapeMarkdownLinkUrl(url)
Acceptance criteria:
- External links export as valid Markdown links.
- Plain text without links remains unchanged.
- Unsafe href values are not preserved as Markdown links.
- Links in lists, paragraphs, and tables remain readable.
- No LLM calls.
- No Project Thoth application dependency.