Skip to content

Commit

Permalink
Renderer: Add support for LaTeX.
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek committed Nov 6, 2019
1 parent f159d1b commit 04a5de2
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions src/CommonMarkRenderer.php
Expand Up @@ -10,6 +10,14 @@
class CommonMarkRenderer extends BaseRenderer
{

/**
* @var string[]
*/
private static $helpers = [
'\(' => 'LATEX-L',
'\)' => 'LATEX-R',
];

/**
* @var ConverterAccessor
*/
Expand All @@ -35,15 +43,17 @@ public function render(string $content): string

if (isset($cache[$content]) === false) {
$html = $this->process(
$this->commonMarkConverter->get()->convertToHtml($content)
$this->commonMarkConverter->get()->convertToHtml(
$this->beforeProcess($content)
)
);

$html = preg_replace_callback(
'/src="\/?(static\/([^"]+))"/',
function (array $match): string {
return 'src="' . Helpers::getBaseUrl() . '/' . $match[1] . '"';
},
$html
$this->afterProcess($html)
);

$cache[$content] = $html;
Expand All @@ -52,4 +62,30 @@ function (array $match): string {
return $cache[$content];
}

}
/**
* @param string $haystack
* @return string
*/
private function beforeProcess(string $haystack): string
{
foreach (self::$helpers as $key => $value) {
$haystack = str_replace($key, $value, $haystack);
}

return $haystack;
}

/**
* @param string $haystack
* @return string
*/
private function afterProcess(string $haystack): string
{
foreach (self::$helpers as $key => $value) {
$haystack = str_replace($value, $key, $haystack);
}

return $haystack;
}

}

0 comments on commit 04a5de2

Please sign in to comment.