Rebuild article system
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
function extractMeta($directory) {
|
||||
$metaData = [];
|
||||
|
||||
if (!is_dir($directory)) {
|
||||
throw new Exception("Directory not found: $directory");
|
||||
}
|
||||
|
||||
foreach (new DirectoryIterator($directory) as $file) {
|
||||
if ($file->isFile() && (
|
||||
$file->getExtension() === 'md' ||
|
||||
$file->getExtension() === 'php'
|
||||
)) {
|
||||
$contents = file_get_contents($file->getPathname());
|
||||
|
||||
// Extract title and image from the front matter
|
||||
if (preg_match('/^-{3,}\s*(.*?)\s*-{3,}/ms', $contents, $matches)) {
|
||||
$frontMatter = $matches[1];
|
||||
|
||||
// Extract title
|
||||
preg_match('/title:\s*(.*?)\s*$/m', $frontMatter, $titleMatch);
|
||||
$title = trim($titleMatch[1] ?? '');
|
||||
|
||||
// Extract image
|
||||
preg_match('/image:\s*(.*?)\s*$/m', $frontMatter, $imageMatch);
|
||||
$image = trim($imageMatch[1] ?? '');
|
||||
|
||||
$metaData[$file->getFilename()] = [
|
||||
'title' => $title,
|
||||
'image' => $image,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $metaData;
|
||||
}
|
||||
|
||||
// Test the function
|
||||
try {
|
||||
$directory = __DIR__ ;
|
||||
$meta = extractMeta($directory);
|
||||
print_r($meta);
|
||||
} catch (Exception $e) {
|
||||
echo "Error: " . $e->getMessage();
|
||||
}
|
||||
Reference in New Issue
Block a user