Tools For Better Developers

Done: 20.4

2022 July Done

3 weeks ago ∙ 1 minute read

Effort measurement / estimation process

A blog post.

Relative link rendering, including the table of contents, in blog post abstracts

Currently, {{ toc }} generates anchor links, for example #test. On the blog post page, post.html it becomes an absolute link post.html#test.

On the list page, /, it becomes an incorrect link /#test.

Later. One more problem. On the list page, abstracts are links, and having a link inside a link is bad HTML. The solution is not to make a link if abstract contains inner links.

It should better generate post.html#test.

Here is the fix:

# Osm\Data\Markdown\Placeholder\Toc
public function render(File $file): ?string
{

    $markdown = '';
    foreach ($file->toc as $urlKey => $tocEntry) {
        $markdown .= str_repeat(' ', ($tocEntry->depth - 2) * 4)
            . "* [" . $tocEntry->title . "]({$file->url}#{$urlKey})\n";
    }
    return "{$markdown}\n";
}