auto follows content_h1_to_h2 (p only when it is false, h1 when the key is absent), h1 and p force the element, any other option value is auto.
39 lines
1.9 KiB
TypeScript
39 lines
1.9 KiB
TypeScript
// The post title's element (UI-SPEC L6, D-50): journal.postTitleTag x the
|
|
// Journal H1 setting (content_h1_to_h2 on the post answer), so a post page
|
|
// keeps exactly one H1. 'auto' (and any other value, as the module reads
|
|
// the option) follows the setting: h1 while body H1s become H2s or when
|
|
// the answer does not carry the key (a backend before the setting), p
|
|
// while the body keeps its own H1; 'h1' and 'p' force the element.
|
|
import assert from 'node:assert/strict'
|
|
import test from 'node:test'
|
|
import { journalPostTitleTag } from '../app/utils/seo.ts'
|
|
import type { JournalPost } from '../app/utils/types.ts'
|
|
|
|
const settings = [true, false, undefined, null] as const
|
|
|
|
test('auto follows content_h1_to_h2: p only when it is false', () => {
|
|
const want = { true: 'h1', false: 'p', undefined: 'h1', null: 'h1' } as const
|
|
for (const s of settings) assert.equal(journalPostTitleTag('auto', s), want[String(s) as keyof typeof want], `auto x ${s}`)
|
|
})
|
|
|
|
test('h1 and p force the element whatever the setting', () => {
|
|
for (const s of settings) {
|
|
assert.equal(journalPostTitleTag('h1', s), 'h1', `h1 x ${s}`)
|
|
assert.equal(journalPostTitleTag('p', s), 'p', `p x ${s}`)
|
|
}
|
|
})
|
|
|
|
test('any other option value is auto', () => {
|
|
for (const option of [undefined, null, '', 'H1', 'h2', 'span', 1, true, {}]) {
|
|
assert.equal(journalPostTitleTag(option, true), 'h1', `${JSON.stringify(option)} x true`)
|
|
assert.equal(journalPostTitleTag(option, false), 'p', `${JSON.stringify(option)} x false`)
|
|
}
|
|
})
|
|
|
|
test('an answer without the key renders the title as the H1', () => {
|
|
const post = { title: 'Chaga', slug: 'chaga' } as Partial<JournalPost>
|
|
assert.equal(journalPostTitleTag('auto', (post as { content_h1_to_h2?: boolean }).content_h1_to_h2), 'h1')
|
|
const off = { ...post, content_h1_to_h2: false } as Partial<JournalPost>
|
|
assert.equal(journalPostTitleTag('auto', (off as { content_h1_to_h2?: boolean }).content_h1_to_h2), 'p')
|
|
})
|