parsedown-extra
parsedown-extra copied to clipboard
Testing a fenced block without CODE tag: comments welcome
I already posted about this in the main Parsedown section, but it should be here...
I was looking for a fenced block with a DIV tag instead of the CODE tag. When writing articles for our CMS, I want people to have a little control over the outcome. Like a paragraph that is centered or right-aligned, or a piece of lyrics.
I used the period to fence the block and you can add one or more class names after it. So something like this:
... lyrics
here are lyrics
for a song
....
And it parses into this (two spaces after the first line of lyrics to make the BR):
<div class="lyrics">
<p>here are lyrics<br />
for a song</p>
</div>
Below are the function in the extension. Any suggestions or comments? Nested fenced blocks don't work (yet) - not sure if I need them...
protected function blockWrap($Line)
{
if (preg_match('/^\.{3,}[ ]?(.*)/', $Line['text'], $matches)) {
$Block = array(
'element' => array(
'name' => 'div',
'handler' => 'lines',
'attributes' => ['class' => $matches[1]],
),
);
return $Block;
}
}
protected function blockWrapContinue($Line, array $Block)
{
if (isset($Block['complete'])) {
return;
}
if ($Line['text'][0] === '.' and preg_match('/^\.{3,}[ ]?(.*)/', $Line['text'], $matches)) {
if (isset($Block['interrupted'])) {
$Block['element']['text'] []= '';
unset($Block['interrupted']);
}
$Block['element']['text'] []= $matches[1];
$Block['complete'] = true;
return $Block;
}
if ( ! isset($Block['interrupted'])) {
$Block['element']['text'] []= $Line['text'];
return $Block;
}
return $Block;
}
protected function blockWrapComplete($Block)
{
return $Block;
}