On Escaping the Complexities of Internal Linking Automation

Why the placeholder method is no longer ideal for linking automation in page content.

As with the current state of the page hook system, Mecha still relies on the block features and simple string patterns in page’s content and properties to generate automated internal links.

The following patterns are very common if you have previously used Mecha as your blog’s content management system:

<a href="[‌[‌url‌]‌]/article/lorem-ipsum">
  this is an internal link
<a href="%‌{‌url‌}‌%/article/lorem-ipsum">
  this is an internal link

The initial purpose of this practice was actually to make it easier when one day you want to export your blog data to another domain address or folder path. Without doing this, to write links prefixed with the blog’s root domain address as-is in the article content will make your old blog data difficult to maintain in the future. Because when it’s time to change the domain address or site folder location, your old links in the article content can’t be changed automatically following the new blog domain address and/or folder location.

In addition, block patterns such as in the example above likely will not be accepted by the link input validator because they are not valid link patterns anyway.

Image of invalid URL pattern in Panel.
This sucks!

People actually have long enough to accept the method of writing relative links to maintain the validity of old internal links in their blog articles. Therefore, I would like to announce that I will be adding a built-in extension that doesn’t rely on block or candy extension in the next release of Mecha which works to resolve relative links in the HTML output.

That way, you can focus on writing links relative to the root domain on the back-end side and Mecha will handle how those links will be resolved in the output, regardless of the blog’s domain address and folder location. This extension shouldn’t be complicated to make. I will include it in the core application and give it a name link.

The addition of this feature will also make it easier for developers to build GUI systems (for example, to build a link or an image input in a rich text editor application) as developers don’t have to bother with tweaking the link validator and URL parser to allow block pattern in a URL.

Maybe this code can give you a little idea of what I’m going to do:

Hook::set('content', function ($content) {
    if (!$content || !str_contains($content, '</a>')) {
        return $content;
    return preg_replace_callback('/<a(\s[^>]*?)?>/', function ($m) {
        $anchor = new HTML($m[0]);
        if ($href = $anchor['href']) {
            $anchor['href'] = URL::long($href, false);
            return (string) $anchor;
        return $m[0];
    }, $content);

1 Comment


Test comment.