Skip to content

Instantly share code, notes, and snippets.

@integer
Created July 16, 2016 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save integer/05dea90948c4e96a254bb2508151d1ef to your computer and use it in GitHub Desktop.
Save integer/05dea90948c4e96a254bb2508151d1ef to your computer and use it in GitHub Desktop.
Comments for Parsedown
<?php
/**
* Adds support for
* {comm}
* Multiline comment block
* {/comm}
* and {lcomm}inline comment{/lcomm}.
*
* Comments will be ignored in page output.
*
* Inline comments over whole line produce empty <p> tag. This change fix it: https://github.com/integer/parsedown/commit/c28065b8c107ecb0117674598f55ba70346aee41
*/
class ParseDownExtension extends Parsedown
{
public function __construct()
{
$this->InlineTypes['{'] []= 'CommentInline';
$this->inlineMarkerList .= '{';
$this->BlockTypes['{'] []= 'CommentBlock';
}
protected function inlineCommentInline($Excerpt)
{
if (preg_match('/^{lcomm}(.*?){\/lcomm}/', $Excerpt['text'], $matches))
{
return array(
'extent' => strlen($matches[0]),
'markup' => '',
'text' => null,
);
}
}
protected function blockCommentBlock($Line, $Block)
{
if (preg_match('/\{comm\}/', $Line['text'], $matches))
{
return array(
'hidden' => true,
);
}
}
protected function blockCommentBlockContinue($Line, $Block)
{
if (isset($Block['complete'])) {
return;
}
// A blank newline has occurred
if (isset($Block['interrupted'])) {
unset($Block['interrupted']);
}
//Check for end of the block.
if (preg_match('/\{\/comm\}/', $Line['text'])) {
$Block['complete'] = true;
return $Block;
}
return $Block;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment