test(embeds): add Go race tests, Blog Editor Vitest suite, and README docs

This commit is contained in:
Jakub Zych
2026-11-28 13:01:26 +01:00
parent 37e89e56db
commit 3fc656da0f
3 changed files with 644 additions and 4 deletions

View File

@@ -276,7 +276,7 @@ The prompt `journal_draft_post` (arguments `topic`, required, and `tone`) asks t
The element follows the admin field type element contract: the `value` property holds the HTML string; the attributes are `field-name`, `record-id`, `locale`, `ui-locale`, `options`, `disabled` and `invalid`; a bubbling `summer-change` event with `detail.value` fires on user edits only. It styles itself only through the admin `--c-*` variables, in `blogeditor.css` under its tag (TipTap's injected stylesheet is off), and never receives or sends credentials.
- **Untouched values.** The element returns the exact string it was given until the user edits; an untouched imported post is saved byte for byte. Setting `value` to the string it currently returns is a no-op. After an edit, top-level blocks that are still equal to the loaded ones keep their original bytes, so editing one paragraph changes only that paragraph. Markup the schema does not model (`footer`, `section`, `aside`, `header`, `nav`, `details`, `style`, `div` and similar blocks, and a `figure` that is not an image with an optional caption) becomes a raw HTML card that shows its source as text and is written back verbatim. On paragraphs, headings, lists, blockquotes, tables, cells, images and links, and on `span` elements, the view-safe unmodelled attributes survive an edit of their block: `class`, `title`, `alt`, `width`, `height`, `colspan`, `rowspan`, `start`, `rel`, `target` and `data-*` (`href` and `src` are kept by the link and image nodes, which check them). Every other attribute, such as `id`, `name`, `style`, `srcset`, `form`, `contenteditable`, `popover`, `itemprop` or an event handler (`on*`), is never rendered into the admin page and is dropped from a block once that block is edited; a `span` left with none of the kept attributes is dropped and its text kept. Untouched documents and untouched blocks keep every stored attribute.
- **Embeds.** The embed button takes a YouTube URL (`youtube.com/watch?v=`, `youtu.be/`, `youtube.com/shorts/`, `youtube.com/embed/`) and stores exactly `<figure data-embed="youtube" data-id="ID"></figure>`, where `ID` matches `^[A-Za-z0-9_-]{11}$`. The admin shows a card and never an iframe; the site layer expands the placeholder. Another provider is one entry in `EMBED_PROVIDERS` (`embed.ts`) and one in the layer.
- **Embeds.** By default, the embed button takes a YouTube URL (`youtube.com/watch?v=`, `youtu.be/`, `youtube.com/shorts/`, `youtube.com/embed/`) and stores `<figure data-embed="youtube" data-id="ID"></figure>`, where `ID` matches `^[A-Za-z0-9_-]{11}$`. When the application registers additional embed providers (via `golem15.journal.embed_providers` YAML config or `classes.EmbedRegistry` on `backpack.App`), `blogRequestOptions` sends `embedProviders` in the element's `options` JSON and the inline embed form adapts to show a provider selector, an ID input, an optional fallback image (including the shared Media Library picker), alt text, caption, and optional `data-aspect` / `data-height` CLS hints. The TipTap `Embed` node preserves fallback `<img>` and `<figcaption>` children on round-trip; both the editor card and the preview pane show the fallback image (or a YouTube thumbnail card) and never an iframe. The site layer expands or lazily hydrates the placeholder.
- **Preview.** The Preview button (`Podgląd` for `ui-locale` `pl`) is off by default, belongs to each element on its own and shows its state through `aria-pressed`. It splits the editor 50/50; when the element is narrower than about 720px, the pane wraps below the editor. The pane renders the current value from the WYSIWYG or Source tab 300 ms (`PREVIEW_DEBOUNCE_MS` in `preview.ts`) after the last change, and each update reloads the frame. The frame is an `<iframe srcdoc>` with an empty `sandbox` attribute, so no scripts run and its origin is opaque. It has `referrerpolicy="no-referrer"` and a Content-Security-Policy meta (`PREVIEW_CSP`) that allows only inline styles and images. The preview copy leaves out `script`, `iframe`, `frame`, `frameset`, `object`, `embed`, `meta`, `base` and `link` elements and event handler attributes. YouTube placeholders show as thumbnail cards loaded from img.youtube.com, never as a player; these and the post's own images are the only network traffic the pane makes. The styling is post-body typography, with the `--c-*` values read from the element at render time or neutral defaults. Previewing never fires `summer-change` and never changes the value, so an untouched post is still saved byte for byte.
- **Headings (J1).** The element reads `headingLevels` from its `options` (the field types serve them per form request, see [headings and links](#headings-and-links)): `[2, 3, 4]` while the H1 setting is on, when the toolbar offers H2 to H4 and a pasted or loaded `<h1>` is parsed as an H2, so the editor never holds an H1; `[1, 2, 3, 4]` while it is off, with an H1 button first. Stored H5 and H6 still parse. The Source tab accepts any HTML; the server decides what is stored.
- **Outline (J2).** A full-width bar between the editor and the status line shows the heading count and, when there are any, the problem count; it starts collapsed and never takes focus. Opened, it lists the headings in document order with their level, indented 12px per level, and flags a skipped level (the page title is the H1), an empty heading, a heading that is only a link and, while the H1 setting is on, an H1 in the Source tab; then the links whose whole text is on the generic list (`genericAnchors` option). It recomputes 300 ms (`OUTLINE_DEBOUNCE_MS`) after a change, from the Source text in the Source tab. Activating a heading puts the caret in it. It never edits the content.
@@ -306,7 +306,7 @@ The plugin registers two admin form field types (`journal.Plugin.FormFieldTypes`
| `blogeditor` | One HTML string in a host column (category content). | `uploadFolder` |
| `mlblogeditor` | One HTML string per content locale, stored like `mlmarkdown`: the default locale in the host column, the others as translations (post content). | `htmlColumn`, `uploadFolder` |
Both types also serve two options per form request (`pact.FormFieldType.RequestOptions`): `headingLevels` (`[2, 3, 4]` while the H1 setting is on, `[1, 2, 3, 4]` while it is off) and `genericAnchors` (the settings' generic link texts, `classes.GenericAnchors`). A `fields.yaml` option of the same name wins.
Both types also serve three options per form request (`pact.FormFieldType.RequestOptions`): `headingLevels` (`[2, 3, 4]` while the H1 setting is on, `[1, 2, 3, 4]` while it is off), `genericAnchors` (the settings' generic link texts, `classes.GenericAnchors`), and `embedProviders` (the registered embed provider descriptors from `classes.EmbedRegistryFrom(app)`). A `fields.yaml` option of the same name wins.
Both types show the element's Preview pane, and previewing never changes what a save stores.
@@ -435,6 +435,38 @@ The request is a `POST` to the URL with `Authorization: Bearer <token>`, `Conten
|-----|----------------------|-------|
| `golem15.journal.tag_pages` | `SUMMER_GOLEM15__JOURNAL__TAG_PAGES` | Whether the host site has tag pages, default `true`. With `false`, `journal-tag` items resolve to nothing, tag descriptions carry empty URLs and tag paths never match, for sites whose frontend turns tag routes off. |
### Embed providers
The Journal plugin pre-registers `youtube` (`id_pattern: ^[A-Za-z0-9_-]{11}$`, `mode: url`) on an app-scoped `*classes.EmbedRegistry` published on `backpack.App` during `Plugin.Register`. Applications can declare additional custom embed providers in YAML under `golem15.journal.embed_providers` (merged during `Plugin.Boot`) and/or register them programmatically in Go via `classes.EmbedRegistryFrom(app).Register(...)`:
```yaml
# config/golem15/journal.yaml (or under golem15.journal in config)
embed_providers:
viz:
name: "Interactive Chart"
id_pattern: "^[a-z0-9][a-z0-9-]{0,63}$"
mode: "id"
placeholder: "acme-chart-slug"
```
```go
if reg := classes.EmbedRegistryFrom(app); reg != nil {
if err := reg.Register(classes.EmbedProvider{
Key: "viz",
Name: "Interactive Chart",
IDPattern: `^[a-z0-9][a-z0-9-]{0,63}$`,
Mode: classes.EmbedModeID,
Placeholder: "acme-chart-slug",
}); err != nil {
return err
}
}
```
- **Boot validation.** Provider keys must match `^[a-z][a-z0-9_-]{0,31}$`, `id_pattern` must compile as a Go regular expression (automatically anchored with `^(?:...)$`), and `mode` must be `"url"` or `"id"` (default `"id"`). Invalid definitions fail boot with an error naming the provider.
- **Pair-aware HTML sanitization.** `FormatHTML`, `SanitizeHTML`, `SanitizeHTMLReport`, and `CheckHTML` (both registry-scoped and default package-level helpers) validate `(data-embed, data-id)` as a pair on `<figure>` so an ID valid for `viz` is never accepted on `data-embed="youtube"` or vice versa. Optional CLS-reservation hints `data-aspect` (`^[1-9][0-9]{0,3}(?:\s*/\s*[1-9][0-9]{0,3}|\.[0-9]{1,4})?$`) and `data-height` (`^[1-9][0-9]{0,3}(?:px)?$`) are allowed only on `<figure>` elements with a valid `(data-embed, data-id)` pair; invalid or mismatched attributes are stripped and reported in `SanitizeHTMLReport` / rejected by `CheckHTML`.
- **Static fallback children.** Fallback markup inside `<figure data-embed="...">` (such as `<img src="/media/..." alt="...">` and `<figcaption>`) is sanitized by the standard UGC policy, stripping `<script>`, `<style>`, `<iframe>`, `<svg>`, event handlers, and unsafe URLs while preserving responsive Media Library fallback images (`classes.EnrichMediaHTML` enriches `/media/<short-id>/...` fallbacks automatically).
## API reference
| Identifier | Description |
@@ -481,7 +513,9 @@ The request is a `POST` to the URL with `Authorization: Bearer <token>`, `Conten
| `models.Settings` | Singleton settings row; `TableName` is `golem15_journal_settings`; `ContentH1ToH2` and `GenericAnchors` hold the Headings and links tab. |
| `models.All` | Models registered by this plugin. |
| `updates.All` | gormigrate steps registered by this plugin. |
| `classes.BlogEditorTypes` | The `blogeditor` and `mlblogeditor` field types: Decode through `SanitizeHTMLReport` with removal notices and, while the H1 setting is on, `ConvertH1` with the conversion notice; Load with the markdown fallback, list text, the image upload and the per-request `headingLevels` and `genericAnchors` options. |
| `classes.BlogEditorTypes` | The `blogeditor` and `mlblogeditor` field types: Decode through `SanitizeHTMLReport` with removal notices and, while the H1 setting is on, `ConvertH1` with the conversion notice; Load with the markdown fallback, list text, the image upload and the per-request `headingLevels`, `genericAnchors`, and `embedProviders` options. |
| `classes.EmbedRegistry`, `classes.NewEmbedRegistry`, `classes.EmbedRegistryFrom` | Thread-safe app-scoped embed provider registry (pre-populated with `youtube`), YAML boot loader (`LoadFromConfig`), pair-aware HTML sanitization methods (`FormatHTML`, `SanitizeHTML`, `SanitizeHTMLReport`, `FormatTranslation`, `CheckHTML`), and `EditorDescriptors` for the Blog Editor. |
| `classes.EmbedProvider`, `classes.EmbedProviderDescriptor`, `classes.EmbedProvidersConfigKey`, `classes.EmbedModeURL`, `classes.EmbedModeID` | Embed provider definition, Blog Editor JSON descriptor, YAML config key `golem15.journal.embed_providers`, and input mode constants. |
| `classes.EditorH1Converted`, `classes.EditorH1ConvertedCategory` | The phrase keys of the H1 conversion notice for a post and for a category. |
| `classes.H1ToH2Key` | `golem15.journal.content_h1_to_h2`, the config key of the H1 setting when no settings row exists. |
| `classes.EffectiveH1ToH2` | The one reader of the H1 setting: the settings row, else the config key, else true. |