Skip to content

Commit

Permalink
Merge pull request #32 from smartbooster/markdown_utils
Browse files Browse the repository at this point in the history
Add MarkdownUtils to help formatting markdown content
  • Loading branch information
mathieu-ducrot authored Jun 12, 2024
2 parents 72fd816 + 11e1179 commit 4e13229
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
CHANGELOG for 1.x
===================
## v1.6.0 - (2024-06-12)
### Added
- `MarkdownUtils` to help formatting markdown content before being render as html

## v1.5.0 - (2024-06-10)
### Added
- `ProcessTrait::restartedAt` Used to know when we wanted to retry this process (creating a new dedicated process based on this one)
Expand Down
38 changes: 38 additions & 0 deletions src/Utils/MarkdownUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Smart\CoreBundle\Utils;

use function Symfony\Component\String\u;

/**
* @author Mathieu Ducrot <[email protected]>
*/
class MarkdownUtils
{
/**
* @param string $md '# Heading #{"id": "heading"}'
* @param string $baseUrl 'http://url.test'
* @return string '# <a href="http://url.test#heading" id="heading">Heading</a>'
*/
public static function addAnchorToHeadings(string $md, string $baseUrl): string
{
foreach (u($md)->match('/# .* #{.*}/', PREG_PATTERN_ORDER)[0] as $heading) {
$lastHashIndex = u($heading)->indexOfLast('#');
$text = u($heading)->slice(1, $lastHashIndex - 1)->toString();
$data = json_decode(u($heading)->slice($lastHashIndex + 1)->toString(), true);
$id = $data['id'] ?? null;
if ($id === null) {
continue;
}
$anchoredHeading = sprintf(
'# <a href="%s" id="%s">%s</a>',
$baseUrl . '#' . $id,
$id,
trim($text),
);
$md = u($md)->replace($heading, $anchoredHeading)->toString();
}

return $md;
}
}
93 changes: 93 additions & 0 deletions tests/Utils/MarkdownUtilsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Smart\CoreBundle\Tests\Utils;

use Smart\CoreBundle\Utils\MarkdownUtils;
use PHPUnit\Framework\TestCase;

/**
* vendor/bin/simple-phpunit tests/Utils/MarkdownUtilsTest.php
*/
class MarkdownUtilsTest extends TestCase
{
/**
* @dataProvider addAnchorToHeadingsProvider
*/
public function testAddAnchorToHeadings(string $expected, string $md, string $baseUrl): void
{
$this->assertEquals($expected, MarkdownUtils::addAnchorToHeadings($md, $baseUrl));
}

public function addAnchorToHeadingsProvider(): array
{
$baseUrl = 'http://url.test';

return [
'without heading' => [
'Texte',
'Texte',
$baseUrl,
],
'heading without anchor' => [
'# Heading',
'# Heading',
$baseUrl,
],
'heading with anchor' => [
'# <a href="http://url.test#heading" id="heading">Heading</a>',
'# Heading #{"id": "heading"}',
$baseUrl,
],
'heading with anchor + text before and after which need to be untouched after formatting' => [
<<<EOD
Texte avant
# <a href="http://url.test#heading" id="heading">Heading</a>
Texte Après
EOD,
<<<EOD
Texte avant
# Heading #{"id": "heading"}
Texte Après
EOD,
$baseUrl,
],
'anchored heading with a level superior to h1' => [
<<<EOD
Texte avant
### <a href="http://url.test#heading" id="heading">Heading</a>
Texte Après
EOD,
<<<EOD
Texte avant
### Heading #{"id": "heading"}
Texte Après
EOD,
$baseUrl,
],
'multiple headings with same level' => [
<<<EOD
## <a href="http://url.test#heading-a" id="heading-a">Heading A</a>
## <a href="http://url.test#heading-b" id="heading-b">Heading B</a>
EOD,
<<<EOD
## Heading A #{"id": "heading-a"}
## Heading B #{"id": "heading-b"}
EOD,
$baseUrl,
],
'multiple headings with different level' => [
<<<EOD
# <a href="http://url.test#heading-1" id="heading-1">Heading 1</a>
## <a href="http://url.test#heading-2" id="heading-2">Heading 2</a>
### <a href="http://url.test#heading-3" id="heading-3">Heading 3</a>
EOD,
<<<EOD
# Heading 1 #{"id": "heading-1"}
## Heading 2 #{"id": "heading-2"}
### Heading 3 #{"id": "heading-3"}
EOD,
$baseUrl,
],
];
}
}

0 comments on commit 4e13229

Please sign in to comment.