Skip to content

Commit

Permalink
imp: Make the tags in comments clickable
Browse files Browse the repository at this point in the history
  • Loading branch information
marienfressinaud committed Oct 17, 2024
1 parent 4b63e01 commit fc648e3
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions src/utils/MiniMarkdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\utils;

use App\services;

/**
* An extension to the Parsedown library.
*
Expand Down Expand Up @@ -30,8 +32,55 @@ class MiniMarkdown extends \Parsedown

public function __construct()
{
$this->setSafeMode(true)
->setBreaksEnabled(true);
$this->setSafeMode(true);
$this->setBreaksEnabled(true);

$this->InlineTypes['#'][] = 'Tag';

$this->inlineMarkerList .= '#';
}

/**
* Make sure to block header tag so it doesn't conflict with lines starting
* with a #tag.
*
* @param array{body: string, indent: int, text: string} $line
*
* @return ?array<string, mixed>
*/
protected function blockHeader($line): ?array
{
return null;
}

/**
* @param array{text: string, context: string} $excerpt
*
* @return ?array<string, mixed>
*/
protected function inlineTag(array $excerpt): ?array
{
$result = preg_match(services\LinkTags::TAG_REGEX, $excerpt['text'], $matches);

if ($result) {
$tag = $matches['tag'];
$tag_url = \Minz\Url::for('links', [
'q' => "#{$tag}",
]);

return array(
'extent' => strlen($tag) + 1,
'element' => array(
'name' => 'a',
'text' => "#{$tag}",
'attributes' => array(
'href' => $tag_url,
),
),
);
} else {
return null;
}
}

/**
Expand Down

0 comments on commit fc648e3

Please sign in to comment.