Add id links to the headers and tweak link formatting

This commit is contained in:
Nathan McCarty 2025-07-12 01:25:24 -04:00
parent 5880e12b82
commit 2afc2b9d55
3 changed files with 23 additions and 2 deletions

View file

@ -100,10 +100,21 @@ sub markdown-first-paragraph(IO::Path:D $file --> Str:D) is export {
$para
}
my regex header { '<h' $<level>=(\d) \s+ 'id="' $<id>=(<-["]>+) '">' $<content>=(<-[<]>+) '</h' $<level> '>'};
#| Use pandoc to render a markdown document to html
sub markdown-to-html(IO::Path:D $file --> Str:D) is export {
# Remove the header, we'll regenerate it later
# Have pandoc do its thing
my $output = pandoc <-f gfm>, $file;
# Remove the header, we'll regenerate it later
$output ~~ s:g/'<h1' .* '</h1>'//;
# Make all headers links to themselves
$output ~~ s:g[<header>] =
do given $<header> {
my $link = "<a href=\"#$_<id>\">{$_<content>}</a>";
my $header ="<h{$_<level>} class=\"heading-id-link\">{$link}</h{$_<level>}>";
$header
};
$output
}