feat: FAQPage node and widget headings

- FaqBlock prop schema (default true) adds a FAQPage node (@id
  <page URL>#faq, one Question with its acceptedAnswer as plain text per
  item) to the page's one JSON-LD graph through vue-seo-layer's
  useSchemaNode (faqSchema, faqAnswerText)
- headingLevel 0 renders the title as <p class="faq-block__title"> with
  the same id, still labelling the section, for sidebar and footer
  widgets
- smoke: count= and repeatable options; expectations for the widget
  title, one FAQPage node and the answers in the HTML inside details
This commit is contained in:
Jakub Zych
2026-11-04 14:02:05 +01:00
parent cb321e9437
commit f7ec4fe56f
7 changed files with 185 additions and 22 deletions

View File

@@ -3,7 +3,16 @@
// heading (the FAQ name), the server-sanitized description and the items.
// A missing FAQ (404), a failed request or a FAQ without items renders
// nothing, or the #fallback slot; never an empty heading.
//
// headingLevel 0 renders the title as <p class="faq-block__title"> (same id,
// still the section's aria-labelledby) for sidebar and footer widgets, which
// are styled text, not content headings (D-52, UI-SPEC L6). With schema (the
// default) the block adds a FAQPage node to the page's one JSON-LD graph
// (vue-seo-layer's useSchemaNode, D-43); set schema false on every block but
// one when a page shows several FAQs. The answers are in the HTML inside
// <details>, so crawlers read them without opening an item.
import { computed, useId } from 'vue'
import { faqSchema } from '../../utils/faqSchema.ts'
const props = withDefaults(
defineProps<{
@@ -11,14 +20,16 @@ const props = withDefaults(
slug: string
/** ?locale= for the API; the API's default locale when unset. */
locale?: string
/** Level of the heading element, 2 to 4. */
headingLevel?: 2 | 3 | 4
/** Level of the heading element, 2 to 4; 0 renders a styled <p> (widgets). */
headingLevel?: 0 | 2 | 3 | 4
showHeading?: boolean
showDescription?: boolean
/** Only one item open at a time (native exclusive details). */
exclusive?: boolean
/** Add the FAQPage node to the page's JSON-LD graph. */
schema?: boolean
}>(),
{ locale: undefined, headingLevel: 2, showHeading: true, showDescription: true, exclusive: false },
{ locale: undefined, headingLevel: 2, showHeading: true, showDescription: true, exclusive: false, schema: true },
)
defineSlots<{ fallback?(): unknown }>()
@@ -29,9 +40,20 @@ const { data: faq, error } = await useFaq(
)
const items = computed(() => (error.value ? [] : (faq.value?.content ?? [])))
const headingTag = computed(() => `h${Math.min(4, Math.max(2, Math.trunc(Number(props.headingLevel)) || 2))}`)
const headingTag = computed(() => {
const level = Math.trunc(Number(props.headingLevel))
return level === 0 ? 'p' : `h${Math.min(4, Math.max(2, level || 2))}`
})
const description = computed(() => (props.showDescription ? (faq.value?.description ?? '').trim() : ''))
const headingId = `${useId()}-title`
const answer = useSeoAnswer()
const request = useRequestURL()
useSchemaNode(() => {
if (!props.schema || error.value || items.value.length === 0) return null
const url = answer.value?.action === 'ok' && /^https?:\/\//i.test(answer.value.url) ? answer.value.url : `${request.origin}${request.pathname}`
return faqSchema(faq.value, url)
})
</script>
<template>

53
app/utils/faqSchema.ts Normal file
View File

@@ -0,0 +1,53 @@
// The schema.org FAQPage node of a FAQ (D-43): one Question per item with its
// acceptedAnswer as plain text (the answer's HTML without markup), @id
// <page URL>#faq. FaqBlock adds it to the page's one JSON-LD @graph through
// vue-seo-layer's useSchemaNode (prop schema, default true), so a page keeps
// one JSON-LD script; a second block on the page with the same @id merges
// into the first (the first one's questions stay). Pure, so `node --test`
// drives it.
import type { Faq } from './types.ts'
export interface FaqPageNode {
'@type': 'FAQPage'
'@id': string
url: string
mainEntity: Array<{ '@type': 'Question'; name: string; acceptedAnswer: { '@type': 'Answer'; text: string } }>
[key: string]: unknown
}
const NAMED: Record<string, string> = { amp: '&', lt: '<', gt: '>', quot: '"', apos: "'", nbsp: ' ' }
/** Plain text of an answer: tags dropped (script and style with their content), entities decoded, whitespace collapsed. */
export function faqAnswerText(html: string | null | undefined): string {
if (!html) return ''
return html
.replace(/<(script|style)\b[\s\S]*?<\/\1\s*>/gi, ' ')
.replace(/<[^>]*>/g, ' ')
.replace(/&(#x[0-9a-f]+|#[0-9]+|[a-z]+);/gi, (m, code: string) => {
if (code[0] === '#') {
const n = code[1] === 'x' || code[1] === 'X' ? Number.parseInt(code.slice(2), 16) : Number.parseInt(code.slice(1), 10)
return Number.isFinite(n) && n > 0 && n <= 0x10ffff ? String.fromCodePoint(n) : m
}
return NAMED[code.toLowerCase()] ?? m
})
.replace(/\s+/g, ' ')
.trim()
}
/** The FAQPage node of `faq` on the page at `pageUrl`, or null without questions. */
export function faqSchema(faq: Faq | null | undefined, pageUrl: string): FaqPageNode | null {
const items = (faq?.content ?? []).filter((item) => item && typeof item.question === 'string' && item.question.trim() !== '')
if (items.length === 0) return null
const url = pageUrl.split('#')[0] ?? pageUrl
return {
'@type': 'FAQPage',
'@id': `${url}#faq`,
url,
mainEntity: items.map((item) => ({
'@type': 'Question',
name: item.question.trim(),
acceptedAnswer: { '@type': 'Answer', text: faqAnswerText(item.answer) },
})),
}
}