Files
Project-Thoth/codex/chatgpt-capture-mvp/implementation/0003b-dom-to-markdown-conversion.md
T

2.8 KiB

Implement Task 3B — ChatGPT DOM-to-Markdown Conversion.

Context:

  • Task 3A discovers all conversation-bearing containers.
  • 3A prioritizes completeness and returns containers/messages with role, type, rawText, htmlSnippet, and debug data.
  • This task improves formatting quality for discovered ChatGPT message containers.
  • Do not change the 3A discovery strategy unless absolutely necessary.

Goal: Convert discovered ChatGPT message DOM content into readable Markdown while preserving source structure.

Primary rule: Do not lose content. If structured conversion fails, fall back to rawText.

Requirements:

  1. Preserve paragraphs:

    • becomes one paragraph.

    • Paragraphs separated by one blank line.
    • Inline elements stay inline.
  2. Preserve inline formatting:

    • /text
    • /text
    • inline text
    • text
  3. Preserve lists:

      • → - item
        1. → 1. item
        2. Preserve nested indentation where practical.
    • Preserve code blocks:

      •  → fenced Markdown.
        
      • Preserve code exactly.
      • Include language tag if detectable.
    • Preserve tables:

      • → Markdown table.
      • Use
      • or first row as header.
      • Escape pipe characters inside cells.
      • Do not duplicate flattened table text.
      • Preserve blockquotes:

        • Prefix lines with >.
      • Preserve headings:

        • h1-h6 → Markdown headings.
      • Fix spacing and punctuation:

        • Do not put punctuation on its own line.
        • Do not break around bold/italic/link spans.
        • Keep em dashes inline.
        • Collapse excessive blank lines outside code fences only.
      • Preserve attachments/tool/artifact placeholders:

        • If a container represents image generation, file upload, artifact, or tool output and cannot be fully converted, emit a readable placeholder plus rawText.
        • Example: [Unsupported ChatGPT content: image generation]
      • Keep conversion separate from discovery:

        • discovery decides what containers exist.
        • conversion decides how each container becomes Markdown.

        Suggested functions:

        • containerToMarkdown(container)
        • nodeToMarkdown(node, context)
        • blockChildrenToMarkdown(node, context)
        • inlineChildrenToMarkdown(node, context)
        • listToMarkdown(listNode, context)
        • tableToMarkdown(tableNode, context)
        • codeBlockToMarkdown(preNode)
        • linkToMarkdown(anchorNode, context)
        • cleanupMarkdownOutsideCodeFences(markdown)

        Acceptance criteria:

        • Previously discovered messages are still all present.
        • Bold text exports as bold text.
        • Links export as Markdown links.
        • Tables export as Markdown tables.
        • Lists remain lists.
        • Code blocks remain fenced and unchanged.
        • Paragraph breaks are preserved.
        • No punctuation-only lines are introduced.
        • Unknown/unsupported containers are not dropped.
        • No LLM calls.
        • No Project Thoth application dependency.