blog.dopana

Back

In the age of AI assistants, code companions, and Large Language Models (LLMs), reading habits on the web have fundamentally shifted. Readers no longer just read with their eyes; they frequently copy technical posts directly into ChatGPT, Claude, or Cursor to summarize, query, and refactor concepts.

However, standard web copying (highlighting text with a cursor and pressing Ctrl + C) produces frustrating results: mixed HTML junk, broken header structures, mangled tables, and lost code block formatting.

This post walks through how to build a reusable “Copy as Markdown” button for Astro, turning your blog into an Agent-Friendly Web resource.

The Problem: Why Copying from Web Pages Fails AI Agents#

When someone highlights text and copies content from a typical rendered web page:

  • The system clipboard receives a stripped rich-text or plain-text representation torn away from its semantic source.
  • Markdown links, footnotes, code syntax highlighting blocks, and formatted tables degrade into disconnected fragments.
  • When copying entire articles, peripheral interface chrome like navigation headers, footers, buttons, and sidebars bleed into the LLM context window, burning tokens and degrading prompt fidelity.

[!NOTE] ELI5 (Explain Like I’m 10): Imagine your article is a delicious birthday cake. The web UI (HTML/CSS) is the disposable cardboard box, plastic fork, and ribbon wrapped around it. When an AI agent wants to taste your cake, it only wants the pure sponge cake (original Markdown) — it does not want to choke on the cardboard box or plastic wrap (HTML wrappers, sidebars, and ads)!

The Solution: Injecting Raw Markdown into a Hidden Textarea#

There are two common architectural patterns to deliver raw Markdown:

  1. Injecting into HTML data attributes (such as data-markdown="..."): This bloats the browser’s DOM tree, creates edge cases with character escaping (quotes, newlines, code ticks), and slows down DOM rendering.
  2. Injecting into a visually hidden <textarea> element: This is the gold standard. A <textarea> element safely preserves multiline text and special characters without HTML entity escaping issues, while remaining instantly accessible via JavaScript when the copy button is clicked.
Browser RuntimeAstro Build Timepost.bodyPass prop1. Click event2. Read clean payload3. Paste into chatboxSource Files (.md / .mdx)Astro Content LayerBlogPost.astroHidden Textarea (sr-only)Raw Markdown PayloadCopy as Markdown Button(daisyUI)System ClipboardAI Agent / LLM Prompt

Step-by-Step Implementation#

Step 1: Create CopyMarkdownButton.astro#

We create a self-contained component at src/components/CopyMarkdownButton.astro. It accepts the raw Markdown string (content), alongside optional title and description props to enrich the prompt context for LLMs.

The button is styled with daisyUI 5 utility classes (btn btn-sm btn-soft), providing instant visual feedback by switching to btn-success upon a successful copy operation:

[!TIP] View Transitions Compatibility: Astro supports client-side navigation via View Transitions. Listening to both astro:page-load and astro:after-swap, guarded by dataset.listener = 'true', prevents duplicate listeners and ensures the button remains fully interactive after soft navigations.

Step 2: Integrate into BlogPost.astro#

Under Astro’s Content Collections, every entry inside the blog collection contains a raw body property representing the uncompiled Markdown text.

Extract body in your layout file and render the button directly above the article contents:

Key Benefits#

  1. Superior AI Experience: When readers want to feed your article to Cursor, Claude Projects, or ChatGPT, a single click delivers clean, 100% formatted Markdown with code blocks, headings, and tables intact.
  2. Zero Runtime Overhead: No server endpoints, no heavy client-side HTML-to-Markdown converters; payloads are injected at static build-time.
  3. daisyUI Aesthetic: The button is lightweight, fully accessible, and adapts effortlessly to dark and light color themes.

References#