Rebuild article system

This commit is contained in:
2025-06-09 20:59:09 -06:00
parent 2ffa1b2267
commit 6d996abe27
8 changed files with 422 additions and 40 deletions
+46
View File
@@ -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();
}
+36
View File
@@ -0,0 +1,36 @@
<?php
use League\CommonMark\Environment\Environment;
use League\CommonMark\MarkdownConverter;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\Extension\GithubFlavoredMarkdownExtension;
use League\CommonMark\Extension\FrontMatter\FrontMatterExtension;
use League\CommonMark\Extension\FrontMatter\Output\RenderedContentWithFrontMatter;
use Symfony\Component\Yaml\Yaml;
function render_markdown($file) {
// Set up the environment with FrontMatter and GFM support
$environment = new Environment([]);
$environment->addExtension(new CommonMarkCoreExtension());
$environment->addExtension(new GithubFlavoredMarkdownExtension());
$environment->addExtension(new FrontMatterExtension());
$converter = new MarkdownConverter($environment);
$markdownText = file_get_contents($file);
// Convert markdown and extract front matter
/** @var RenderedContentWithFrontMatter $result */
$result = $converter->convert($markdownText);
// Parse YAML front matter
if ($result instanceof RenderedContentWithFrontMatter) {
$frontMatter = $result->getFrontMatter();
}
$html = $result->getContent();
// return result
return [
'meta' => $frontMatter,
'html' => $html,
];
}
+30
View File
@@ -0,0 +1,30 @@
<?php
use Symfony\Component\Yaml\Yaml;
function render_php($file) {
ob_start();
#include __DIR__ . '/' . $file;
include $file;
$html = ob_get_clean();
$meta = [];
# Split off head matter
#
# this pattern uses the 'ms' modifier to treat multiline strings as a
# single string, and let '\s', '.' etc treat newlines as whitespace
# I match an _optional_ HTML ( '<!-- nnnn -->') comment if we want to wrap
# the headmatter in a comment.
#
# The headmatter is delimited by either '----' or '++++' as per
# Markdown/yaml standards.
# Technical note that '+' is a regex character so \+{3,} instead of ++++
if (preg_match('/^(<!--\s*)?(-{3,}|\+{3,})\s*(.*?)(-{3,}|\+{3,})\s*(-->)?\s*(.*)/ms',$html, $parts) === 1) {
$html = $parts[6];
$meta = array_merge($meta,Yaml::parse($parts[3]));
}
return [
'html' => $html,
'meta' => $meta,
];
}