-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from smartbooster/markdown_utils
Add MarkdownUtils to help formatting markdown content
- Loading branch information
Showing
3 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
], | ||
]; | ||
} | ||
} |