Files
Project-Thoth/applications/chatgpt-capture/codex-work-orders/debugging/0004-external-link-defect.md
T

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:

  1. Update src/chatgptExtractor.js.

  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.