Replies: 2 comments 1 reply
-
I think your best bet would be to implement custom parsers that looks for that alternative syntax and parses them as-is to ensure that other parsers don't handle them. |
Beta Was this translation helpful? Give feedback.
1 reply
-
Here are the files I made so far, heavily using the strikethrough extension... LaTeX.php <?php
namespace App\Scolcours\commonmark\latex;
use League\CommonMark\Node\Inline\AbstractInline;
use League\CommonMark\Node\Inline\DelimitedInterface;
final class LaTeX extends AbstractInline implements DelimitedInterface
{
private string $delimiter;
public function __construct(string $delimiter = '\\(')
{
parent::__construct();
$this->delimiter = $delimiter;
}
public function getOpeningDelimiter(): string
{
return '\\(';
}
public function getClosingDelimiter(): string
{
return '\\)';
}
} LaTeXinlineDelimiterProcessor.php <?php
namespace App\Scolcours\commonmark\latex;
use League\CommonMark\Delimiter\DelimiterInterface;
use League\CommonMark\Delimiter\Processor\DelimiterProcessorInterface;
use League\CommonMark\Node\Inline\AbstractStringContainer;
final class LaTeXinlineDelimiterProcessor implements DelimiterProcessorInterface
{
public function getOpeningCharacter(): string
{
return '\\(';
}
public function getClosingCharacter(): string
{
return '\\)';
}
public function getMinLength(): int
{
return 1;
}
public function getDelimiterUse(DelimiterInterface $opener, DelimiterInterface $closer): int
{
$min = \min($opener->getLength(), $closer->getLength());
return $min >= 1 ? $min : 0;
}
public function process(AbstractStringContainer $opener, AbstractStringContainer $closer, int $delimiterUse): void
{
$latex = new LaTeX();
$tmp = $opener->next();
while ($tmp !== null && $tmp !== $closer) {
$next = $tmp->next();
$latex->appendChild($tmp);
$tmp = $next;
}
$opener->insertAfter($latex);
}
} LaTeXRenderer.php <?php
namespace App\Scolcours\commonmark\latex;
use League\CommonMark\Node\Node;
use League\CommonMark\Renderer\ChildNodeRendererInterface;
use League\CommonMark\Renderer\NodeRendererInterface;
use League\CommonMark\Util\HtmlElement;
use League\CommonMark\Xml\XmlNodeRendererInterface;
final class LaTeXRenderer implements NodeRendererInterface, XmlNodeRendererInterface
{
/**
* @param LaTeX $node
*
* {@inheritDoc}
*
* @psalm-suppress MoreSpecificImplementedParamType
*/
public function render(Node $node, ChildNodeRendererInterface $childRenderer): \Stringable
{
LaTeX::assertInstanceOf($node);
return new HtmlElement('div',
$node->data->get('attributes'),
$childRenderer->renderNodes($node->children())
);
}
public function getXmlTagName(Node $node): string
{
return 'math';
}
/**
* {@inheritDoc}
*/
public function getXmlAttributes(Node $node): array
{
return [];
}
} and LaTeXExtension.php <?php
namespace App\Scolcours\commonmark\latex;
use League\CommonMark\Environment\EnvironmentBuilderInterface;
use League\CommonMark\Extension\ExtensionInterface;
final class LaTeXExtension implements ExtensionInterface {
public function register( EnvironmentBuilderInterface $environment ): void {
$environment->addDelimiterProcessor( new LaTeXinlineDelimiterProcessor() );
$environment->addRenderer(LaTeX::class, new LaTeXRenderer());
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
I would like to disable parsing between LaTeX default delimiters. The math rendering is done using javascript (KaTeX).
The default delimiters are:
The delimiters with the dollar signs are working, but:
Is there an easy way to make everything between these delimiters untouched (and also keeping the delimiters) ?
Thanks,
Basil
Beta Was this translation helpful? Give feedback.
All reactions