Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Header links #355

Merged
merged 3 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions app/Markdown/ConfigureHeadingLinks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Markdown;

use League\CommonMark\Event\DocumentParsedEvent;
use League\CommonMark\Extension\CommonMark\Node\Block\Heading;
use League\CommonMark\Extension\CommonMark\Node\Inline\HtmlInline;
use League\CommonMark\Node\Block\Paragraph;

class ConfigureHeadingLinks
{
public function __invoke(DocumentParsedEvent $documentParsedEvent): void
{
$walker = $documentParsedEvent->getDocument()->walker();

while ($event = $walker->next()) {
[$paragraph, $heading, $linkOpener, $linkCloser] = [
$event->getNode(),
$event->getNode()->next(),
$event->getNode()->firstChild(),
$event->getNode()->lastChild(),
];

if (
! $heading instanceof Heading ||
! $paragraph instanceof Paragraph ||
count($paragraph->children()) !== 2 ||
! $linkOpener instanceof HtmlInline ||
! $linkCloser instanceof HtmlInline ||
! str_starts_with($linkOpener->getLiteral(), '<a name="') ||
! str_ends_with($linkOpener->getLiteral(), '">') ||
substr_count($linkOpener->getLiteral(), '"') !== 2 ||
$paragraph->firstChild()->next() !== $paragraph->lastChild() ||
! str_starts_with($linkCloser->getLiteral(), '</a>')
) {
continue;
}

$link = str($linkOpener->getLiteral())->after('<a name="')->beforeLast('">')->toString();

$heading->data->set('attributes.id', $link);
$heading->prependChild(new HtmlInline('<a href="#'.$link.'">'));
$heading->appendChild(new HtmlInline('</a>'));

$paragraph->detach();

$walker->resumeAt($heading->next());
}
}
}
23 changes: 9 additions & 14 deletions app/Markdown/GithubFlavoredMarkdownConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,19 @@

namespace App\Markdown;

use Illuminate\Support\Str;
use Laravel\Unfenced\UnfencedExtension;
use League\CommonMark\Environment\Environment;
use League\CommonMark\Environment\EnvironmentInterface;
use League\CommonMark\Event\DocumentParsedEvent;
use League\CommonMark\Extension\Attributes\AttributesExtension;
use League\CommonMark\Extension\Autolink\AutolinkExtension;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\Extension\CommonMark\Node\Block\BlockQuote;
use League\CommonMark\Extension\Strikethrough\StrikethroughExtension;
use League\CommonMark\Extension\Table\TableExtension;
use League\CommonMark\Extension\TaskList\TaskListExtension;
use League\CommonMark\MarkdownConverter;
use League\CommonMark\Environment\Environment;
use App\Markdown\GithubFlavoredMarkdownExtension;
use Illuminate\Support\Facades\Vite;
use League\CommonMark\Node\Node;
use League\CommonMark\Renderer\ChildNodeRendererInterface;
use League\CommonMark\Util\HtmlElement;
use Torchlight\Commonmark\V2\TorchlightExtension;
use Laravel\Unfenced\UnfencedExtension;
use League\CommonMark\Environment\EnvironmentInterface;
use League\CommonMark\Extension\Attributes\AttributesExtension;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\Extension\CommonMark\Node\Block\BlockQuote;
use League\CommonMark\Renderer\NodeRendererInterface;

/**
* Converts GitHub Flavored Markdown to HTML.
Expand All @@ -44,7 +38,8 @@ public function __construct(array $config = [])
$environment->addExtension(new AttributesExtension());
$environment->addExtension(new TorchlightExtension());

$environment->addRenderer(BlockQuote::class, new BlockQuoteRenderer());
$environment->addRenderer(BlockQuote::class, new BlockQuoteRenderer);
$environment->addEventListener(DocumentParsedEvent::class, new ConfigureHeadingLinks);

parent::__construct($environment);
}
Expand Down
6 changes: 6 additions & 0 deletions public/build/assets/docs-143568ce.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading