Created
August 27, 2021 18:35
-
-
Save hanspagel/aa7a2f1f122f4e767f31951a41b9edb6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Drivers; | |
use SplFileInfo; | |
use FilesystemIterator; | |
use Orbit\Facades\Orbit; | |
use Illuminate\Support\Str; | |
use RecursiveIteratorIterator; | |
use RecursiveDirectoryIterator; | |
use Illuminate\Support\Collection; | |
use Spatie\YamlFrontMatter\YamlFrontMatter; | |
use Orbit\Drivers\Markdown as OriginalMarkdown; | |
class CustomMarkdown extends OriginalMarkdown | |
{ | |
public function shouldRestoreCache(string $directory): bool | |
{ | |
// Disable the cache in all non-production environments | |
if (config('app.env') !== 'production') { | |
return true; | |
} | |
$highest = 0; | |
foreach (new FilesystemIterator($directory) as $file) { | |
if ($file->getMTime() > $highest) { | |
$highest = $file->getMTime(); | |
} | |
} | |
return $highest > filemtime(Orbit::getDatabasePath()); | |
} | |
protected function parseContent(SplFileInfo $file): array | |
{ | |
$document = YamlFrontMatter::parseFile($file->getPathname()); | |
// Make the absolute path a relative path | |
$file = Str::of($file->getPathname()) | |
->ltrim(config('orbit.paths.content')) | |
->explode('/')->slice(1)->join('/'); | |
// Get the repository name from the first folder | |
$repository = Str::of($file)->explode('/')->shift(); | |
// Use the remaining path for the URL | |
$path = Str::of($file)->rtrim('.' . $this->extension())->explode('/')->slice(1)->join('/'); | |
return array_merge( | |
$document->matter(), | |
[ | |
// 'doc_pages/tiptap/guide/example.md' | |
'file' => $file, | |
// '# Example' | |
'content' => trim($document->body()), | |
// 'tiptap' | |
'repository' => $repository, | |
// 'guide/example' | |
'path' => $path, | |
] | |
); | |
} | |
public function all(string $directory): Collection | |
{ | |
$collection = Collection::make(); | |
// Loop through the directory recursively | |
$files = new RecursiveIteratorIterator( | |
new RecursiveDirectoryIterator( | |
$directory, | |
FilesystemIterator::FOLLOW_SYMLINKS | |
), | |
RecursiveIteratorIterator::SELF_FIRST, | |
); | |
foreach ($files as $file) { | |
if ($file->getExtension() !== $this->extension()) { | |
continue; | |
} | |
$collection->push($this->parseContent($file)); | |
} | |
return $collection; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment