Skip to content

Commit 66bb399

Browse files
authored
Use one environment for Markdown parsing
This provides a massive speed difference when parsing the API, as it is no longer creating the environment each time, and doesn't fire off the Markdown events in Winter.
1 parent 2299624 commit 66bb399

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

classes/PHPApiParser.php

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
namespace Winter\Docs\Classes;
44

5+
use League\CommonMark\Environment\Environment;
6+
use League\CommonMark\Extension\Autolink\AutolinkExtension;
7+
use League\CommonMark\Extension\DisallowedRawHtml\DisallowedRawHtmlExtension;
8+
use League\CommonMark\Parser\MarkdownParser;
9+
use League\CommonMark\Renderer\HtmlRenderer;
510
use phpDocumentor\Reflection\DocBlock\Tags\Author;
611
use phpDocumentor\Reflection\DocBlock\Tags\Generic;
712
use phpDocumentor\Reflection\DocBlock\Tags\InvalidTag;
@@ -22,8 +27,10 @@
2227
use PhpParser\NodeFinder;
2328
use PhpParser\ParserFactory;
2429
use Symfony\Component\Finder\Glob;
30+
use Winter\Storm\Parse\Markdown\CommonMarkCoreExtension;
31+
use Winter\Storm\Parse\Markdown\StrikethroughExtension;
2532
use Winter\Storm\Support\Arr;
26-
use Winter\Storm\Support\Facades\Markdown;
33+
2734
/**
2835
* PHP API Parser.
2936
*
@@ -66,6 +73,15 @@ class PHPApiParser
6673
/** Factory instance for generating DocBlock reflections */
6774
protected DocBlockFactory $docBlockFactory;
6875

76+
/** Markdown parsing environment */
77+
protected Environment $markdownEnvironment;
78+
79+
/** Markdown parser */
80+
protected MarkdownParser $markdownParser;
81+
82+
/** Markdown renderer */
83+
protected HtmlRenderer $markdownRenderer;
84+
6985
/**
7086
* Constructor.
7187
*/
@@ -2124,11 +2140,28 @@ protected function sortDefinitions(array &$class)
21242140
*/
21252141
protected function renderMarkdown(string $markdown): string
21262142
{
2143+
if (!isset($this->markdownEnvironment)) {
2144+
$this->createMarkdownEnvironment();
2145+
}
2146+
21272147
try {
2128-
$content = Markdown::parse($markdown);
2129-
return trim($content);
2148+
$ast = $this->markdownParser->parse($markdown);
2149+
return trim($this->markdownRenderer->renderDocument($ast));
21302150
} catch (\Throwable $e) {
21312151
return '';
21322152
}
21332153
}
2154+
2155+
protected function createMarkdownEnvironment()
2156+
{
2157+
$markdown = new Environment();
2158+
$markdown->addExtension(new CommonMarkCoreExtension);
2159+
$markdown->addExtension(new StrikethroughExtension);
2160+
$markdown->addExtension(new AutolinkExtension);
2161+
$markdown->addExtension(new DisallowedRawHtmlExtension);
2162+
2163+
$this->markdownEnvironment = $markdown;
2164+
$this->markdownParser = new MarkdownParser($this->markdownEnvironment);
2165+
$this->markdownRenderer = new HtmlRenderer($this->markdownEnvironment);
2166+
}
21342167
}

0 commit comments

Comments
 (0)