feat!: render the post page on the shared SEO layer with one H1 in both Journal H1 modes (tracer)

- extend vue-seo-layer (link dependency during development) and drop
  @nuxtjs/seo: the page head, canonical and the one JSON-LD graph come
  from the CMS resolve answer; the post's own title, excerpt and image
  apply only while there is no CMS answer
- remove the journal sitemap source and the copied shared purge route,
  library and plugin (the SEO layer serves POST /api/__site__/cache/purge);
  the deprecated POST /api/__journal__/cache/purge keeps working
- post title is h1 while the Journal API reports content_h1_to_h2 on
  (or absent) and p.journal-post__title while off; journal.postTitleTag
  ('auto' | 'h1' | 'p') overrides it; body h1 headings are demoted to h2
  while the setting is on (gateJournalHtml/sanitizeJournalHtml demoteH1)
- journal.breadcrumbs option and post-title CSS tokens
- smoke harness: count=, header=, noheader= and resolves lines; the
  fixture API also answers the SEO resolve and site endpoints

BREAKING CHANGE: the layer no longer installs @nuxtjs/seo (no
nuxt-site-config, nuxt-schema-org, robots or sitemap module, no
/api/__sitemap__/journal source, no useSiteConfig()); apps take their
head, sitemaps and robots from the SEO plugin through vue-seo-layer.
This commit is contained in:
Jakub Zych
2026-11-04 13:37:11 +01:00
parent 1dd3176060
commit 289d031201
31 changed files with 1034 additions and 2846 deletions

View File

