Refactor export path handling and improve markdown generation for articles

This commit is contained in:
interlope committed 2026-06-01 17:00:55 +02:00
1 parent 571827b18c
commit 35ecc763ba
1 file changed
+42 -42
+42 -42
View File
@@ -9,7 +9,7 @@ class pluginBluditGithub extends Plugin
'owner' => '', 'owner' => '',
'repo' => '', 'repo' => '',
'branch' => 'main', 'branch' => 'main',
'path' => 'posts', 'path' => '',
'exportDrafts' => false, 'exportDrafts' => false,
]; ];
} }
@@ -46,9 +46,9 @@ class pluginBluditGithub extends Plugin
$html .= '</div>'; $html .= '</div>';
$html .= '<div>'; $html .= '<div>';
$html .= '<label>Export Path</label>'; $html .= '<label>Path Prefix <em>(optional)</em></label>';
$html .= '<input name="path" type="text" value="' . htmlspecialchars($path) . '" placeholder="posts">'; $html .= '<input name="path" type="text" value="' . htmlspecialchars($path) . '" placeholder="Leave empty to export at repo root">';
$html .= '<span class="tip">Directory in the repo where articles are written (e.g. <code>posts</code>). Leave empty for root.</span>'; $html .= '<span class="tip">Each article is exported as <code>{key}/index.md</code>. Set a prefix (e.g. <code>articles</code>) to nest under a subfolder.</span>';
$html .= '</div>'; $html .= '</div>';
$html .= '<div>'; $html .= '<div>';
@@ -119,25 +119,28 @@ class pluginBluditGithub extends Plugin
$currentKeys = $this->getAllPageKeys(); $currentKeys = $this->getAllPageKeys();
$exportPath = trim($this->getValue('path'), '/'); $exportPath = trim($this->getValue('path'), '/');
$endpoint = '/contents/' . ($exportPath ?: ''); $listEndpoint = '/contents/' . ($exportPath ?: '');
$files = $this->githubRequest('GET', $endpoint); $items = $this->githubRequest('GET', $listEndpoint);
if (!is_array($files)) { if (!is_array($items)) {
return; return;
} }
foreach ($files as $file) { foreach ($items as $item) {
if (!isset($file['type']) || $file['type'] !== 'file') { if (!isset($item['type']) || $item['type'] !== 'dir') {
continue; continue;
} }
if (substr($file['name'], -3) !== '.md') { $key = $item['name'];
if (in_array($key, $currentKeys, true)) {
continue; continue;
} }
$key = substr($file['name'], 0, -3); // Directory is no longer a Bludit page — delete its index.md
if (!in_array($key, $currentKeys, true)) { $indexPath = $item['path'] . '/index.md';
$this->githubRequest('DELETE', '/contents/' . $file['path'], [ $sha = $this->getFileSha($indexPath);
if ($sha) {
$this->githubRequest('DELETE', '/contents/' . $indexPath, [
'message' => 'Delete: ' . $key, 'message' => 'Delete: ' . $key,
'sha' => $file['sha'], 'sha' => $sha,
'branch' => $this->getValue('branch'), 'branch' => $this->getValue('branch'),
]); ]);
} }
@@ -153,8 +156,9 @@ class pluginBluditGithub extends Plugin
private function exportPage($page, $saveStatus = true) private function exportPage($page, $saveStatus = true)
{ {
$content = $this->buildMarkdown($page); $key = $page->getValue('key');
$filePath = $this->getFilePath($page->key()); $content = $this->buildMarkdown($page, $key);
$filePath = $this->getFilePath($key);
$sha = $this->getFileSha($filePath); $sha = $this->getFileSha($filePath);
$data = [ $data = [
@@ -201,42 +205,38 @@ class pluginBluditGithub extends Plugin
$this->setStatus(date('Y-m-d H:i:s') . " — Bulk export: {$count} articles pushed."); $this->setStatus(date('Y-m-d H:i:s') . " — Bulk export: {$count} articles pushed.");
} }
private function buildMarkdown($page) private function buildMarkdown($page, $key)
{ {
// Prefer raw content (Markdown as typed); fall back to rendered HTML // TinyMCE stores HTML — content() returns the full rendered content
$content = $page->getValue('content');
if (empty($content)) {
$content = $page->content(); $content = $page->content();
}
// Tags // Tags: getValue returns comma-separated string in Bludit
$tags = $page->tags(); $rawTags = $page->getValue('tags');
$tagLines = ''; $tagLines = '';
if (!empty($tags)) { if (!empty($rawTags)) {
foreach ($tags as $tag) { $tagArray = array_map('trim', explode(',', $rawTags));
$tagName = is_object($tag) ? $tag->name() : (string)$tag; foreach ($tagArray as $tag) {
$tagLines .= "\n - " . $tagName; if ($tag !== '') {
$tagLines .= "\n - " . $tag;
}
} }
} }
$fm = "---\n"; $fm = "---\n";
$fm .= 'title: "' . addslashes($page->title()) . "\"\n"; $fm .= 'title: "' . str_replace('"', '\"', $page->title()) . "\"\n";
$fm .= 'date: ' . date('Y-m-d', strtotime($page->getValue('dateRaw'))) . "\n"; $fm .= 'date: ' . date('Y-m-d', strtotime($page->getValue('dateRaw'))) . "\n";
$fm .= 'slug: ' . $page->key() . "\n"; $fm .= 'slug: ' . $key . "\n";
$fm .= 'status: ' . $page->getValue('status') . "\n"; $fm .= 'status: ' . $page->getValue('status') . "\n";
$fm .= 'tags:' . ($tagLines ?: ' []') . "\n"; $fm .= 'tags:' . ($tagLines ?: ' []') . "\n";
$description = $page->description(); $description = $page->getValue('description');
if (!empty($description)) { if (!empty($description)) {
$fm .= 'description: "' . addslashes($description) . "\"\n"; $fm .= 'description: "' . str_replace('"', '\"', $description) . "\"\n";
} }
$category = $page->category(); $category = $page->getValue('category');
if (!empty($category)) { if (!empty($category) && $category !== 'uncategorized') {
$catKey = is_object($category) ? $category->key() : (string)$category; $fm .= 'category: ' . $category . "\n";
if ($catKey && $catKey !== 'uncategorized') {
$fm .= 'category: ' . $catKey . "\n";
}
} }
$cover = $page->coverImage(); $cover = $page->coverImage();
@@ -251,8 +251,8 @@ class pluginBluditGithub extends Plugin
private function getFilePath($key) private function getFilePath($key)
{ {
$exportPath = trim($this->getValue('path'), '/'); $prefix = trim($this->getValue('path'), '/');
return ($exportPath ? $exportPath . '/' : '') . $key . '.md'; return ($prefix ? $prefix . '/' : '') . $key . '/index.md';
} }
private function getFileSha($filePath) private function getFileSha($filePath)
@@ -305,7 +305,7 @@ class pluginBluditGithub extends Plugin
return false; return false;
} }
// 404 on GET means the file doesn't exist yet — not an error // 404 on GET = file/dir doesn't exist yet, not an error
if ($httpCode === 404) { if ($httpCode === 404) {
return null; return null;
} }
@@ -330,7 +330,7 @@ class pluginBluditGithub extends Plugin
} }
} }
// Fallback: most recently modified page directory // Fallback: most recently modified page directory (Bludit stores pages as index.txt)
if (!defined('PATH_PAGES') || !is_dir(PATH_PAGES)) { if (!defined('PATH_PAGES') || !is_dir(PATH_PAGES)) {
return null; return null;
} }
@@ -339,7 +339,7 @@ class pluginBluditGithub extends Plugin
$newestTime = 0; $newestTime = 0;
foreach (glob(PATH_PAGES . '*', GLOB_ONLYDIR) as $dir) { foreach (glob(PATH_PAGES . '*', GLOB_ONLYDIR) as $dir) {
$indexFile = $dir . DS . 'index.php'; $indexFile = $dir . DS . 'index.txt';
if (file_exists($indexFile)) { if (file_exists($indexFile)) {
$mtime = filemtime($indexFile); $mtime = filemtime($indexFile);
if ($mtime > $newestTime) { if ($mtime > $newestTime) {