Skip to content

Instantly share code, notes, and snippets.

@JunaidQadirB
Created August 1, 2020 19:38
Show Gist options
  • Save JunaidQadirB/4df3c7daa5cb2ab40639df65e55ced80 to your computer and use it in GitHub Desktop.
Save JunaidQadirB/4df3c7daa5cb2ab40639df65e55ced80 to your computer and use it in GitHub Desktop.
<?php
/**
*
* @link https://github.com/codex-team/editor.js/issues/676#issuecomment-587462314
*/
namespace App\Traits;
trait OutputsHtml
{
public function render()
{
$blocks = json_decode($this->body)->blocks;
$content = '';
foreach ($blocks as $block) {
if ($block->type == 'paragraph') {
$content .= '<p>' . $block->data->text . "</p>\n";
} else if ($block->type == 'header') {
$content .= '<h' . $block->data->level . '>' . $block->data->text . '</h' . $block->data->level . ">\n";
} else if ($block->type == 'list') {
$listTag = ($block->data->style == 'ordered') ? 'ol' : 'ul';
$content .= '<' . $listTag . ">\n";
foreach ($block->data->items as $itemText) {
$content .= "\t<li>" . $itemText . "</li>\n";
}
$content .= '</' . $listTag . ">\n";
} else if ($block->type == 'checklist') {
foreach ($block->data->items as $itemText) {
$content .= "<label>\n";
$content .= "<input " . ($itemText->checked ? 'checked' : '') . " type=\"checkbox\" disabled=\"true\" />\n";
$content .= $itemText->text . "</label>\n<br/>";
}
}
}
return $content;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment