Skip to content

Commit aa0eb2a

Browse files
committed
New MarkDown parser
1 parent 3e19c39 commit aa0eb2a

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
"phpfui/phpfui": "^6.0",
1717
"phpdocumentor/reflection-docblock": "^5.0",
1818
"gitonomy/gitlib": "^1.2",
19-
"cebe/markdown": "^1.2",
2019
"voku/simple_html_dom": "^4.7",
2120
"soundasleep/html2text": "^2.0",
22-
"scrivo/highlight.php": ">=v9.18"
21+
"scrivo/highlight.php": ">=v9.18",
22+
"league/commonmark": "^2.4"
2323
},
2424
"autoload": {
2525
"psr-4": {"PHPFUI\\InstaDoc\\": "src/PHPFUI/InstaDoc/"}

src/PHPFUI/InstaDoc/MarkDownParser.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@
44

55
class MarkDownParser
66
{
7-
private \cebe\markdown\GithubMarkdown $parser;
7+
private \League\CommonMark\GithubFlavoredMarkdownConverter $parser;
88

99
public function __construct()
1010
{
11-
$this->parser = new \cebe\markdown\GithubMarkdown();
12-
$this->parser->html5 = true;
13-
$this->parser->keepListStartNumber = true;
14-
$this->parser->enableNewlines = true;
11+
$this->parser = new \League\CommonMark\GithubFlavoredMarkdownConverter(['html_input' => 'strip', 'allow_unsafe_links' => false, ]);
1512
}
1613

1714
public function fileText(string $filename) : string
@@ -23,17 +20,20 @@ public function fileText(string $filename) : string
2320

2421
public function html(string $markdown) : string
2522
{
26-
return $this->parser->parseParagraph(\str_replace(['<p>', '</p>'], '', $markdown));
23+
$markdown = \str_replace('<?php', '', $markdown);
24+
$html = $this->parser->convert($markdown);
25+
26+
return \str_replace(['<p>', '</p>'], '', "{$html}");
2727
}
2828

2929
public function text(string $markdown) : string
3030
{
31-
$position = 0;
31+
$markdown = \str_replace('<?php', '', $markdown);
3232
$hl = new \Highlight\Highlighter();
3333

3434
$div = new \PHPFUI\HTML5Element('div');
3535
$div->addClass('markdown-body');
36-
$html = $this->parser->parse($markdown);
36+
$html = "{$this->parser->convert($markdown)}";
3737
$dom = new \voku\helper\HtmlDomParser($html);
3838
$codeBlocks = $dom->find('.language-php');
3939

@@ -47,6 +47,6 @@ public function text(string $markdown) : string
4747
}
4848
$div->add("{$dom}");
4949

50-
return $div;
50+
return "{$div}";
5151
}
5252
}

tests/MDTest.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the PHPFUI package
5+
*
6+
* (c) Bruce Wells
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE.md file that was distributed with this source
10+
* code
11+
*/
12+
class MDTest extends \PHPFUI\HTMLUnitTester\Extensions
13+
{
14+
public function testMarkdownFiles() : void
15+
{
16+
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__ . '/../vendor'));
17+
18+
$parser = new \PHPFUI\InstaDoc\MarkDownParser();
19+
20+
$ignoreDirectories = ['devbridge', ];
21+
22+
foreach ($iterator as $file)
23+
{
24+
foreach ($ignoreDirectories as $directory)
25+
{
26+
if (\str_contains($file->getPathname(), $directory))
27+
{
28+
continue 2;
29+
}
30+
}
31+
$fileName = \strtolower($file->getFilename());
32+
33+
if ($file->isFile() && \str_ends_with($fileName, '.md'))
34+
{
35+
$html = $parser->fileText($file->getPathname());
36+
$this->assertNotWarningHtml($html, "File {$file->getPathname()} has HTML warnings");
37+
$this->assertValidHtml($html, "File {$file->getPathname()} has HTML errors");
38+
}
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)