You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.1 KiB
45 lines
1.1 KiB
<?php
|
|
|
|
namespace App\Ingest;
|
|
|
|
class MDConvertor
|
|
{
|
|
protected $content;
|
|
|
|
public function __construct($content)
|
|
{
|
|
$this->content = json_decode($content, true);
|
|
}
|
|
|
|
public function execute()
|
|
{
|
|
return $this->handleParagraphs($this->content);
|
|
}
|
|
|
|
protected function handleParagraphs(array $paragraphs, $depth = 1)
|
|
{
|
|
$content = '';
|
|
|
|
foreach ($paragraphs as $paragraph) {
|
|
$content = $content .
|
|
str_repeat('#', $depth) .
|
|
' ' .
|
|
(isset($paragraph['numbering']) ? $paragraph['numbering'] : '') .
|
|
' ' .
|
|
$paragraph['content'] .
|
|
"\n";
|
|
|
|
if (
|
|
array_key_exists('children', $paragraph) &&
|
|
$paragraph['children'] &&
|
|
is_array($paragraph['children'])
|
|
) {
|
|
$childrenContent = $this->handleParagraphs($paragraph['children'], $depth + 1);
|
|
|
|
$content = $content . $childrenContent;
|
|
}
|
|
}
|
|
|
|
return $content;
|
|
}
|
|
}
|