Markdown versions
An agent reads Markdown better than HTML. A route whose _format requirement lists md has a Markdown version,
rendered by its action from a template of its own: the bundle does not convert HTML.
llms_txt: markdown_versions: true#[Route('/blog/{slug}.{_format}', name: 'post_show', format: 'html', requirements: ['_format' => 'html|md'])]public function show(Request $request, Post $post): Response{ return $this->render('post/show.' . $request->getRequestFormat() . '.twig', ['post' => $post]);}{# templates/post/show.md.twig #}# {{ post.title }}
{{ post.body }}format: 'html' is the default _format. The bundle registers the md request format, served as
text/markdown; charset=UTF-8, and the action answers Markdown:
- at
/blog/first-post.md: the{_format}of the path ismd; - at
/blog/first-post, to anAcceptheader preferringtext/markdowntotext/html, e.g.Accept: text/markdown, text/html;q=0.9. A browser, which asks fortext/htmlfirst, still gets the HTML page.
The requirement must name md as one of its alternatives (html|md, (md|html)): a pattern which merely matches it,
such as [a-z]+, does not opt in, its action would get the Markdown requests without a Markdown template.
Accept-only pages
Section titled “Accept-only pages”Without {_format} in the path, the Accept header is the only way to the Markdown version: the same route, without
the suffix.
#[Route('/faq', name: 'faq', format: 'html', requirements: ['_format' => 'html|md'])]The router ignores a requirement on a variable which is not in the path: it only tells the bundle that /faq has a
Markdown version.
Templates
Section titled “Templates”Twig escapes HTML in a template whose extension it does not know: {{ post.title }} would render Q&A as Q&A.
With markdown_versions, the bundle escapes nothing in the *.md.twig templates, as Twig does for *.txt.twig. An
application with an escaping strategy of its own (twig.autoescape_service) keeps it. Clear the cache after enabling
the option: Twig decides the escaping when it compiles a template.
A content stored as HTML, e.g. by a WYSIWYG editor, can be converted in the template by the html_to_markdown filter
of twig/markdown-extra.
Headers
Section titled “Headers”- The responses of these routes carry
Vary: Accept: one URL answers HTML or Markdown, a cache keeps both. - The HTML page links to its Markdown version in its
Linkheader, through WebLink (needssymfony/web-link):<https://example.com/blog/first-post.md>; rel="alternate"; type="text/markdown". An Accept-only page links to itself.
In llms.txt
Section titled “In llms.txt”The link of a route option points to the .md URL on its own. In a
link provider, generate the URL with _format: md:
$this->urlGenerator->generate('post_show', ['slug' => $post->getSlug(), '_format' => 'md'], UrlGeneratorInterface::ABSOLUTE_URL);Caching
Section titled “Caching”The bundle lists the routes with a Markdown version next to the compiled routes, in
%kernel.build_dir%/llms_txt_markdown_routes.php: a request checks its route against this list without loading the
route collection. The list follows the route files like the router cache, and cache:warmup builds it.
