You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
177 lines
6.0 KiB
PHTML
177 lines
6.0 KiB
PHTML
3 years ago
|
<?php
|
||
3 years ago
|
|
||
|
namespace brain\data;
|
||
|
|
||
3 years ago
|
use League\CommonMark\Environment\Environment;
|
||
|
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
|
||
|
use League\CommonMark\Extension\Strikethrough\StrikethroughExtension;
|
||
|
use League\CommonMark\Extension\Attributes\AttributesExtension;
|
||
|
use League\CommonMark\Extension\FrontMatter\FrontMatterExtension;
|
||
|
use League\CommonMark\Extension\FrontMatter\Output\RenderedContentWithFrontMatter;
|
||
|
use League\CommonMark\MarkdownConverter;
|
||
|
use League\CommonMark\CommonMarkConverter;
|
||
3 years ago
|
use HtmlSanitizer\Extension\Basic\BasicExtension;
|
||
|
use HtmlSanitizer\Extension\Iframe\IframeExtension;
|
||
|
use HtmlSanitizer\SanitizerBuilder;
|
||
|
|
||
3 years ago
|
use function _\orderBy;
|
||
|
|
||
|
class Contents
|
||
|
{
|
||
3 years ago
|
public $files = [];
|
||
|
public $config = [];
|
||
|
public function __construct($folder)
|
||
|
{
|
||
|
$this->read($folder);
|
||
3 years ago
|
}
|
||
3 years ago
|
public function read($folder)
|
||
|
{
|
||
|
$folders = glob("$folder/*", GLOB_ONLYDIR);
|
||
|
foreach ($folders as $folder) {
|
||
3 years ago
|
//$this->files[] = $folder . "/";
|
||
3 years ago
|
$this->read($folder);
|
||
|
}
|
||
|
$files = array_filter(glob("$folder/*md"), "is_file");
|
||
|
foreach ($files as $file) {
|
||
|
$this->files[] = $file;
|
||
|
}
|
||
3 years ago
|
}
|
||
3 years ago
|
|
||
3 years ago
|
public function getAll()
|
||
3 years ago
|
{
|
||
|
$environment = new Environment($this->config);
|
||
|
$environment->addExtension(new CommonMarkCoreExtension());
|
||
|
|
||
3 years ago
|
// Add the extension
|
||
3 years ago
|
$environment->addExtension(new FrontMatterExtension());
|
||
|
|
||
3 years ago
|
//Add Strikethrough rendering
|
||
3 years ago
|
$environment->addExtension(new StrikethroughExtension());
|
||
|
|
||
3 years ago
|
//add attributes to elements in markdown
|
||
3 years ago
|
$environment->addExtension(new AttributesExtension());
|
||
|
|
||
3 years ago
|
// Instantiate the converter engine and start converting some Markdown!
|
||
3 years ago
|
$converter = new MarkdownConverter($environment);
|
||
|
|
||
|
$contents = [];
|
||
|
foreach ($this->files as $file) {
|
||
3 years ago
|
//get meta and html from file
|
||
3 years ago
|
$result = $converter->convertToHtml(file_get_contents($file));
|
||
|
$meta = [];
|
||
|
if ($result instanceof RenderedContentWithFrontMatter) {
|
||
|
$meta = $result->getFrontMatter();
|
||
|
}
|
||
|
|
||
3 years ago
|
//get raw markdown from file
|
||
3 years ago
|
$frontMatterExtension = new FrontMatterExtension();
|
||
|
$parsed = $frontMatterExtension
|
||
|
->getFrontMatterParser()
|
||
|
->parse(file_get_contents($file));
|
||
|
|
||
3 years ago
|
//never trust the front end. clean it up
|
||
3 years ago
|
|
||
|
$builder = new SanitizerBuilder();
|
||
|
$builder->registerExtension(new BasicExtension());
|
||
|
$builder->registerExtension(new IframeExtension());
|
||
|
|
||
3 years ago
|
//relative-a and relative-image
|
||
3 years ago
|
$builder->registerExtension(
|
||
|
new \HtmlSanitizer\Extension\Relative\A\AExtension()
|
||
|
);
|
||
|
$builder->registerExtension(
|
||
|
new \HtmlSanitizer\Extension\Relative\Image\ImageExtension()
|
||
|
);
|
||
|
|
||
|
$detergent = [
|
||
|
"extensions" => ["basic", "relative-a", "relative-image", "iframe"],
|
||
|
"tags" => [
|
||
|
"div" => [
|
||
|
"allowed_attributes" => ["class", "title", "id", "style"],
|
||
|
],
|
||
|
"img" => [
|
||
|
"allowed_attributes" => ["src", "alt", "title", "class"],
|
||
|
],
|
||
|
"iframe" => [
|
||
|
"allowed_attributes" => ["height", "width", "title", "src"],
|
||
|
],
|
||
|
],
|
||
|
];
|
||
|
|
||
|
$sanitizer = $builder->build($detergent);
|
||
|
|
||
|
$scrubbed = $sanitizer->sanitize($result->getContent());
|
||
|
$featureList = explode(",", $meta["feature"]);
|
||
|
$docs = '';
|
||
|
if (isset($meta["files"])) {
|
||
|
$fileList = explode(",", $meta["files"]);
|
||
|
$docs = $meta["files"];
|
||
|
} else {
|
||
|
$fileList = [];
|
||
|
$docs = '';
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
$media = [];
|
||
|
$files = [];
|
||
|
foreach ($featureList as $file) {
|
||
|
$item = trim($file);
|
||
|
$ext = pathinfo($item, PATHINFO_EXTENSION);
|
||
|
if ($item != null || $item != "") {
|
||
3 years ago
|
array_push($media, ["file" => $item, "type" => trim($ext)]);
|
||
3 years ago
|
}
|
||
|
}
|
||
|
|
||
|
foreach ($fileList as $file) {
|
||
|
$item = trim($file);
|
||
|
$ext = pathinfo($item, PATHINFO_EXTENSION);
|
||
|
if ($item != null || $item != "") {
|
||
3 years ago
|
array_push($files, ["file" => $item, "type" => trim($ext)]);
|
||
3 years ago
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
//sort attributes into page object
|
||
3 years ago
|
$page = [
|
||
|
"id" => $meta["id"],
|
||
|
"uuid" => $meta["uuid"],
|
||
|
"title" => $meta["title"],
|
||
|
"feature" => $meta["feature"],
|
||
|
"files" => $docs,
|
||
|
"path" => $meta["path"],
|
||
|
"layout" => $meta["layout"],
|
||
|
"tags" => $meta["tags"],
|
||
|
"author" => $meta["author"],
|
||
|
"created" => date("Y M D d", $meta["created"]),
|
||
|
"updated" => date("Y M D d", $meta["updated"]),
|
||
|
"rawCreated" => $meta["created"],
|
||
|
"rawUpdated" => $meta["updated"],
|
||
|
"createdYear" => date("Y", $meta["created"]),
|
||
|
"createdMonth" => date("m", $meta["created"]),
|
||
|
"deleted" => $meta["deleted"],
|
||
|
"menu" => $meta["menu"],
|
||
|
"featured" => $meta["featured"],
|
||
|
"published" => $meta["published"],
|
||
|
"slug" => $meta["slug"],
|
||
|
"filePath" => $file,
|
||
|
"content" => $parsed->getContent(),
|
||
|
"html" => $scrubbed,
|
||
|
"media" => $media,
|
||
3 years ago
|
"docs" => $files
|
||
3 years ago
|
];
|
||
3 years ago
|
//checks for duplicates
|
||
3 years ago
|
$uuid = $meta["uuid"];
|
||
|
$found = current(
|
||
|
array_filter($contents, function ($item) use ($uuid) {
|
||
|
return isset($item["uuid"]) && $uuid == $item["uuid"];
|
||
|
})
|
||
|
);
|
||
|
|
||
3 years ago
|
// if uuid is not present, add it
|
||
3 years ago
|
if (!$found) {
|
||
3 years ago
|
array_push($contents, $page);
|
||
3 years ago
|
}
|
||
3 years ago
|
}
|
||
3 years ago
|
$contents = orderBy($contents, ["id"], ["desc"]);
|
||
|
return $contents;
|
||
3 years ago
|
}
|
||
|
}
|