I'm adapting a React and Next.js site for better use by AI agents. I'd like to serve the same content as HTML for browsers and as text/markdown or plain text for clients that can't process complex HTML, CSS, or JavaScript. Should I convert the generated HTML into Markdown, or keep a structured format such as XML or another content model as the source of truth and render it differently based on the request headers?
4 Answers
HTML-to-Markdown conversion is fairly straightforward for ordinary content—headings, paragraphs, links, lists, emphasis, and code blocks all have common equivalents. However, HTML features such as JavaScript widgets, forms, layout elements, and custom components do not have direct Markdown representations. Decide what content should remain accessible in the text version and provide a meaningful fallback for unsupported elements.
The usual approach is to keep Markdown or structured content as the source of truth, then render it to HTML for browsers and expose the original or transformed content as Markdown/plain text for other clients. In a Next.js app, tools such as remark, unified, or Markdoc can parse and render Markdown. Converting already-generated HTML back into Markdown is possible, but it can be lossy and difficult to maintain, especially with interactive components.
A better architecture is to store content in a structured, presentation-independent model—Markdown, a CMS schema, or JSON—and let the backend negotiate the response format. Return HTML to browsers and Markdown or plain text to clients that request it through the Accept header. Avoid using generated HTML as your canonical data, because converting it back later can lose structure and meaning.
Markdown is mainly a lightweight formatting syntax, not a replacement for HTML. It works well for articles and documentation, but it cannot represent most interactive UI. Keep the full HTML/React experience for normal users and create a deliberate text endpoint that extracts the important headings, prose, links, lists, and code instead of trying to serialize every visual component.

The main reason to offer Markdown or plain text is usually reducing noise for downstream consumers. It can contain more of the actual prose per token, but capable clients and AI systems can still parse normal HTML, so this should be treated as an additional representation rather than a replacement.