Update README and plugin logic: Clarify article export structure and enhance image deletion process in GitHub integration

This commit is contained in:
interlope committed 2026-06-01 17:34:52 +02:00
1 parent acc60c7007
commit 54b88e49c5
2 files changed
+41 -25

No files matched your search

+12 -7
View File
@@ -4,16 +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 containing an `index.md` file:
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/`:
```
my-article/
index.md
article/
index.md
img/
photo.png
screenshot.jpg
another-post/
index.md
article/
index.md
```
Each `index.md` contains a YAML frontmatter block followed by the article content:
Each `index.md` contains a YAML frontmatter block followed by the article content. Image references are written as relative paths from the `article/` subfolder:
```markdown
---
@@ -28,7 +33,7 @@ description: "Optional meta description"
category: tech
---
Article content here...
Article content with images like ![](../img/photo.png)
```
## Installation
@@ -54,8 +59,8 @@ The settings page includes an **Export all articles now** button that pushes all
## Behavior
- **On article save:** the article's `index.md` is created or updated in the repository.
- **On article delete:** the article's `index.md` is deleted from the repository (GitHub removes the empty folder automatically).
- **On article save:** `article/index.md` is created or updated, and all images referenced in the content are uploaded to `img/`.
- **On article delete:** both `article/index.md` and all files in `img/` are deleted (GitHub removes the empty folders automatically).
- **Errors** are displayed in the plugin settings panel and never interfere with Bludit's own save process.
## Requirements
+29 -18
View File
@@ -115,21 +115,32 @@ class pluginBluditGithub extends Plugin
if (empty($key) || !$this->isConfigured()) {
return;
}
// List all files in the article's folder and delete them
$prefix = trim($this->getValue('path'), '/');
$dirPath = ($prefix ? $prefix . '/' : '') . $key;
$items = $this->githubRequest('GET', '/contents/' . $dirPath);
if (!is_array($items)) {
return;
$prefix = trim($this->getValue('path'), '/');
$base = ($prefix ? $prefix . '/' : '') . $key;
// Delete article/index.md
$mdPath = $base . '/article/index.md';
$sha = $this->getFileSha($mdPath);
if ($sha) {
$this->githubRequest('DELETE', '/contents/' . $mdPath, [
'message' => 'Delete: ' . $key,
'sha' => $sha,
'branch' => $this->getValue('branch'),
]);
}
foreach ($items as $item) {
if ($item['type'] === 'file') {
$this->githubRequest('DELETE', '/contents/' . $item['path'], [
'message' => 'Delete: ' . $key . '/' . $item['name'],
'sha' => $item['sha'],
'branch' => $this->getValue('branch'),
]);
// Delete all files in img/
$items = $this->githubRequest('GET', '/contents/' . $base . '/img');
if (is_array($items)) {
foreach ($items as $item) {
if ($item['type'] === 'file') {
$this->githubRequest('DELETE', '/contents/' . $item['path'], [
'message' => 'Delete image: ' . $key . '/img/' . $item['name'],
'sha' => $item['sha'],
'branch' => $this->getValue('branch'),
]);
}
}
}
} catch (Throwable $e) {
@@ -214,7 +225,7 @@ class pluginBluditGithub extends Plugin
}
}
$repoPath = ($prefix ? $prefix . '/' : '') . $key . '/' . $filename;
$repoPath = ($prefix ? $prefix . '/' : '') . $key . '/img/' . $filename;
$sha = $this->getFileSha($repoPath);
$data = [
@@ -262,11 +273,11 @@ class pluginBluditGithub extends Plugin
{
$content = $rawContent ?? $page->content();
// Rewrite absolute Bludit upload URLs to relative filenames
// e.g. https://domain/bl-content/uploads/pages/UUID/image.png → image.png
// Rewrite absolute Bludit upload URLs to relative img/ paths
// e.g. https://domain/bl-content/uploads/pages/UUID/image.png → ../img/image.png
$content = preg_replace(
'/https?:\/\/[^\/]+\/bl-content\/uploads\/pages\/[^\/]+\/([^"\')\s<>]+)/',
'$1',
'../img/$1',
$content
);
@@ -313,7 +324,7 @@ class pluginBluditGithub extends Plugin
private function getFilePath($key)
{
$prefix = trim($this->getValue('path'), '/');
return ($prefix ? $prefix . '/' : '') . $key . '/index.md';
return ($prefix ? $prefix . '/' : '') . $key . '/article/index.md';
}
private function getFileSha($filePath)