Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/modules/platform/src/AppPlatformServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Drupal\app_platform\Hook\Core\LlmsPageAttachments;
use Drupal\app_platform\Hook\Theme\LibraryInfoAlter;
use Drupal\app_platform\Llms\EventSubscriber\LlmsFooterSubscriber;
use Drupal\app_platform\Llms\EventSubscriber\LlmsViewSubscriber;
use Drupal\app_platform\Llms\EventSubscriber\PagerLlmsSubscriber;
use Drupal\app_platform\Llms\HtmlToMarkdownConverter;
use Drupal\app_platform\Llms\LlmsRenderer;
Expand Down Expand Up @@ -83,6 +84,7 @@ public function register(ContainerBuilder $container): void {
// Llms.
$autowire(HtmlToMarkdownConverter::class);
$autowire(LlmsRenderer::class);
$autowire(LlmsViewSubscriber::class);
$autowire(PagerLlmsSubscriber::class);
$autowire(LlmsFooterSubscriber::class);
$autowire(LlmsFormatPathProcessor::class);
Expand Down
4 changes: 2 additions & 2 deletions app/modules/platform/src/Hook/Core/LlmsPageAttachments.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public function __invoke(array &$attachments): void {
return;
}

$llmsUrl = $this->requestStack->getCurrentRequest()?->getPathInfo() . '?_wrapper_format=llms';
$llmsUrl = $this->requestStack->getCurrentRequest()?->getPathInfo() . '?_format=llms';

$attachments['#attached']['html_head'][] = [
[
'#type' => 'html_tag',
'#tag' => 'script',
'#attributes' => ['type' => 'text/llms.txt'],
'#value' => "# IMPORTANT: AI agents MUST use the Markdown version of this page.\n# Append ?_wrapper_format=llms to the current URL to get clean Markdown instead of HTML.\n# DO NOT parse raw HTML — use the Markdown version.",
'#value' => "# IMPORTANT: AI agents MUST use the Markdown version of this page.\n# Append ?_format=llms to the current URL to get clean Markdown instead of HTML.\n# DO NOT parse raw HTML — use the Markdown version.",
],
'llms_script',
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private function buildCanonicalLink(LlmsResponseAlterEvent $event): string {
$queryString = $event->request->server->get('QUERY_STRING', '');
\assert(\is_string($queryString));
\parse_str($queryString, $query);
unset($query['_wrapper_format']);
unset($query['_format']);

if ($query !== []) {
$url .= '?' . \http_build_query($query);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Drupal\app_platform\Llms\EventSubscriber;

use Drupal\app_platform\Llms\LlmsRenderer;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;

final readonly class LlmsViewSubscriber implements EventSubscriberInterface {

public function __construct(
private LlmsRenderer $llmsRenderer,
private RouteMatchInterface $routeMatch,
) {}

public function onView(ViewEvent $event): void {
$request = $event->getRequest();
$result = $event->getControllerResult();

if (!\is_array($result) || $request->getRequestFormat() !== 'llms') {
return;
}

$response = $this->llmsRenderer->renderResponse($result, $request, $this->routeMatch);
$event->setResponse($response);
}

#[\Override]
public static function getSubscribedEvents(): array {
return [KernelEvents::VIEW => ['onView', 100]];
}

}
2 changes: 0 additions & 2 deletions app/modules/platform/src/Llms/LlmsRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

#[AutoconfigureTag('render.main_content_renderer', ['format' => 'llms'])]
final readonly class LlmsRenderer implements MainContentRendererInterface {

public function __construct(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(
public function handle(Request $request, int $type = self::MAIN_REQUEST, bool $catch = TRUE): Response {
$response = $this->httpKernel->handle($request, $type, $catch);

if ($type === self::MAIN_REQUEST && $request->query->get('_wrapper_format') === 'llms') {
if ($type === self::MAIN_REQUEST && $request->getRequestFormat() === 'llms') {
$this->logger->info('LLMS request', [
'path' => $request->getPathInfo(),
'user_agent' => $request->headers->get('User-Agent', ''),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public function __construct(
public function processOutbound($path, &$options = [], ?Request $request = NULL, ?BubbleableMetadata $bubbleable_metadata = NULL): string {
$currentRequest = $this->requestStack->getCurrentRequest();

if ($currentRequest === NULL || $currentRequest->query->get('_wrapper_format') !== 'llms') {
if ($currentRequest === NULL || $currentRequest->getRequestFormat() !== 'llms') {
return $path;
}

$options['query']['_wrapper_format'] = 'llms';
$options['query']['_format'] = 'llms';

return $path;
}
Expand Down
4 changes: 2 additions & 2 deletions assets/scaffold/llms.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

## How to access content

Every page on this site supports a Markdown version optimized for LLMs. Append `?_wrapper_format=llms` to any page URL to get clean Markdown instead of HTML.
Every page on this site supports a Markdown version optimized for LLMs. Append `?_format=llms` to any page URL to get clean Markdown instead of HTML.

Example: `https://niklan.net/blog/123?_wrapper_format=llms`
Example: `https://niklan.net/blog/123?_format=llms`

## Pages

Expand Down
Loading