Skip to content

Commit

Permalink
Merge pull request #55 from mrgeneralgoo/2.0
Browse files Browse the repository at this point in the history
Release 2.3.0
  • Loading branch information
mrgeneralgoo authored Jun 15, 2024
2 parents f3741ac + b7a63bf commit afe8e14
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 170 deletions.
24 changes: 4 additions & 20 deletions MarkdownParse.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,6 @@ public function parse(string $text, array $config = []): string

$environment = new Environment(array_merge($this->getConfig(), $config));

$environment->addEventListener(DocumentPreRenderEvent::class, function (DocumentPreRenderEvent $event) {
$document = $event->getDocument();
$matchingNodes = (new Query())
->where(Query::type(FencedCode::class))
->andWhere(function (Node $node): bool {
return $node->getInfo() === 'mermaid';
})
->findAll($document);

foreach ($matchingNodes as $node) {
$divNode = new Text('<div class="' . $node->getInfo() . '">' . $node->getLiteral() . '</div>');
// foreach ($node->children() as $child) {
// $divNode->appendChild($child);
// }
$node->replaceWith($divNode);
}
});

$this->addCommonMarkExtensions($environment);

$htmlContent = (new MarkdownConverter($environment))->convert($text)->getContent();
Expand Down Expand Up @@ -149,7 +131,10 @@ public function preParse(string $text, array $config = []): array
*/
public function postParse(string $htmlContent, array $config = []): array
{
$htmlContent = htmlspecialchars_decode($htmlContent);
// If Mermaid is needed, replace the class attribute of Mermaid code blocks
if ($this->isNeedMermaid) {
$htmlContent = str_replace(['<code class="language-mermaid">'], '<code class="mermaid">', $htmlContent);
}

// If LaTeX is needed, remove <div> tags added during preParse
if ($this->isNeedLaTex) {
Expand Down Expand Up @@ -186,7 +171,6 @@ public function getConfig(): array
$infoWords = $node->getInfoWords();
if (\count($infoWords) !== 0 && $infoWords[0] === 'mermaid') {
$instance->setIsNeedMermaid(true);
return 'mermaid';
}
return null;
},
Expand Down
4 changes: 2 additions & 2 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @author mrgeneral
* @package MarkdownParse
* @version 2.2.0
* @version 2.3.0
* @link https://www.chengxiaobai.cn/
*/
class Plugin implements PluginInterface
Expand Down Expand Up @@ -102,7 +102,7 @@ public static function resourceLink()
}

if ($isAvailableMathjax) {
$resourceContent .= '<script type="text/javascript">(function(){MathJax={tex:{inlineMath:[[\'$\',\'$\'],[\'\\\\(\',\'\\\\)\']]}}})();</script>';
$resourceContent .= '<script type="text/javascript">(function(){MathJax={loader: {load: [\'[tex]/gensymb\']},tex:{inlineMath:[[\'$\',\'$\'],[\'\\\\(\',\'\\\\)\']],packages: {\'[+]\': [\'gensymb\']}}}})();</script>';
$resourceContent .= '<script defer src="https://polyfill.alicdn.com/v3/polyfill.min.js?features=es6"></script>';
$resourceContent .= sprintf('<script id="MathJax-script" defer type="text/javascript" src="%s"></script>', self::CDN_SOURCE_MATHJAX[$configCDN] ?: self::CDN_SOURCE_MATHJAX[self::CDN_SOURCE_DEFAULT]);
}
Expand Down
Loading

0 comments on commit afe8e14

Please sign in to comment.