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.
53 lines
1.3 KiB
53 lines
1.3 KiB
<?php
|
|
|
|
namespace App\Ingest;
|
|
|
|
class MDConvertor
|
|
{
|
|
protected $content;
|
|
|
|
public function __construct($content)
|
|
{
|
|
$this->content = $content;
|
|
}
|
|
|
|
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'] : '') .
|
|
' ' .
|
|
$this->parseContent($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;
|
|
}
|
|
|
|
protected function parseContent($content)
|
|
{
|
|
$content = preg_replace("/\xE2\x80\x8B/", "", $content);
|
|
$content = preg_replace("/[\x{200B}-\x{200D}\x{FEFF}]/u", "", $content);
|
|
|
|
return utf8_decode($content);
|
|
}
|
|
}
|