Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Override's some of Parsedown's features to add support for github style task lists. You must build your own serverside & client side components for updating the markdown. The value of the checkbox is it's index of the checkbox based on all of the checkboxes found in the markdown.
<?php
/**
* Override's some of Parsedown's features to add support for github style task lists.
* You must build your own serverside & client side components for updating the markdown.
* The value of the checkbox is it's index of the checkbox based on all of the checkboxes found in the markdown.
* @see https://github.com/erusev/parsedown
*/
class TaskListParsedown extends Parsedown {
protected $taskListIndex=0;
#
# List
protected function identifyList($Line) {
list($name, $pattern)=($Line['text'][0]<='-' ? array('ul', '[*+-]'):array('ol', '[0-9]+[.]'));
if(preg_match('/^('.$pattern.'[ ]+)(.*)/', $Line['text'], $matches)) {
$Block=array(
'indent'=>$Line['indent'],
'pattern'=>$pattern,
'element'=>array(
'name'=>$name,
'handler'=>'elements',
),
);
$Block['li']=$this->identifyTaskList($matches[2], $Block);
$Block['element']['text'][]=&$Block['li'];
return $Block;
}
}
protected function addToList($Line, array $Block) {
if($Block['indent']===$Line['indent'] && preg_match('/^'.$Block['pattern'].'[ ]+(.*)/', $Line['text'], $matches)) {
if(isset($Block['interrupted'])) {
$Block['li']['text'][]= '';
unset($Block['interrupted']);
}
unset($Block['li']);
$Block['li']=$this->identifyTaskList($matches[1], $Block);
$Block['element']['text'][]=&$Block['li'];
return $Block;
}
if(!isset($Block['interrupted'])) {
$text=preg_replace('/^[ ]{0,4}/', '', $Line['body']);
$Block['li']['text'][]=$text;
return $Block;
}
if($Line['indent']>0) {
$Block['li']['text'][]='';
$text = preg_replace('/^[ ]{0,4}/', '', $Line['body']);
$Block['li']['text'][]=$text;
unset($Block['interrupted']);
return $Block;
}
}
protected function identifyTaskList($lineText, &$Block) {
if(preg_match('/^(\s*)\[(\s|x)\](.*?)$/i', $lineText, $matches)) {
$result=array();
if(count($matches)>3) {
if(!array_key_exists('taskList', $Block['element'])) {
if(!array_key_exists('attributes', $Block['element'])) {
$Block['element']['attributes']=array('class'=>'taskList');
}else {
$Block['element']['attributes']['class']='taskList';
}
$Block['element']['taskList']=true;
}
if(!empty($matches[1])) {
$result[]=$matches[1];
}
$checkID='md-check-'.uniqid(time());
$result['input']=array(
'name'=>'input',
'attributes'=>array(
'type'=>'checkbox',
'name'=>$checkID,
'id'=>$checkID,
'value'=>'1'
)
);
if(strtolower($matches[2])=='x') {
$result['input']['attributes']['checked']='checked';
}
$result['label']=array(
'name'=>'label',
'handler'=>'line',
'attributes'=>array('for'=>$checkID),
'text'=>$matches[3]
);
$lineText='<p>';
foreach($result as $item) {
$lineText.=$this->element($item);
}
$lineText.='</p>';
//Increment the index
$this->taskListIndex++;
}
}
return array(
'name'=>'li',
'handler'=>'li',
'text'=>array($lineText)
);
}
}
?>
@BenjaminHoegh

This comment has been minimized.

Copy link

@BenjaminHoegh BenjaminHoegh commented Jun 8, 2018

Do you have a new version of this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment