Skip to content

[Site] refactor commonmark config #1791

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

Merged
merged 1 commit into from
Apr 26, 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
6 changes: 0 additions & 6 deletions ux.symfony.com/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,3 @@ services:

# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones

App\Service\CommonMark\CodeExtension:
tags: ['twig.markdown.league_extension']

League\CommonMark\Extension\ExternalLink\ExternalLinkExtension:
tags: ['twig.markdown.league_extension']
27 changes: 0 additions & 27 deletions ux.symfony.com/src/Service/CommonMark/CodeExtension.php

This file was deleted.

51 changes: 51 additions & 0 deletions ux.symfony.com/src/Service/CommonMark/ConverterFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Service\CommonMark;

use League\CommonMark\CommonMarkConverter;
use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode;
use League\CommonMark\Extension\ExternalLink\ExternalLinkExtension;
use League\CommonMark\Extension\Mention\MentionExtension;
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
#[AsDecorator('twig.markdown.league_common_mark_converter_factory')]
final class ConverterFactory
{
public function __invoke(): CommonMarkConverter
{
$converter = new CommonMarkConverter([
'mentions' => [
'github_handle' => [
'prefix' => '@',
'pattern' => '[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}(?!\w)',
'generator' => 'https://github.com/%s',
],
'github_issue' => [
'prefix' => '#',
'pattern' => '\d+',
'generator' => 'https://github.com/symfony/ux/issues/%d',
],
],
]);

$converter->getEnvironment()
->addRenderer(FencedCode::class, new CodeRenderer(), 10)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Soon Tempest's highlighting 👀

->addExtension(new ExternalLinkExtension())
->addExtension(new MentionExtension())
;

return $converter;
}
}
15 changes: 3 additions & 12 deletions ux.symfony.com/src/Twig/Components/ChangelogItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,12 @@ private function formatContent(string $content): string
// Replace "## " with "### "
$content = preg_replace('/^## /m', '### ', $content);

// Replace #1234 with a mardown link to the issue
$content = preg_replace('/#(\d+)/', '[#$1](https://github.com/symfony/ux/issues/$1)', $content);
// Replace "https://github.com/symfony/ux/pull/x" with "#x"
$content = preg_replace('#https://github.com/symfony/ux/pull/(\d+)/?#', '#$1', $content);

// Replace "in https://github.com/symfony/ux/pull/1234" with a mardown link to the PR
$content = preg_replace('#in https://github.com/symfony/ux/pull/(\d+)/?#', 'in [#$1](https://github.com/symfony/ux/issues/$1)', $content);

// Replace "https://github.com/symfony/ux/compare/v2.14.1...v2.14.2" with a mardown link to the full changelog
// Replace "https://github.com/symfony/ux/compare/v2.14.1...v2.14.2" with a markdown link to the full changelog
$content = preg_replace('#(https://github.com/symfony/ux/compare/(v(\d+\.\d+\.\d+))...(v(\d+\.\d+\.\d+)))#', '[$2 -> $4]($1)', $content);

// Insert markdown link to the user's Github profile
// ...in: "by @weaverryan in "
$content = preg_replace('/by @([a-zA-Z0-9_]+) in /', 'by [@$1](https://github.com/$1) in ', $content);
// ...in: "@weaverryan made their first "
$content = preg_replace('/@([a-zA-Z0-9_]+) made their first /', '[@$1](https://github.com/$1) made their first ', $content);

// Shorten "made their first contribution in" to "in"
$content = preg_replace('/made their first contribution in/', 'in', $content);

Expand Down
8 changes: 0 additions & 8 deletions ux.symfony.com/tests/Twig/Components/ChangelogItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,6 @@ public static function provideContentValues(): iterable
'### Title 3',
'### Title 3',
];
yield 'inject_#issue_link' => [
'Fixes #1234',
'Fixes [#1234](https://github.com/symfony/ux/issues/1234)',
];
yield 'inject_@user_link' => [
'by @weaverryan in ',
'by [@weaverryan](https://github.com/weaverryan) in ',
];
yield 'inject_changelog_link' => [
'https://github.com/symfony/ux/compare/v2.14.1...v2.14.2',
'[v2.14.1 -> v2.14.2](https://github.com/symfony/ux/compare/v2.14.1...v2.14.2)',
Expand Down