@@ -238,16 +238,34 @@ const KEEP_ALL: Options = {
nonBooleanAttributes: [],
}
// H1 demotion (the Journal H1 setting, UI-SPEC L7 step 1): every h1 becomes
// an h2 with the same attributes. A tag's own transform replaces the '*'
// one in sanitize-html, so the policy's demotion still filters the values.
const demoted = (filter: (tag: string, attribs: sanitizeHtml.Attributes) => sanitizeHtml.Tag) => (_tag: string, attribs: sanitizeHtml.Attributes): sanitizeHtml.Tag => ({ tagName: 'h2', attribs: filter('h1', attribs).attribs })
const POLICY_DEMOTE_H1: Options = { ...POLICY, transformTags: { ...POLICY.transformTags, h1: demoted(filterAttributeValues) } }
const KEEP_ALL_DEMOTE_H1: Options = { ...KEEP_ALL, transformTags: { h1: demoted((tagName, attribs) => ({ tagName, attribs })) } }
/** Options of the gate. */
export interface JournalHtmlGateOptions {
/**
* Turn every h1 into an h2: true while the Journal API reports its H1
* setting on (content_h1_to_h2), so the page title is the page's only
* H1. Default false, the HTML as written.
*/
demoteH1?: boolean
}
/**
* Sanitizes journal HTML with the Journal policy. Whatever the policy does
* not allow is removed (the text of a removed element is kept, except for
* script, style and similar), so the result is never blank because of one
* stray attribute. Null or empty input gives "". Pure and deterministic: the
* same input gives the same string on the server and in the browser.
* stray attribute. With demoteH1, h1 headings come out as h2. Null or empty
* input gives "". Pure and deterministic: the same input gives the same
* string on the server and in the browser.
*/
export function sanitizeJournalHtml(html: string | null | undefined): string {
export function sanitizeJournalHtml(html: string | null | undefined, options: JournalHtmlGateOptions = {}): string {
if (!html) return ''
return sanitizeHtml(html, POLICY)
return sanitizeHtml(html, options.demoteH1 ? POLICY_DEMOTE_H1 : POLICY)
}
/**
@@ -262,11 +280,13 @@ export function isSafeJournalHtml(html: string | null | undefined): boolean {
/**
* Gate result for a v-html sink. `html` is always the sanitized value and is
* what the sink renders; `safe` reports whether the policy kept the input
* whole (isSafeJournalHtml). It never blanks: unsafe parts are removed, not
* the value. Null or empty input is safe and "".
* whole (isSafeJournalHtml; the H1 demotion is not a removal and never makes
* it false). It never blanks: unsafe parts are removed, not the value. Null
* or empty input is safe and "".
*/
export function gateJournalHtml(html: string | null | undefined): { safe: boolean; html: string } {
export function gateJournalHtml(html: string | null | undefined, options: JournalHtmlGateOptions = {}): { safe: boolean; html: string } {
if (!html) return { safe: true, html: '' }
const clean = sanitizeHtml(html, POLICY)
return { safe: clean === sanitizeHtml(html, KEEP_ALL), html: clean }
const demote = options.demoteH1 === true
const clean = sanitizeHtml(html, demote ? POLICY_DEMOTE_H1 : POLICY)
return { safe: clean === sanitizeHtml(html, demote ? KEEP_ALL_DEMOTE_H1 : KEEP_ALL), html: clean }
}

View File

@@ -1,12 +1,20 @@
// The post page's SSR payload: the post with its body sanitized, without the
// raw `content` source or `translations`, and with related posts as list
// rows. Kept apart from view.ts because it loads the sanitizer, which list
// The post page's SSR payload: the post with its body sanitized (h1 demoted
// to h2 while the Journal H1 setting is on), without the raw `content`
// source or `translations`, and with related posts as list rows. Kept apart from view.ts because it loads the sanitizer, which list
// pages and cards do not need.
import { sanitizeJournalHtml } from './htmlGate.ts'
import type { JournalPostDetail } from './types.ts'
import { toListPost } from './view.ts'
/**
* True while the post's body h1 headings become h2 (the Journal API's
* content_h1_to_h2; absent on an older Journal, which counts as on).
*/
export function demotesH1(post: { content_h1_to_h2?: boolean | null } | null | undefined): boolean {
return post?.content_h1_to_h2 !== false
}
/** A post page's data: the post with its body sanitized and no source or translations. */
export type JournalPostView = JournalPostDetail
@@ -15,7 +23,7 @@ export function toPostView(post: JournalPostDetail): JournalPostView {
...post,
content: null,
translations: null,
content_html: sanitizeJournalHtml(post.content_html),
content_html: sanitizeJournalHtml(post.content_html, { demoteH1: demotesH1(post) }),
related_posts: (post.related_posts ?? []).map(toListPost),
}
}

View File

@@ -1,5 +1,8 @@
// SEO values for Journal pages, fed to useSeoMeta. The site name suffix comes
// from the app's site.name through the SEO title template, not from here.
// SEO values for Journal pages. The page head comes from the CMS through
// vue-seo-layer (useCmsSeo); these values are the fallback a page applies
// only while there is no CMS answer for it (the SEO layer off with seo.cms
// false, the CMS unreachable, or a page the CMS does not know), so a site
// without the SEO plugin keeps its titles and no page gets two of them.
import type { JournalPost } from './types.ts'
@@ -64,6 +67,38 @@ export function postSeoMeta(post: JournalPost, siteUrl?: string | null): Journal
return meta
}
/** True when the resolve answer sets the page head (an ok answer with head values). */
export function cmsHeadPresent(answer: { action?: string; seo?: unknown } | null | undefined): boolean {
return answer?.action === 'ok' && !!answer.seo && typeof answer.seo === 'object'
}
/**
* The origin to make page URLs absolute on: the CMS's page URL (the site
* URL the backend knows) when there is an answer, else `fallback` (the
* request's origin).
*/
export function journalSiteOrigin(answerUrl: string | null | undefined, fallback: string): string {
if (typeof answerUrl === 'string' && /^https?:\/\//i.test(answerUrl)) {
try {
return new URL(answerUrl).origin
} catch {
// fall through
}
}
return fallback
}
/**
* The post title's element (UI-SPEC L6): the option journal.postTitleTag
* when it is 'h1' or 'p', otherwise ('auto') h1 while the Journal H1 setting
* is on (content_h1_to_h2 true or absent) and p while it is off, so the
* page keeps exactly one H1 either way.
*/
export function journalPostTitleTag(option: unknown, contentH1ToH2: boolean | null | undefined): 'h1' | 'p' {
if (option === 'h1' || option === 'p') return option
return contentH1ToH2 === false ? 'p' : 'h1'
}
/** A listing title; pages after the first append the "Page N" string. */
export function listTitle(base: string, page: number, pageTitle: string): string {
return page > 1 ? `${base} · ${pageTitle}` : base

View File

@@ -1,40 +0,0 @@
// Sitemap entries for Journal content. Every loc is built from journal.routes,
// so moving or disabling a view moves or drops its URLs; nothing hard-codes
// a path.
import { flattenCategories } from './client.ts'
import { fillJournalPath } from './routes.ts'
import type { JournalCategory, JournalPost, JournalRoutes } from './types.ts'
export interface JournalSitemapEntry {
loc: string
lastmod?: string
}
/**
* One entry per published post (lastmod = updated_at) at routes.post, and one
* per category of the flattened tree at routes.category. A disabled route
* contributes nothing.
*/
export function buildJournalSitemapEntries(
posts: readonly JournalPost[],
categories: readonly JournalCategory[],
routes: JournalRoutes,
): JournalSitemapEntry[] {
const entries: JournalSitemapEntry[] = []
if (routes.post !== false) {
for (const post of posts) {
if (!post.published || !post.slug) continue
const entry: JournalSitemapEntry = { loc: fillJournalPath(routes.post, { slug: post.slug }) }
if (post.updated_at) entry.lastmod = post.updated_at
entries.push(entry)
}
}
if (routes.category !== false) {
for (const category of flattenCategories(categories)) {
if (!category.slug) continue
entries.push({ loc: fillJournalPath(routes.category, { category: category.slug }) })
}
}
return entries
}

View File

@@ -77,6 +77,12 @@ export interface JournalPostDetail extends JournalPost {
next_post: JournalPostSibling | null
/** Latest 3 in the same categories; empty when the post has no category. */
related_posts: JournalPost[]
/**
* The Journal H1 setting (newer Journal APIs): true while stored
* content has its H1 headings converted to H2, so the post title is the
* page's H1. Absent on an older Journal, which counts as true.
*/
content_h1_to_h2?: boolean
}
/** One node of the GET /categories tree. */
@@ -158,4 +164,19 @@ export interface JournalModuleOptions {
relatedCount: number
/** Whether /rss.xml is served and linked. */
rss: boolean
/**
* The post page title's element: 'auto' (default) follows the Journal H1
* setting (h1 while content_h1_to_h2 is on, p while it is off), 'h1' or
* 'p' force it. NUXT_PUBLIC_JOURNAL_POST_TITLE_TAG at runtime.
*/
postTitleTag: JournalPostTitleTag
/**
* Whether the journal pages render <SeoBreadcrumbs> at the top of their
* content (default true). false for a site whose layout places them.
* NUXT_PUBLIC_JOURNAL_BREADCRUMBS at runtime.
*/
breadcrumbs: boolean
}
/** journal.postTitleTag. */
export type JournalPostTitleTag = 'auto' | 'h1' | 'p'