Add language prefix support to cached markdown rendering
Add language prefix parameter to cache functions Update renderContentFile to handle language prefixes Add language prefix to internal links in markdown Make template variables available to PHP content files
This commit is contained in:
parent
f212e320cb
commit
8291a86373
2 changed files with 33 additions and 15 deletions
|
|
@ -1,9 +1,9 @@
|
|||
<?php
|
||||
|
||||
function getCachedMarkdown(string $filePath): ?string {
|
||||
function getCachedMarkdown(string $filePath, string $langPrefix = ''): ?string {
|
||||
$cacheDir = '/tmp/folderweb_cache';
|
||||
$mtime = filemtime($filePath);
|
||||
$cacheKey = md5($filePath . $mtime);
|
||||
$cacheKey = md5($filePath . $mtime . $langPrefix);
|
||||
$cachePath = "$cacheDir/$cacheKey";
|
||||
|
||||
if (file_exists($cachePath)) {
|
||||
|
|
@ -13,14 +13,14 @@ function getCachedMarkdown(string $filePath): ?string {
|
|||
return null;
|
||||
}
|
||||
|
||||
function setCachedMarkdown(string $filePath, string $html): void {
|
||||
function setCachedMarkdown(string $filePath, string $html, string $langPrefix = ''): void {
|
||||
$cacheDir = '/tmp/folderweb_cache';
|
||||
if (!is_dir($cacheDir)) {
|
||||
mkdir($cacheDir, 0755, true);
|
||||
}
|
||||
|
||||
$mtime = filemtime($filePath);
|
||||
$cacheKey = md5($filePath . $mtime);
|
||||
$cacheKey = md5($filePath . $mtime . $langPrefix);
|
||||
$cachePath = "$cacheDir/$cacheKey";
|
||||
|
||||
file_put_contents($cachePath, $html);
|
||||
|
|
|
|||
|
|
@ -1,16 +1,19 @@
|
|||
<?php
|
||||
|
||||
function renderContentFile(string $filePath): string {
|
||||
function renderContentFile(string $filePath, ?Context $ctx = null): string {
|
||||
$ext = pathinfo($filePath, PATHINFO_EXTENSION);
|
||||
|
||||
ob_start();
|
||||
if ($ext === 'md') {
|
||||
require_once __DIR__ . '/cache.php';
|
||||
$cached = getCachedMarkdown($filePath);
|
||||
$langPrefix = $ctx ? $ctx->get('langPrefix', '') : '';
|
||||
$cached = getCachedMarkdown($filePath, $langPrefix);
|
||||
|
||||
if ($cached !== null) {
|
||||
echo $cached;
|
||||
} else {
|
||||
// Update to newer versions before PHP 9.0 release
|
||||
// Current versions have been patched for PHP 8.4+ compatibility
|
||||
if (!class_exists('Parsedown')) {
|
||||
require_once __DIR__ . '/vendor/Parsedown.php';
|
||||
}
|
||||
|
|
@ -18,10 +21,25 @@ function renderContentFile(string $filePath): string {
|
|||
require_once __DIR__ . '/vendor/ParsedownExtra.php';
|
||||
}
|
||||
$html = '<article>' . (new ParsedownExtra())->text(file_get_contents($filePath)) . '</article>';
|
||||
setCachedMarkdown($filePath, $html);
|
||||
|
||||
// Add language prefix to internal links
|
||||
if ($langPrefix) {
|
||||
$html = preg_replace(
|
||||
'/href="(\/[^"]*)"/',
|
||||
'href="' . $langPrefix . '$1"',
|
||||
$html
|
||||
);
|
||||
}
|
||||
|
||||
setCachedMarkdown($filePath, $html, $langPrefix);
|
||||
echo $html;
|
||||
}
|
||||
} elseif (in_array($ext, ['html', 'php'])) {
|
||||
// Make template variables available to PHP content files
|
||||
if ($ctx !== null && $ext === 'php') {
|
||||
$templateVars = Hooks::apply(Hook::TEMPLATE_VARS, [], $ctx);
|
||||
extract($templateVars);
|
||||
}
|
||||
include $filePath;
|
||||
}
|
||||
return ob_get_clean();
|
||||
|
|
@ -59,7 +77,7 @@ function renderFile(Context $ctx, string $filePath): void {
|
|||
$ext = pathinfo($realPath, PATHINFO_EXTENSION);
|
||||
|
||||
if (in_array($ext, CONTENT_EXTENSIONS)) {
|
||||
$content = renderContentFile($realPath);
|
||||
$content = renderContentFile($realPath, $ctx);
|
||||
|
||||
$pageDir = dirname($realPath);
|
||||
$pageMetadata = loadMetadata($pageDir);
|
||||
|
|
@ -116,7 +134,7 @@ function renderFile(Context $ctx, string $filePath): void {
|
|||
function renderMultipleFiles(Context $ctx, array $files, string $pageDir): void {
|
||||
$content = '';
|
||||
foreach ($files as $file) {
|
||||
$content .= renderContentFile($file);
|
||||
$content .= renderContentFile($file, $ctx);
|
||||
}
|
||||
|
||||
$pageMetadata = loadMetadata($pageDir);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue