Skip to content

Commit 8aaf447

Browse files
committed
Use our own markdown_to_html convertor
Which stashes LaTeX during conversion and pops it afterwards.
1 parent bc44652 commit 8aaf447

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

webapp/src/Twig/TwigExtension.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use Twig\Environment;
3838
use Twig\Extension\AbstractExtension;
3939
use Twig\Extension\GlobalsInterface;
40+
use Twig\Extra\Markdown\MarkdownRuntime;
4041
use Twig\Runtime\EscaperRuntime;
4142
use Twig\TwigFilter;
4243
use Twig\TwigFunction;
@@ -124,6 +125,7 @@ public function getFilters(): array
124125
new TwigFilter('medalType', $this->awards->medalType(...)),
125126
new TwigFilter('numTableActions', $this->numTableActions(...)),
126127
new TwigFilter('extensionToMime', $this->extensionToMime(...)),
128+
new TwigFilter('domjudge_markdown_to_html', $this->domjudgeMarkdownToHTML(...), ['is_safe' => ['html']]),
127129
];
128130
}
129131

@@ -1406,4 +1408,35 @@ public function extensionToMime(string $extension): string
14061408
{
14071409
return DOMJudgeService::EXTENSION_TO_MIMETYPE[$extension];
14081410
}
1411+
1412+
public function domjudgeMarkdownToHTML(string $markdown): string
1413+
{
1414+
$latexFound = [];
1415+
while(true) {
1416+
$start = strpos($markdown,'$$');
1417+
$end = strpos(substr($markdown, $start+2), '$$');
1418+
if ($start === false || $end === false) {
1419+
break;
1420+
}
1421+
$latexFound[] = substr($markdown, $start, $end+2+2);
1422+
$newMarkdown = substr($markdown, 0, $start);
1423+
$newMarkdown .= '$LaTeX$';
1424+
$newMarkdown .= substr($markdown, $end+$start+4);
1425+
$markdown = $newMarkdown;
1426+
}
1427+
1428+
/** @var MarkdownRuntime $runtime */
1429+
$runtime = $this->twig->getRuntime(MarkdownRuntime::class);
1430+
$markdown = (string)$runtime->convert($markdown);
1431+
1432+
$new = '';
1433+
foreach ($latexFound as $inlineLatex) {
1434+
$replacedStart = strpos($markdown,'$LaTeX$');
1435+
$new = substr($markdown, 0, $replacedStart);
1436+
$new .= $inlineLatex;
1437+
$new .= substr($markdown, $replacedStart+strlen('$LaTeX$'));
1438+
$markdown = $new;
1439+
}
1440+
return $markdown;
1441+
}
14091442
}

0 commit comments

Comments
 (0)