Problem/Motivation

For delivering #3550681: Add field "Include in llms.txt" for all content types we need a way a flexible way for embed the markdown version of a Views to /llms.txt.
Tokens warrants flexibility because their result could appear anywhere in the output.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#6 6.diff3.58 KBmxr576

Issue fork llms_txt-3550772

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

mxr576 created an issue. See original summary.

mxr576’s picture

@a.dmitriiev shared the following code examples, thanks for this!

  $info['types']['llm_txt_views'] = [
    'name' => t('llms.txt: Views'),
    'description' => t('Render views in markdown for llms.txt'),
  ];
  $views = \Drupal::entityTypeManager()->getStorage('view')->loadMultiple();
  /** @var \Drupal\views\ViewEntityInterface $view */
  foreach ($views as $view) {
    foreach ($view->get('display') as $display) {
      $key = $view->id() . '=' . $display['id'];
      $info['tokens']['llm_txt_views'][$key] = [
        'name' => $view->label() . ' ' . $display['display_title'],
      ];
    }
  }
  if ($type === 'llm_txt_views') {
    foreach ($tokens as $name => $original) {
      [$view_id, $display_id] = explode('=', $name, 2);
      if (!empty($view_id) && !empty($display_id)) {
        $view = Views::getView($view_id);
        if (!empty($view) && $view->access($display_id)) {
          $view->setDisplay($display_id);
          $view->initHandlers();
          $preview = $view->preview($display_id);
          $html = \Drupal::service('renderer')->renderInIsolation($preview);
          if (\Drupal::moduleHandler()->moduleExists('markdownify')) {
            $base_url = \Drupal::service('router.request_context')->getCompleteBaseUrl();
            $markdown = \Drupal::service('markdownify.html_converter')->convert($html);
            $markdown = preg_replace('/('. preg_quote($base_url, '/') . '[a-zA-Z0-9-_\/]+)/', '${1}.md', $markdown);
            $replacements[$original] = $markdown;
          }
        }
      }
    }
  }

My spidey senses indicates that cacheability information is lost somewhere here, even if nothing in the call chain of $view->access($display_id) bubbles up cacheability information - why is that? probably there is a Core issue for this, nvm for now... - . So I think it would be better to run the complete rendering in a dedicated render context and bubbling up the collected cacheability information, just in case.

markdownify_views is surely needed for this feature. Second guessing a bit but should this feature be included in that module? What is your thought on this @iambatman?

I was also thinking about why views_embed_view() has not been leveraged in the original code, but then I realized that it is not important. The most important part probably doing the same what is done in \Drupal\markdownify_views\Controller\MarkdownifyViewPageController::handle(), or calling that code for consistently generating the markdown version of a Views view.

mxr576’s picture

Maybe recent results in #3548981-4: Output markdown menu links when Markdownify is enabled are just proves that we also do not and cannot serialize any kind of Views view to markdown. Links after the forced transformation may not work.

The special case we are building the ideal examole for transfermarion, maybe we should support those kinds of Views somehow...

Also, the work is being done in #3521569: Allow linking to the md version of an entity could also help with directly generating markdown links for nodes.

mxr576’s picture

Also, the work is being done in #3521569: Allow linking to the md version of an entity could also help with directly generating markdown links for nodes.

Not alone, after that entity views data also has to be altered so the Markdown link would be available in Views field settings. \Drupal\views\EntityViewsData::addEntityLinks()

mxr576’s picture

StatusFileSize
new3.58 KB

Current state of MR#6 without changes on phpstan baseline so it could be applied on the llm_support recipe.

mxr576’s picture

Issue tags: +Needs tests

Test coverage would be great, especially on cache invalidation.

mxr576’s picture

Issue tags: -Needs tests

Due to preparations for DrupalCon, MR#6 was merged.

Created a follow up for test coverage: #3550976: Test coverage for "Add token for rendering a markdown version of a Views view"

mxr576’s picture

Status: Active » Fixed

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.