Use absolute URLs

Created on 14 April 2025, 16 days ago

It would be nice to have an option to transform relative URLs in the content to absolute URLs, for instance:

[my-page](/my-page) => [my-page](https://mysite.com/my-page)

This could increase the probablity to get visitors for AI.

Feature request
Status

Active

Version

1.0

Component

Code

Created by

🇫🇷France GuillaumeDuveau Toulouse

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @GuillaumeDuveau
  • 🇫🇷France GuillaumeDuveau Toulouse

    Actually it's already possible:

    /**
     * Implements hook_markdownify_entity_html_alter().
     */
    function mymodule_markdownify_entity_html_alter(string &$html, array $context): void {
      $entity = $context['entity'];
    
      if ($entity && $entity->getEntityTypeId() === 'node') {
        $url = $entity->toUrl('canonical', ['absolute' => TRUE])->toString();
        $host = parse_url($url, PHP_URL_SCHEME) . '://' . parse_url($url, PHP_URL_HOST);
    
        $html = preg_replace_callback(
          '#(src|href)=["\'](/[^"\']+)["\']#',
          fn($m) => $m[1] . '="' . $host . $m[2] . '"',
          $html
        );
    
        $source = '<p>Source: ' . $url . '</p>';
        $html = $source . $html;
      }
    }
    

    Here I'm adding the source URL and absolute links.

Production build 0.71.5 2024