diff --git a/Dockerfile b/Dockerfile index b62a518..c643d4d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,8 +2,9 @@ FROM php:8.4-apache-bookworm WORKDIR "/var/www/html" RUN apt-get update; \ - docker-php-ext-install pdo_mysql; \ apt-get -y --no-install-recommends install \ - git; \ + git \ + libonig-dev; \ + docker-php-ext-install mbstring pdo_mysql; \ apt-get clean; \ rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/* diff --git a/extensions/plugi-ext-blog/README.md b/extensions/plugi-ext-blog/README.md new file mode 100644 index 0000000..47a9438 --- /dev/null +++ b/extensions/plugi-ext-blog/README.md @@ -0,0 +1,25 @@ +# Plugi Blog Extension + +The blog extension provides Website Manager post and category management plus two page-builder blocks. Posts are single-language; post bodies are stored as Markdown and rendered safely on request. + +## Install and update the database + +1. Place this directory at `extensions/plugi-ext-blog`. Plugi automatically discovers extension folders containing both `extension.php` and `config.php`. +2. Open Website Manager and apply the pending database definition update. Existing installations need this update to create or update the extension-owned post and category tables, including the optional featured-image field. +3. Add categories if useful, then create posts as drafts or publish them from the Blog section. + +## Create the public pages + +1. Create a regular Plugi page with the route `/blog` and add the **Published Posts** block. It lists published posts newest first; drafts are never shown. +2. Create another regular page with the route `/blog/{slug}` and add the **Blog Post** block. The core router supplies the named `slug` parameter to the block. +3. Apply the site’s normal page layout and theme to both pages. Their presentation remains controlled by the page builder and theme. + +The detail block displays a not-found state for a missing, unknown, or unpublished slug. The blog blocks are not page-cached so publishing or editing content is reflected on the next request. Authors, tags, scheduling, RSS, and translated posts are not included in this release. + +Keep `/blog` and `/blog/{slug}` unique among the site’s page routes. + +## Authoring content and images + +Write post bodies in Markdown using the syntax-highlighted source editor. Its toolbar inserts bold, italic, headings, links, unordered and ordered lists, blockquotes, horizontal rules, inline code, and fenced code blocks; the live preview uses the same server-side renderer as the public detail page. If JavaScript is unavailable, the source textarea remains usable and can still save the post. The detail page supports headings, paragraphs and line breaks, bold and italic text, inline code, links, ordered and unordered lists, blockquotes, horizontal rules, and fenced code blocks. Add a language identifier after the opening fence (for example, `php`) to enable language-specific highlighting. Bash, CSS, diff, HTML/XML, JavaScript/TypeScript, JSON, Markdown, PHP, Python, SQL, and YAML are highlighted; unlabeled blocks are auto-detected from this set, and other language labels remain plain text. Code-block contents are rendered as escaped text before highlighting. Raw HTML is displayed as text; links only render with relative URLs or `http`, `https`, and `mailto` schemes. + +Choose a featured image from the existing Website Manager media library to show it on post cards and detail pages. Use the editor’s inline-image selector to insert Markdown image syntax at the body cursor; image alt text comes from the selected media item. Inline images render only with relative, `http`, or `https` sources. Unsafe link and image protocols are not rendered. \ No newline at end of file diff --git a/extensions/plugi-ext-blog/admin/Classes/CategoriesRepository.php b/extensions/plugi-ext-blog/admin/Classes/CategoriesRepository.php new file mode 100644 index 0000000..b0d1e7f --- /dev/null +++ b/extensions/plugi-ext-blog/admin/Classes/CategoriesRepository.php @@ -0,0 +1,61 @@ +validatedData($data)); + } + + public function update($instance, array $data) + { + $id = is_array($instance) ? ($instance['id'] ?? null) : ($instance->id ?? null); + if (!is_numeric($id) || (int) $id < 1 || $this->findWithId($id) === null) { + return false; + } + + return parent::update(['id' => (int) $id], $this->validatedData($data, (int) $id)); + } + + public function getAllForAdmin(): array + { + return $this->db->select("SELECT * FROM {$this->table} ORDER BY name ASC, id ASC", []); + } + + public function destroy($id) + { + if (!is_numeric($id) || (int) $id < 1) { + return false; + } + + return $this->db->transaction(function () use ($id) { + return (new PostsRepository())->clearCategory((int) $id) && parent::destroy((int) $id); + }); + } + + private function validatedData(array $data, ?int $categoryId = null): array + { + $name = is_string($data['name'] ?? null) ? trim($data['name']) : ''; + $slug = is_string($data['slug'] ?? null) ? trim($data['slug']) : ''; + if ($name === '' || mb_strlen($name) > 255) { + throw new InvalidArgumentException('A category name of 1–255 characters is required.'); + } + if (strlen($slug) > 191 || !preg_match('/^[a-z0-9]+(?:-[a-z0-9]+)*$/', $slug)) { + throw new InvalidArgumentException('Use a lowercase slug with letters, numbers, and single hyphens.'); + } + foreach ($this->findWhere('slug', $slug) as $category) { + if ((int) $category['id'] !== $categoryId) { + throw new InvalidArgumentException('That category slug is already in use.'); + } + } + + return ['name' => $name, 'slug' => $slug]; + } +} \ No newline at end of file diff --git a/extensions/plugi-ext-blog/admin/Classes/PostContentRenderer.php b/extensions/plugi-ext-blog/admin/Classes/PostContentRenderer.php new file mode 100644 index 0000000..e7b8c96 --- /dev/null +++ b/extensions/plugi-ext-blog/admin/Classes/PostContentRenderer.php @@ -0,0 +1,186 @@ +' . nl2br($this->renderInline(implode("\n", $paragraph))) . '
'; + $paragraph = []; + } + if ($listType !== null) { + $class = $listType === 'ul' ? 'list-disc' : 'list-decimal'; + $items = array_map(fn(string $item): string => ''; + $quote = []; + } + }; + + $fence = null; + $fenceLanguage = null; + $codeLines = []; + foreach (preg_split('/\R/', $markdown) ?: [] as $line) { + if ($fence !== null) { + if (preg_match('/^ {0,3}(`{3,}|~{3,})[ \t]*$/', $line, $matches) + && $matches[1][0] === $fence[0] + && strlen($matches[1]) >= strlen($fence)) { + $codeClass = $fenceLanguage === null ? '' : ' class="language-' . $fenceLanguage . '"'; + $blocks[] = '' . nl2br($this->renderInline(implode("\n", $quote))) . '
' . $this->escape(implode("\n", $codeLines)) . '';
+ $fence = null;
+ $fenceLanguage = null;
+ $codeLines = [];
+ continue;
+ }
+ $codeLines[] = $line;
+ continue;
+ }
+
+ if (preg_match('/^ {0,3}(`{3,}|~{3,})(.*)$/', $line, $matches)) {
+ $flush();
+ $fence = $matches[1];
+ $info = trim($matches[2]);
+ $fenceLanguage = preg_match('/^([A-Za-z0-9][A-Za-z0-9_+-]*)(?:[ \t]+|$)/', $info, $languageMatches)
+ ? $languageMatches[1]
+ : null;
+ $codeLines = [];
+ continue;
+ }
+
+ if (trim($line) === '') {
+ $flush();
+ continue;
+ }
+ if (preg_match('/^ {0,3}(#{1,6})[ \t]+(.+?)[ \t]*#*[ \t]*$/', $line, $matches)) {
+ $flush();
+ $level = strlen($matches[1]);
+ $class = [
+ 1 => 'text-3xl font-bold tracking-tight',
+ 2 => 'text-2xl font-bold tracking-tight',
+ 3 => 'text-xl font-semibold',
+ 4 => 'text-lg font-semibold',
+ 5 => 'text-base font-semibold',
+ 6 => 'text-sm font-semibold',
+ ][$level];
+ $blocks[] = '' . $this->escape(implode("\n", $codeLines)) . '';
+ }
+ return implode("\n", $blocks);
+ }
+
+ private function renderInline(string $text): string
+ {
+ $parts = preg_split('/(`[^`\r\n]+`|!\[[^\]\r\n]*\]\([^\s)\r\n]+\)|\[[^\]\r\n]+\]\([^\s)\r\n]+\)|\*\*[^*\r\n]+\*\*|__[^_\r\n]+__|\*[^*\r\n]+\*|_[^_\r\n]+_)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE) ?: [$text];
+ $html = '';
+
+ foreach ($parts as $part) {
+ if ($part === '') {
+ continue;
+ }
+ if ($part[0] === '`') {
+ $html .= '' . $this->escape(substr($part, 1, -1)) . '';
+ } elseif (preg_match('/^!\[([^\]]*)\]\(([^)]+)\)$/', $part, $matches)) {
+ $altText = html_entity_decode($matches[1], ENT_QUOTES | ENT_HTML5, 'UTF-8');
+ $html .= $this->isSafeImageUrl($matches[2])
+ ? 'Blog editor
+Save as a draft or publish the article.
+Blog extension
+Organize posts with one optional category each.
+Create a category above to make it available to posts.
+| Name | Slug | Actions |
|---|---|---|
| = phpb_e($item['name']) ?> | += phpb_e($item['slug']) ?> | +
+
+
+
+
+
+
+ |
+
Blog extension
+= phpb_e((string) $pageResult['total']) ?> post= $pageResult['total'] !== 1 ? 's' : '' ?>
+Create a draft or publish your first article.
+ + + Create your first post + +| Title | +Category | +Status | +Published | +Actions | +
|---|---|---|---|---|
|
+ = phpb_e($post['title']) ?>
+ /= phpb_e($post['slug']) ?>
+ |
+ = phpb_e($post['category_id'] === null ? '—' : ($categoryNames[$post['category_id']] ?? '—')) ?> | += phpb_e(ucfirst($post['status'])) ?> | += phpb_e($post['published_at'] ?? '—') ?> | +
+
+
+
+
+
+
+ |
+
t;){let n=!0,u=!1;if(!l||c>a[l-1].to){let e=L[c-1];e!=s&&(n=!1,u=e==16)}let d=!n&&s==1?[]:null,f=n?r:r+1,p=c;e:for(;;)if(l&&p==a[l-1].to){if(u)break;let m=a[--l];if(!n)for(let e=m.from,n=l;;){if(e==t)break e;if(n&&a[n-1].to==e)e=a[--n].from;else if(L[e-1]==s)break e;else break}d?d.push(m):(m.to{if(t>e&&tt(e)),n.from,t.head>n.from?-1:1);return r==n.from?n:w.cursor(r,r=s&&this.right.forEachLine(e,t,n,o,s,a);else{let c=this.lineAt(s,B.ByPos,n,r,i);e