From b7fa018718feccb08c6e5200f94a299bac10cea1 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 11 Nov 2023 19:53:30 +0000 Subject: [PATCH] Improve performance by checking for trigger char --- Utils/Parser/Markdown/Markdown.php | 51 +++++++++++++++++++--- tests/Utils/Parser/Markdown/data/mark.html | 2 +- tests/Utils/Parser/Markdown/data/mark.md | 2 +- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/Utils/Parser/Markdown/Markdown.php b/Utils/Parser/Markdown/Markdown.php index b19f59223..a38fad5cb 100755 --- a/Utils/Parser/Markdown/Markdown.php +++ b/Utils/Parser/Markdown/Markdown.php @@ -405,6 +405,25 @@ class Markdown */ private static $instances = []; + /** + * Clean up state + * + * @return void + * + * @since 1.0.0 + */ + public function clean() : void + { + $this->definitionData = []; + $this->contentsListArray = []; + $this->contentsListString = ''; + $this->firstHeadLevel = 0; + $this->anchorDuplicates = []; + $this->footnoteCount = 0; + $this->currentAbreviation = ''; + $this->currentMeaning = ''; + } + /** * Create instance for static use * @@ -414,9 +433,12 @@ class Markdown * * @since 1.0.0 */ - public static function instance(string $name = 'default') : self + public static function getInstance(string $name = 'default') : self { if (isset(self::$instances[$name])) { + $obj = self::$instances[$name]; + $obj->clean(); + return self::$instances[$name]; } @@ -548,7 +570,7 @@ class Markdown */ public static function parse(string $text) : string { - $parsedown = new self(); + $parsedown = self::getInstance(); return $parsedown->text($text); } @@ -1220,7 +1242,10 @@ class Markdown */ protected function inlineMark(array $excerpt) : ?array { - if (\preg_match('/^(==)([^=]*?)(==)/', $excerpt['text'], $matches) !== 1) { + if (!isset($excerpt['text'][1]) + || $excerpt['text'][1] !== '=' + || \preg_match('/^(==)([^=]*?)(==)/', $excerpt['text'], $matches) !== 1 + ) { return null; } @@ -1244,7 +1269,10 @@ class Markdown */ protected function inlineSpoiler(array $excerpt) : ?array { - if (\preg_match('/^>!(.*?)!!(.*?)!options['embeding'] ?? false) + || !isset($excerpt['text'][1]) + || ($excerpt['text'][1] !== 'v' && $excerpt['text'][1] !== 'a') || (!($video = (\preg_match('/\[video.*src="([^"]*)".*\]/', $excerpt['text'], $matches) === 1)) && !($audio = (\preg_match('/\[audio.*src="([^"]*)".*\]/', $excerpt['text'], $matches) === 1))) ) { @@ -1400,6 +1433,8 @@ class Markdown protected function inlineMap(array $excerpt) : ?array { if (!($this->options['map'] ?? false) + || !isset($excerpt['text'][1]) + || $excerpt['text'][1] !== 'm' || (\preg_match('/\[map(?:\s+(?:name="([^"]+)"|country="([^"]+)"|city="([^"]+)"|zip="([^"]+)"|address="([^"]+)"|lat="([^"]+)"|lon="([^"]+)")){0,7}\]/', $excerpt['text'], $matches) !== 1) ) { return null; @@ -1445,6 +1480,8 @@ class Markdown protected function inlineAddress(array $excerpt) : ?array { if (!($this->options['address'] ?? false) + || !isset($excerpt['text'][1]) + || $excerpt['text'][1] !== 'a' || (\preg_match('/\[addr(?:\s+(?:name="([^"]+)"|country="([^"]+)"|city="([^"]+)"|zip="([^"]+)"|address="([^"]+)")){0,5}\]/', $excerpt['text'], $matches) !== 1) ) { return null; @@ -1507,6 +1544,8 @@ class Markdown protected function inlineContact(array $excerpt) : ?array { if (!($this->options['contact'] ?? false) + || !isset($excerpt['text'][1]) + || $excerpt['text'][1] !== 'c' || (\preg_match('/\[contact.*?([a-zA-Z]+)="(.*?)"\]/', $excerpt['text'], $matches) !== 1) ) { return null; @@ -1590,6 +1629,8 @@ class Markdown protected function inlineProgress(array $excerpt) : ?array { if (!($this->options['progress'] ?? false) + || !isset($excerpt['text'][1]) + || $excerpt['text'][1] !== 'p' || (\preg_match('/\[progress(?:\s+(?:type="([^"]+)"|percent="([^"]+)"|value="([^"]+)")){0,3}\]/', $excerpt['text'], $matches) !== 1) ) { return null; diff --git a/tests/Utils/Parser/Markdown/data/mark.html b/tests/Utils/Parser/Markdown/data/mark.html index 111e911cf..2b6624130 100644 --- a/tests/Utils/Parser/Markdown/data/mark.html +++ b/tests/Utils/Parser/Markdown/data/mark.html @@ -1 +1 @@ -

Mark test

\ No newline at end of file +

Text with Mark test inline.

\ No newline at end of file diff --git a/tests/Utils/Parser/Markdown/data/mark.md b/tests/Utils/Parser/Markdown/data/mark.md index 3f6a3099b..efefef266 100644 --- a/tests/Utils/Parser/Markdown/data/mark.md +++ b/tests/Utils/Parser/Markdown/data/mark.md @@ -1 +1 @@ -==Mark test== \ No newline at end of file +Text with ==Mark test== inline. \ No newline at end of file