diff --git a/README.md b/README.md index b034f7c..9ec25fb 100644 --- a/README.md +++ b/README.md @@ -4,21 +4,21 @@ A [Bludit](https://www.bludit.com) plugin that automatically exports your blog a ## What it does -Whenever you create, update, or delete an article in Bludit, the plugin pushes the change to a GitHub repository via the GitHub API. Each article is exported as its own folder, with the markdown file inside `article/` and images inside `img/`: +Whenever you create, update, or delete an article in Bludit, the plugin pushes the change to a GitHub repository via the GitHub API. Each article is exported as its own folder, with the markdown file inside `content/` and images inside `img/`: ``` my-article/ - article/ + content/ index.md img/ photo.png screenshot.jpg another-post/ - article/ + content/ index.md ``` -Each `index.md` contains a YAML frontmatter block followed by the article content. Image references are written as relative paths from the `article/` subfolder: +Each `index.md` contains a YAML frontmatter block followed by the article content. Image references are written as relative paths from the `content/` subfolder: ```markdown --- @@ -51,18 +51,22 @@ Article content with images like ![](../img/photo.png) | Repository Name | The target repository (e.g. `my-blog`). | | Branch | Branch to push to (default: `main`). | | Path Prefix | Optional subfolder prefix. Leave empty to export at the repo root. | -| Export drafts | When checked, draft articles are also exported. | -| Auto-export on save | When checked, articles are automatically pushed to GitHub whenever you save or modify them. Uncheck to only use manual bulk export. | +| Auto-export draft articles on save | When checked, draft articles are automatically pushed to GitHub when saved or modified. **Default is OFF**. | +| Auto-export published articles on save | When checked, published articles are automatically pushed to GitHub when saved or modified. **Default is ON**. | ## Bulk export -The settings page includes an **Export all articles now** button that pushes all your existing articles to GitHub in one go. Useful on first install or after changing the target repository. +The settings page includes two export buttons: +- **Export everything** — pushes all articles (both drafts and published) to GitHub in one go. +- **Export published only** — pushes only published articles to GitHub. + +Useful on first install or after changing the target repository. ## Behavior -- **On article save** (if auto-export is enabled): `article/index.md` is created or updated, and all images referenced in the content are uploaded to `img/`. -- **On article delete** (if auto-export is enabled): both `article/index.md` and all files in `img/` are deleted (GitHub removes the empty folders automatically). -- **Manual bulk export:** Use the "Export all articles now" button to push articles regardless of auto-export setting. +- **On article save** (if auto-export is enabled): `content/index.md` is created or updated, and all images referenced in the content are uploaded to `img/`. +- **On article delete** (if auto-export is enabled): both `content/index.md` and all files in `img/` are deleted (GitHub removes the empty folders automatically). +- **Manual bulk export:** Use the "Export everything" or "Export published only" button to push articles regardless of auto-export setting. - **Errors** are displayed in the plugin settings panel and never interfere with Bludit's own save process. ## Requirements diff --git a/plugin.php b/plugin.php index ad25e92..428009b 100644 --- a/plugin.php +++ b/plugin.php @@ -5,25 +5,26 @@ class pluginBluditGithub extends Plugin public function init() { $this->dbFields = [ - 'token' => '', - 'owner' => '', - 'repo' => '', - 'branch' => 'main', - 'path' => '', - 'exportDrafts' => false, - 'autoExport' => true, + 'token' => '', + 'owner' => '', + 'repo' => '', + 'branch' => 'main', + 'path' => '', + 'exportDraftsAuto' => false, + 'exportPublishedAuto' => true, ]; } public function form() { - $token = $this->getValue('token'); - $owner = $this->getValue('owner'); - $repo = $this->getValue('repo'); - $branch = $this->getValue('branch'); - $path = $this->getValue('path'); - $exportDrafts = $this->getValue('exportDrafts') ? 'checked' : ''; - $status = $this->getStatus(); + $token = $this->getValue('token'); + $owner = $this->getValue('owner'); + $repo = $this->getValue('repo'); + $branch = $this->getValue('branch'); + $path = $this->getValue('path'); + $exportDraftsAuto = $this->getValue('exportDraftsAuto') ? 'checked' : ''; + $exportPublishedAuto = $this->getValue('exportPublishedAuto') ? 'checked' : ''; + $status = $this->getStatus(); $html = '
'; $html .= ''; @@ -49,22 +50,23 @@ class pluginBluditGithub extends Plugin $html .= '
'; $html .= ''; $html .= ''; - $html .= 'Each article is exported as {key}/index.md. Set a prefix (e.g. articles) to nest under a subfolder.'; + $html .= 'Each article is exported as {key}/content/index.md. Set a prefix (e.g. articles) to nest under a subfolder.'; $html .= '
'; + $html .= '
'; + $html .= ''; $html .= '
'; - $html .= ''; + $html .= ''; $html .= '
'; - - $autoExport = $this->getValue('autoExport') ? 'checked' : ''; $html .= '
'; - $html .= ''; + $html .= ''; + $html .= '
'; $html .= '
'; $html .= '
'; $html .= ''; - $html .= '

Push all ' . ($exportDrafts ? '' : 'published ') . 'articles to GitHub at once.

'; - $html .= ''; + $html .= ''; + $html .= ''; $html .= '
'; if ($status) { @@ -80,24 +82,28 @@ class pluginBluditGithub extends Plugin { parent::post(); - // Ensure checkboxes are properly saved as boolean (Bludit might store them as strings) - $this->db['exportDrafts'] = (isset($_POST['exportDrafts']) && $_POST['exportDrafts']) ? true : false; - $this->db['autoExport'] = (isset($_POST['autoExport']) && $_POST['autoExport']) ? true : false; + $this->db['exportDraftsAuto'] = (isset($_POST['exportDraftsAuto']) && $_POST['exportDraftsAuto']) ? true : false; + $this->db['exportPublishedAuto'] = (isset($_POST['exportPublishedAuto']) && $_POST['exportPublishedAuto']) ? true : false; $this->dbSave(); - if (!empty($_POST['bulkExport'])) { - $this->bulkExport(); + if (!empty($_POST['bulkExportAll'])) { + $this->bulkExport(true); + } elseif (!empty($_POST['bulkExportPublished'])) { + $this->bulkExport(false); } } public function afterPageCreate($key) { try { - if (empty($key) || !$this->isConfigured() || !$this->getValue('autoExport')) { + if (empty($key) || !$this->isConfigured()) { return; } $page = new Page($key); - if ($this->shouldExport($page)) { + $isDraft = $page->getValue('status') === 'draft'; + $shouldAutoExport = $isDraft ? $this->getValue('exportDraftsAuto') : $this->getValue('exportPublishedAuto'); + + if ($shouldAutoExport) { $this->exportPage($page); } } catch (Throwable $e) { @@ -108,11 +114,14 @@ class pluginBluditGithub extends Plugin public function afterPageModify($key) { try { - if (empty($key) || !$this->isConfigured() || !$this->getValue('autoExport')) { + if (empty($key) || !$this->isConfigured()) { return; } $page = new Page($key); - if ($this->shouldExport($page)) { + $isDraft = $page->getValue('status') === 'draft'; + $shouldAutoExport = $isDraft ? $this->getValue('exportDraftsAuto') : $this->getValue('exportPublishedAuto'); + + if ($shouldAutoExport) { $this->exportPage($page); } } catch (Throwable $e) { @@ -130,8 +139,8 @@ class pluginBluditGithub extends Plugin $prefix = trim($this->getValue('path'), '/'); $base = ($prefix ? $prefix . '/' : '') . $key; - // Delete article/index.md - $mdPath = $base . '/article/index.md'; + // Delete content/index.md + $mdPath = $base . '/content/index.md'; $sha = $this->getFileSha($mdPath); if ($sha) { $this->githubRequest('DELETE', '/contents/' . $mdPath, [ @@ -161,14 +170,6 @@ class pluginBluditGithub extends Plugin // --- Private --- - private function shouldExport($page) - { - $isDraft = $page->getValue('status') === 'draft'; - $exportDraftsVal = $this->getValue('exportDrafts'); - $shouldExportDraft = ($exportDraftsVal === true || $exportDraftsVal === '1' || $exportDraftsVal === 1); - - return (!$isDraft) || $shouldExportDraft; - } private function exportPage($page, $saveStatus = true) { @@ -273,7 +274,7 @@ class pluginBluditGithub extends Plugin return ($data !== false && $httpCode === 200) ? $data : null; } - private function bulkExport() + private function bulkExport($includeDrafts = false) { if (!$this->isConfigured()) { $this->setStatus('Error: GitHub settings not configured.'); @@ -282,23 +283,21 @@ class pluginBluditGithub extends Plugin global $pages; - $includeDrafts = (bool)$this->getValue('exportDrafts'); - $list = $pages->getList(1, 9999, true, true, true, $includeDrafts, false); - $count = 0; + $list = $pages->getList(1, 9999, true, true, true, $includeDrafts, false); + $count = 0; foreach ($list as $key) { try { $page = new Page($key); - if ($this->shouldExport($page)) { - $this->exportPage($page, false); - $count++; - } + $this->exportPage($page, false); + $count++; } catch (Exception $e) { continue; } } - $this->setStatus(date('Y-m-d H:i:s') . " — Bulk export: {$count} articles pushed."); + $type = $includeDrafts ? 'everything' : 'published articles'; + $this->setStatus(date('Y-m-d H:i:s') . " — Bulk export: {$count} {$type} pushed."); } private function buildMarkdown($page, $key, $rawContent = null) @@ -381,7 +380,7 @@ class pluginBluditGithub extends Plugin private function getFilePath($key) { $prefix = trim($this->getValue('path'), '/'); - return ($prefix ? $prefix . '/' : '') . $key . '/article/index.md'; + return ($prefix ? $prefix . '/' : '') . $key . '/content/index.md'; } private function getFileSha($filePath)