mirror of
https://github.com/typemill/typemill.git
synced 2025-08-08 23:26:34 +02:00
version 1.1.5 Typemill Learns Math
This commit is contained in:
@@ -2,12 +2,19 @@
|
||||
|
||||
namespace Typemill\Extensions;
|
||||
|
||||
use \URLify;
|
||||
|
||||
class ParsedownExtension extends \ParsedownExtra
|
||||
{
|
||||
function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
# mathjax support
|
||||
$this->InlineTypes['`'][] = 'MathJaxLaTeX';
|
||||
$this->BlockTypes['`'][] = 'FencedMathJaxLaTeX';
|
||||
|
||||
# table of content support
|
||||
array_unshift($this->BlockTypes['['], 'TableOfContents');
|
||||
}
|
||||
|
||||
@@ -70,12 +77,9 @@ class ParsedownExtension extends \ParsedownExtra
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#
|
||||
# Header
|
||||
|
||||
private $headlines = array();
|
||||
private $headlinesCount = 0;
|
||||
|
||||
protected function blockHeader($Line)
|
||||
{
|
||||
@@ -95,15 +99,15 @@ class ParsedownExtension extends \ParsedownExtra
|
||||
|
||||
$text = trim($Line['text'], '# ');
|
||||
|
||||
$this->headlinesCount++;
|
||||
|
||||
$headline = URLify::filter($Line['text']);
|
||||
|
||||
$Block = array(
|
||||
'element' => array(
|
||||
'name' => 'h' . min(6, $level),
|
||||
'text' => $text,
|
||||
'handler' => 'line',
|
||||
'attributes' => array(
|
||||
'id' => "headline-$this->headlinesCount"
|
||||
'id' => "$headline"
|
||||
)
|
||||
)
|
||||
);
|
||||
@@ -323,5 +327,127 @@ class ParsedownExtension extends \ParsedownExtra
|
||||
# ~
|
||||
|
||||
return $markup;
|
||||
}
|
||||
|
||||
# math support. Check https://github.com/aidantwoods/parsedown/blob/mathjaxlatex/ParsedownExtensionMathJaxLaTeX.php
|
||||
|
||||
|
||||
protected function inlineCode($Excerpt)
|
||||
{
|
||||
$marker = $Excerpt['text'][0];
|
||||
if (preg_match('/^('.$marker.')[ ]*(.+?)[ ]*(?<!'.$marker.')\1(?!'.$marker.')/s', $Excerpt['text'], $matches))
|
||||
{
|
||||
$text = $matches[2];
|
||||
$text = preg_replace("/[ ]*\n/", ' ', $text);
|
||||
return array(
|
||||
'extent' => strlen($matches[0]),
|
||||
'element' => array(
|
||||
'name' => 'code',
|
||||
'text' => $text,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
protected function inlineMathJaxLaTeX($Excerpt)
|
||||
{
|
||||
$marker = $Excerpt['text'][0];
|
||||
if (preg_match('/^('.$marker.'{2,})[ ]*(.+?)[ ]*(?<!'.$marker.')\1(?!'.$marker.')/s', $Excerpt['text'], $matches))
|
||||
{
|
||||
$text = $matches[2];
|
||||
$text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8');
|
||||
$text = preg_replace("/[ ]*\n/", ' ', $text);
|
||||
return array(
|
||||
'extent' => strlen($matches[0]),
|
||||
'element' => array(
|
||||
'name' => 'span',
|
||||
'text' => '\('.$text.'\)',
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#
|
||||
# Fenced Code
|
||||
protected function blockFencedCode($Line)
|
||||
{
|
||||
if (preg_match('/^(['.$Line['text'][0].']{3,})[ ]*([\w-]+)?[ ]*$/', $Line['text'], $matches))
|
||||
{
|
||||
$Element = array(
|
||||
'name' => 'code',
|
||||
'text' => '',
|
||||
);
|
||||
|
||||
if (isset($matches[2]))
|
||||
{
|
||||
if (strtolower($matches[2]) === 'latex')
|
||||
{
|
||||
return;
|
||||
}
|
||||
$class = 'language-'.$matches[2];
|
||||
$Element['attributes'] = array(
|
||||
'class' => $class,
|
||||
);
|
||||
}
|
||||
$Block = array(
|
||||
'char' => $Line['text'][0],
|
||||
'openerLength' => mb_strlen($matches[1]),
|
||||
'element' => array(
|
||||
'name' => 'pre',
|
||||
'element' => $Element,
|
||||
),
|
||||
);
|
||||
|
||||
return $Block;
|
||||
}
|
||||
}
|
||||
|
||||
#
|
||||
# Fenced MathJax
|
||||
protected function blockFencedMathJaxLaTeX($Line)
|
||||
{
|
||||
if (preg_match('/^['.$Line['text'][0].']{3,}[ ]*([\w-]+)?[ ]*$/', $Line['text'], $matches))
|
||||
{
|
||||
if ( ! isset($matches[1]) or strtolower($matches[1]) !== 'latex')
|
||||
{
|
||||
return;
|
||||
}
|
||||
$Block = array(
|
||||
'char' => $Line['text'][0],
|
||||
'element' => array(
|
||||
'name' => 'span',
|
||||
'text' => '',
|
||||
),
|
||||
);
|
||||
return $Block;
|
||||
}
|
||||
}
|
||||
|
||||
protected function blockFencedMathJaxLaTeXContinue($Line, $Block)
|
||||
{
|
||||
if (isset($Block['complete']))
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (isset($Block['interrupted']))
|
||||
{
|
||||
$Block['element']['text'] .= "\n";
|
||||
unset($Block['interrupted']);
|
||||
}
|
||||
if (preg_match('/^'.$Block['char'].'{3,}[ ]*$/', $Line['text']))
|
||||
{
|
||||
$Block['element']['text'] = substr($Block['element']['text'], 1);
|
||||
$Block['complete'] = true;
|
||||
return $Block;
|
||||
}
|
||||
$Block['element']['text'] .= "\n".$Line['body'];;
|
||||
return $Block;
|
||||
}
|
||||
|
||||
protected function blockFencedMathJaxLaTeXComplete($Block)
|
||||
{
|
||||
$text = $Block['element']['text'];
|
||||
$Block['element']['text'] = "\$\$\n" . $text . "\n\$\$";
|
||||
return $Block;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user