Skip to content

Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.

feat(content-editor): give a product page its Product schema with the price it states (CE-053, part a) (#1497)

Cihan8f72a8ed0946
diff --git a/web/messages/en.json b/web/messages/en.json
index 51ad71415..5b54a7621 100644
--- a/web/messages/en.json
+++ b/web/messages/en.json
@@ -4451,6 +4451,7 @@
"publish": {
"quiet": "Before you publish has the author, dates and schema (JSON-LD) for your CMS.",
"quietOther": "Before you publish has the author and what your CMS adds for this page type.",
+ "quietProduct": "Before you publish has the Product schema (JSON-LD) with the page's price for your CMS.",
"warnings": "Before you publish: {list}.",
"trust": "Trust {trust}, below 60",
"noAuthor": "no author yet",
@@ -4612,6 +4613,14 @@
"detail": "Often added by your CMS. If not, paste the code below into the page.",
"titleLive": "Article schema with dateModified",
"detailLive": "Often added by your CMS. If not, paste the code below into the page. It gives only the updated date: your page keeps the published date it has.",
+ "product": "Product schema with its price",
+ "productDetail": "Price read from the page: {price}. Check it's the product's. Often added by your shop's theme; if not, paste the code below into the page. Availability, SKU and reviews come from a product feed, once one is connected.",
+ "productNoName": "Product schema needs the product's name",
+ "productNoNameDetail": "The title is empty or still has a placeholder in square brackets: write the product's name there. Without it, search engines show no product result.",
+ "productNoPrice": "Product schema needs the price",
+ "productNoPriceDetail": "The page states no price: add the product's price to the page, or connect your product feed. Without it, search engines show no product result.",
+ "productSeveral": "Product schema needs one price",
+ "productSeveralDetail": "The page states more than one price ({prices}), so the code doesn't guess which is the product's. Write its price once, or its range as one (\"$119–$139\"), or connect your product feed.",
"other": "Schema for this page type",
"otherDetail": "Check what your CMS adds for it (Product, ItemList…). The code Ansvisor gives here is for articles."
},
@@ -4629,6 +4638,7 @@
"linksPending": "Still reading {count, plural, one {# of the page's links} other {# of the page's links}}, so Trust may change.",
"claimsReading": "Still reading the claims in your own words, so Trust may change.",
"pageReading": "Still reading the page for claims that need your proof, so your to-dos may change.",
+ "productNote": "The price, its currency and the images are the page's own (a \"$\" reads as US dollars): check they match your shop before you paste it.",
"datesNote": "The dates are today's: change them if the page goes out on another day.",
"headlineLeftOut": "The title still has a placeholder in square brackets, so the code leaves the headline out. Fill it in, and the headline comes back here.",
"states": {
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_before-publish.test.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_before-publish.test.tsx
index d0428e3c3..2f9e0c651 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_before-publish.test.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_before-publish.test.tsx
@@ -104,6 +104,7 @@ async function render(
title = 'How small teams forecast',
disclosureLine = null as string | null,
disclosureAdded = false,
+ product = null as Parameters<typeof BeforePublish>[0]['product'],
} = {},
) {
await act(async () => {
@@ -122,6 +123,7 @@ async function render(
pageType={pageType}
image={image}
linksPending={linksPending}
+ product={product}
onMarkReady={onMarkReady}
onFixFirst={onFixFirst}
disclosureLine={disclosureLine}
@@ -329,6 +331,61 @@ describe('BeforePublish', () => {
expect(rows().some((row) => row.includes('without a disclosure'))).toBe(false);
});
+ it('gives a product page its Product schema with the price it states, and asks for one it doesn’t (CE-053)', async () => {
+ const product = {
+ description: 'A daypack for wet hikes.',
+ images: ['https://cdn.acme.com/ridge.jpg'],
+ offer: { price: 129, currency: 'USD' },
+ };
+ await render(quality(), { pageType: 'product_page', title: 'Ridge 28 daypack', product });
+ // The price as read, for the person to check.
+ expect(rows().find((row) => row.startsWith('Product schema with its price'))).toContain(
+ "Price read from the page: $129. Check it's the product's.",
+ );
+ expect(jsonLd()).toMatchObject({
+ '@type': 'Product',
+ name: 'Ridge 28 daypack',
+ offers: { '@type': 'Offer', price: '129.00', priceCurrency: 'USD' },
+ });
+ expect(dialog().textContent).toContain('The price, its currency and the images are the page');
+ // No name or no price: no code to paste, and it says why.
+ await render(quality(), {
+ pageType: 'product_page',
+ title: '[name of a real person] daypack',
+ product,
+ });
+ expect(rows().some((row) => row.startsWith("Product schema needs the product's name"))).toBe(
+ true,
+ );
+ expect(rows().some((row) => row.startsWith('Product schema needs the price'))).toBe(false);
+ expect(dialog().querySelector('pre')).toBeNull();
+ await render(quality(), {
+ pageType: 'product_page',
+ title: 'Ridge 28 daypack',
+ product: { ...product, offer: null },
+ });
+ expect(rows().some((row) => row.startsWith('Product schema needs the price'))).toBe(true);
+ // Prices that differ: the code doesn't guess which is the product's.
+ await render(quality(), {
+ pageType: 'product_page',
+ title: 'Ridge 28 daypack',
+ product: { ...product, offer: { several: ['$119', '$139'] } },
+ });
+ expect(rows().find((row) => row.startsWith('Product schema needs one price'))).toContain(
+ 'more than one price ($119, $139)',
+ );
+ expect(dialog().querySelector('pre')).toBeNull();
+ await render(quality(), {
+ pageType: 'product_page',
+ title: 'Ridge 28 daypack',
+ product: { ...product, offer: null },
+ });
+ expect(dialog().querySelector('pre')).toBeNull();
+ expect(
+ [...dialog().querySelectorAll('button')].some((b) => b.textContent === 'Copy JSON-LD'),
+ ).toBe(false);
+ });
+
it('names the brand’s byline as checked on a page type that wants no author (CE-052)', async () => {
await render(
quality({
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_before-publish.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_before-publish.tsx
index 7a39f1fcf..8f75d2207 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_before-publish.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_before-publish.tsx
@@ -14,8 +14,12 @@ import {
import {
articleJsonLd,
isArticlePage,
+ productJsonLd,
+ type PageOfferRead,
jsonLdScript,
localDate,
+ offerText,
+ pageName,
titleHasPlaceholder,
} from '@/lib/content/page-schema';
import { checksOff } from '@/lib/content/quality-profiles';
@@ -90,7 +94,14 @@ export function BeforePublish({
disclosureLine = null,
disclosureAdded = false,
onAddDisclosure,
+ product = null,
}: {
+ /**
+ * Phase 3 (CE-053): a product page's Product schema, from its opening
+ * paragraph, its images and the price it states (pageOffer); null on
+ * other pages.
+ */
+ product?: { description: string | null; images: string[]; offer: PageOfferRead } | null;
/** Opened by Mark ready, by Export, or by Send to CMS. */
mode?: 'markReady' | 'export' | 'send';
paste: ReadyToPaste;
@@ -145,25 +156,43 @@ export function BeforePublish({
.filter((s) => s.gain > 0)
.sort((a, b) => b.gain - a.gain)
: [];
- const article = isArticlePage(pageType);
- const jsonLd = article
- ? jsonLdScript(
- articleJsonLd({
- title,
- // An author only confirmed (00271) isn't named here: left out, as unknown.
- author: authorsRead && !(!author && quality.authorConfirmed?.real) ? author : undefined,
- authorUrl:
- (author &&
- siteTrust?.checks.authorPages.authors.find((a) => a.name === author.name)?.url) ||
- null,
- brandName,
- brandUrl: siteTrust?.checks.organizationSchema.url ?? null,
- image,
- isNew,
- today: localDate(new Date()),
- }),
- )
- : null;
+ const article = !product && isArticlePage(pageType);
+ // Product needs its name and price: without either, no code to paste.
+ const productName = pageName(title);
+ const offer = product?.offer && !('several' in product.offer) ? product.offer : null;
+ const productLd =
+ product &&
+ offer &&
+ productName &&
+ jsonLdScript(
+ productJsonLd({
+ name: productName,
+ description: product.description,
+ images: product.images,
+ brandName,
+ offer,
+ }),
+ );
+ const jsonLd = productLd
+ ? productLd
+ : article
+ ? jsonLdScript(
+ articleJsonLd({
+ title,
+ // An author only confirmed (00271) isn't named here: left out, as unknown.
+ author: authorsRead && !(!author && quality.authorConfirmed?.real) ? author : undefined,
+ authorUrl:
+ (author &&
+ siteTrust?.checks.authorPages.authors.find((a) => a.name === author.name)?.url) ||
+ null,
+ brandName,
+ brandUrl: siteTrust?.checks.organizationSchema.url ?? null,
+ image,
+ isNew,
+ today: localDate(new Date()),
+ }),
+ )
+ : null;
// Added here: it counts once the page is read again.
const [added, setAdded] = useState(false);
// Where the page's type counts it (CE-052): a product page is the brand's own.
@@ -198,18 +227,55 @@ export function BeforePublish({
title: t('author.none'),
detail: t('author.noneDetail', { byline: quality.team }),
},
- ...(article
- ? [
- { state: 'check' as const, title: t('dates.title'), detail: t('dates.detail') },
- isNew
- ? { state: 'check' as const, title: t('schema.title'), detail: t('schema.detail') }
- : {
- state: 'check' as const,
- title: t('schema.titleLive'),
- detail: t('schema.detailLive'),
- },
- ]
- : [{ state: 'check' as const, title: t('schema.other'), detail: t('schema.otherDetail') }]),
+ ...(product
+ ? productLd
+ ? [
+ {
+ state: 'check' as const,
+ title: t('schema.product'),
+ detail: t('schema.productDetail', { price: offerText(offer!) }),
+ },
+ ]
+ : [
+ ...(productName
+ ? []
+ : [
+ {
+ state: 'missing' as const,
+ title: t('schema.productNoName'),
+ detail: t('schema.productNoNameDetail'),
+ },
+ ]),
+ ...(offer
+ ? []
+ : [
+ product.offer && 'several' in product.offer
+ ? {
+ state: 'missing' as const,
+ title: t('schema.productSeveral'),
+ detail: t('schema.productSeveralDetail', {
+ prices: product.offer.several.join(', '),
+ }),
+ }
+ : {
+ state: 'missing' as const,
+ title: t('schema.productNoPrice'),
+ detail: t('schema.productNoPriceDetail'),
+ },
+ ]),
+ ]
+ : article
+ ? [
+ { state: 'check' as const, title: t('dates.title'), detail: t('dates.detail') },
+ isNew
+ ? { state: 'check' as const, title: t('schema.title'), detail: t('schema.detail') }
+ : {
+ state: 'check' as const,
+ title: t('schema.titleLive'),
+ detail: t('schema.detailLive'),
+ },
+ ]
+ : [{ state: 'check' as const, title: t('schema.other'), detail: t('schema.otherDetail') }]),
// Phase 2 (CE-050): where the page puts the brand forward.
...(disclosure
? [
@@ -363,7 +429,9 @@ export function BeforePublish({
{titleHasPlaceholder(title) && (
<p className="text-xs text-amber-700 dark:text-amber-300">{t('headlineLeftOut')}</p>
)}
- <p className="text-xs text-muted-foreground">{t('datesNote')}</p>
+ <p className="text-xs text-muted-foreground">
+ {t(productLd ? 'productNote' : 'datesNote')}
+ </p>
</div>
)}
<DialogFooter className="gap-2 sm:justify-between">
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx
index 9e853c577..084d63568 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx
@@ -91,7 +91,13 @@ import {
} from '@/lib/content/clean-html';
import { PAGE_READ_DELAY_MS, draftTitleFromDoc, safeLinkHref } from '@/lib/content/editor';
import { JsonLd } from '@/lib/content/json-ld-node';
-import { firstImage, isArticlePage } from '@/lib/content/page-schema';
+import {
+ firstImage,
+ isArticlePage,
+ openingParagraph,
+ pageName,
+ pageOffer,
+} from '@/lib/content/page-schema';
import { PLACEHOLDER, pagePlaceholders, placeholderIn } from '@/lib/content/placeholders';
import { HANDOFF_KEYS, handOffItems } from '@/lib/content/hand-off';
import {
@@ -951,6 +957,21 @@ export function DraftEditor({
// asks for a qualified reviewer, one of Authors.
const regulated = sourcesOn ? (pageRead.read?.regulated ?? null) : null;
const pageReviewer = authorsRead?.authors.find((a) => a.id === reviewerId) ?? null;
+ // Phase 3 (CE-053): a product page's Product schema in Before you publish,
+ // from its opening paragraph, its own images (else its first) and the
+ // price it states.
+ const productSchema = useMemo(() => {
+ if (profile !== 'product') return null;
+ const own = images
+ .filter((image) => ownImages?.has(image.src) && /^https:\/\//i.test(image.src))
+ .map((image) => image.src);
+ const first = firstImage(pageHtml);
+ return {
+ description: openingParagraph(pageHtml),
+ images: own.length ? own : first ? [first] : [],
+ offer: pageOffer(pageBlocks.map((block) => block.text)),
+ };
+ }, [profile, images, ownImages, pageHtml, pageBlocks]);
// Confirm: only the person can tell their own image from stock.
const [confirmingImages, setConfirmingImages] = useState(false);
// Quality's Confirm on a customer case (CE-052): the person's statement
@@ -3485,6 +3506,9 @@ export function DraftEditor({
noDisclosure:
aiChecks?.disclosure?.found === false && !checksOff(profile).has('disclosure'),
article: isArticlePage(pageType),
+ // Only where Before you publish has its code: a name and one price.
+ product:
+ !!productSchema?.offer && !('several' in productSchema.offer) && !!pageName(title),
onReview: () => {
setExportOpen(null);
openBeforePublish('export', exportOpen.destination);
@@ -3537,6 +3561,7 @@ export function DraftEditor({
disclosureLine={htmlSource === null ? disclosureLine : null}
disclosureAdded={disclosureOnPage}
onAddDisclosure={addLineUnderAnswer}
+ product={productSchema}
onMarkReady={(anyway) => {
setPublishOpen(null);
void markReady(anyway ?? undefined);
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_export-dialog.test.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_export-dialog.test.tsx
index 1e2e75669..c1d257a20 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_export-dialog.test.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_export-dialog.test.tsx
@@ -146,6 +146,13 @@ describe('ExportDialog', () => {
expect(dialog().textContent).toContain(
'Before you publish has the author and what your CMS adds for this page type.Review',
);
+ // A product page in Phase 3 (CE-053): its Product schema.
+ await render({
+ publish: { trustBelow: null, noAuthor: false, article: false, product: true, onReview },
+ });
+ expect(dialog().textContent).toContain(
+ "Before you publish has the Product schema (JSON-LD) with the page's price for your CMS.Review",
+ );
});
it('holds the export while a placeholder is open, and fills it or leaves its sentence out', async () => {
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_export-dialog.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_export-dialog.tsx
index 892afdd2c..bbaa51675 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_export-dialog.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_export-dialog.tsx
@@ -206,6 +206,8 @@ export function ExportDialog({
noDisclosure?: boolean;
/** An article: Before you publish has its dates and JSON-LD too. */
article: boolean;
+ /** A product page (Phase 3, CE-053): Before you publish has its Product JSON-LD. */
+ product?: boolean;
onReview: () => void;
} | null;
}) {
@@ -636,12 +638,14 @@ function PublishLine({
noAuthor,
noDisclosure = false,
article,
+ product = false,
onReview,
}: {
trustBelow: number | null;
noAuthor: boolean;
noDisclosure?: boolean;
article: boolean;
+ product?: boolean;
onReview: () => void;
}) {
const t = useTranslations('contentEditor.editor.exportDialog.publish');
@@ -663,7 +667,7 @@ function PublishLine({
<p className="min-w-0 flex-1">
{warnings.length
? t('warnings', { list: warnings.join(' · ') })
- : t(article ? 'quiet' : 'quietOther')}
+ : t(article ? 'quiet' : product ? 'quietProduct' : 'quietOther')}
</p>
<Button
variant="outline"
diff --git a/web/src/lib/content/brand-facts.test.ts b/web/src/lib/content/brand-facts.test.ts
index 78f580d2a..469148459 100644
--- a/web/src/lib/content/brand-facts.test.ts
+++ b/web/src/lib/content/brand-facts.test.ts
@@ -4,6 +4,7 @@ import {
factMismatches,
factValueText,
pagePrices,
+ priceRanges,
type BrandFact,
} from './brand-facts';
@@ -108,6 +109,55 @@ describe('pagePrices', () => {
// A dash after a plan's name isn't a range.
expect(pagePrices('Pro – $49 per month')[0]).toMatchObject({ amount: 49 });
expect(pagePrices('Since 2019 199 € per month')[0]).toMatchObject({ amount: 199 });
+ // A currency after an amount isn't the next one's: "Ridge 28 $129" is $129;
+ // one after its own amount stays with it.
+ expect(pagePrices('Ridge 28 $129').map((p) => p.amount)).toEqual([129]);
+ expect(pagePrices('129,00 € 159,00 €').map((p) => p.amount)).toEqual([129, 159]);
+ expect(pagePrices('Pro 49 € 10 Nutzer').map((p) => p.amount)).toEqual([49]);
+ // A code after a sign says whose dollar it is.
+ expect(pagePrices('$129.00 CAD')[0]).toMatchObject({
+ text: '$129.00 CAD',
+ amount: 129,
+ currency: 'CAD',
+ });
+ // With its ends, a range is read as two prices, for priceRanges' readers.
+ expect(pagePrices('Ridge 28 – $129').map((p) => p.amount)).toEqual([]);
+ expect(pagePrices('Ridge 28 – $129', { rangeEnds: true }).map((p) => p.amount)).toEqual([129]);
+ });
+});
+
+describe('priceRanges', () => {
+ it('reads a range’s two ends in one currency, and whether it is written as ranges are', () => {
+ const ends = (sentence: string) =>
+ priceRanges(sentence).map((r) => [r.low, r.high, r.currency, r.sure]);
+ expect(ends('Paying $10–$20 or between $30 and $40.')).toEqual([
+ [10, 20, 'USD', true],
+ [30, 40, 'USD', true],
+ ]);
+ expect(ends('From $119-139, or 119–139 € in Europe.')).toEqual([
+ [119, 139, 'USD', true],
+ [119, 139, 'EUR', true],
+ ]);
+ expect(ends('De 49 € à 99 €, ou 49 TL ile 99 TL.')).toEqual([
+ [49, 99, 'EUR', true],
+ [49, 99, 'TRY', true],
+ ]);
+ // One currency with a spaced dash or a word may be a range, or a price beside a number.
+ expect(ends('Preis: 119 – 139 €, $119 to 139, 999 - 1.299 TL')).toEqual([
+ [119, 139, 'EUR', false],
+ [119, 139, 'USD', false],
+ [999, 1299, 'TRY', false],
+ ]);
+ // A name's number before a price, a number after one with a word: no range.
+ expect(ends('Ridge 28 – $129')).toEqual([]);
+ expect(ends('Only $19 - 100% organic cotton')).toEqual([]);
+ expect(ends('$25-30-day returns, $5 a 12-pack')).toEqual([]);
+ expect(ends('$129 – 150 reviews, €20 – 30 Tage Rückgaberecht')).toEqual([]);
+ expect(ends('Kids 10-12 $129')).toEqual([]);
+ // Two currencies, a scale, high before low: no range.
+ expect(ends('$10–€20')).toEqual([]);
+ expect(ends('Raised $1–$2M')).toEqual([]);
+ expect(ends('$129 – 2 colors')).toEqual([]);
});
});
diff --git a/web/src/lib/content/brand-facts.ts b/web/src/lib/content/brand-facts.ts
index acb43ce3a..fac6ca4fc 100644
--- a/web/src/lib/content/brand-facts.ts
+++ b/web/src/lib/content/brand-facts.ts
@@ -132,11 +132,17 @@ const MARK = `(?:${CURRENCY_MARKS.map(([m]) => escape(m)).join('|')})`;
const CODE = `(?<![\\p{L}])(?:${ISO.join('|')}|TL)(?![\\p{L}])`;
/** Thousands by a comma or a point, or by a space when the currency comes after ("1 299 €"). */
const amount = (groups: string) =>
- String.raw`(?<![\d.,])(?:\d{1,3}(?:${groups}\d{3})+(?:[.,]\d{1,2})?|\d+(?:[.,]\d{1,2})?)(?![\d]|[.,]\d)`;
+ String.raw`(?<![\d.,'’])(?:\d{1,3}(?:${groups}\d{3})+(?:[.,]\d{1,2})?|\d+(?:[.,]\d{1,2})?)(?![\d]|[.,'’]\d)`;
+/** A currency after an amount, not one written onto the next ("Ridge 28 $129": $129, not 28 $). */
const PRICE = new RegExp(
- `(?<before>${MARK}|${CODE})\\s?(?<a>${amount('[.,]')})|(?<b>${amount('[.,\\s]')})\\s?(?<after>${MARK}|${CODE}|zł)`,
+ `(?<before>${MARK}|${CODE})\\s?(?<a>${amount("[.,'’]")})|(?<b>${amount("[.,'’\\s]")})\\s?(?<after>${MARK}|${CODE}|zł)(?!\\d)`,
'gu',
);
+/** "$129.00 CAD": the code after a sign says whose dollar it is. */
+const CODE_AFTER = new RegExp(`^\\s?(?<code>${ISO.join('|')})(?![\\p{L}])`, 'u');
+/** "CAD $129", "MX$1,299": a code before a dollar sign says whose too. */
+const CODE_BEFORE = /(?<!\p{L})(USD|CAD|AUD|NZD|SGD|HKD|MXN|MX|HK)\s?$/u;
+const DOLLAR_OF: Record<string, string> = { MX: 'MXN', HK: 'HKD' };
const MAGNITUDE =
/^\s*(?:[kmb](?!\p{L})|bn\b|mn\b|thousand|million|billion|milyon|milyar|mio\b|mrd\b)/iu;
/** "$10–$20", "between $10 and $20": a range, its two ends (a number before the word or dash). */
@@ -159,9 +165,10 @@ const BILLED_YEAR =
/billed\s+(?:annually|yearly)|(?:annual|yearly)\s+(?:billing|plan)|paid\s+(?:annually|yearly)|yıllık\s+ödeme|jährliche\s+zahlung/iu;
const BILLED_MONTH =
/billed\s+monthly|monthly\s+billing|paid\s+monthly|aylık\s+ödeme|monatliche\s+zahlung/iu;
-const FROM = /\b(?:from|start(?:s|ing)?\s+(?:at|from)|ab|à\s+partir\s+de|desde)\s*$/iu;
-/** "₺499'dan başlayan": Turkish says it after the price. */
-const FROM_AFTER = /^['’]?(?:den|dan|ten|tan)\s+(?:başlayan|itibaren)/iu;
+const FROM =
+ /\b(?:from|start(?:s|ing)?\s+(?:at|from)|as\s+low\s+as|(?:begins?|starts?)\s+at|ab|[àa]\s+partire?\s+d[ae]|desde|vanaf)(?:\s+(?:just|only|nur|seulement|solo|slechts))?\s*$/iu;
+/** "₺499'dan başlayan", "1.299 TL'den başlıyor": Turkish says it after the price. */
+const FROM_AFTER = /^['’]?(?:den|dan|ten|tan)\s+(?:başlayan|başlıyor|itibaren)/iu;
const currencyOf = (mark: string) =>
mark === 'TL'
@@ -172,7 +179,12 @@ const currencyOf = (mark: string) =>
/** An amount as written: a separator before exactly three digits groups thousands. */
const amountOf = (token: string) => {
- const value = Number(token.replace(/[.,\s](?=\d{3}(?!\d))/g, '').replace(',', '.'));
+ const value = Number(
+ token
+ .replace(/['’]/g, '')
+ .replace(/[.,\s](?=\d{3}(?!\d))/g, '')
+ .replace(',', '.'),
+ );
return Number.isFinite(value) ? value : null;
};
@@ -184,13 +196,21 @@ export interface PagePrice {
period: 'month' | 'year' | null;
from: boolean;
at: number;
+ /**
+ * Its digits or its sign may be another number's: a space between digits
+ * ("PlayStation 5 549,99 €" reads 5 549,99), or a sign after a whole
+ * amount with a number next ("Ridge 28 € 129").
+ */
+ loose: boolean;
}
/**
* The prices a sentence gives, the way the API reads a brand's: a scale
- * ("$2M"), a range ("$10–$20") and "$10 off" are none.
+ * ("$2M"), a range ("$10–$20") and "$10 off" are none. With `rangeEnds`, a
+ * range's ends are read too, for a reader that takes its ranges apart
+ * (priceRanges): "Ridge 28 – $129" is a price after all.
*/
-export function pagePrices(sentence: string): PagePrice[] {
+export function pagePrices(sentence: string, { rangeEnds = false } = {}): PagePrice[] {
const out: PagePrice[] = [];
for (const m of sentence.matchAll(PRICE)) {
const g = m.groups!;
@@ -198,22 +218,83 @@ export function pagePrices(sentence: string): PagePrice[] {
const before = sentence.slice(Math.max(0, m.index - 24), m.index);
const after = sentence.slice(end, end + 40);
if (MAGNITUDE.test(after) || /^\s*off(?!\p{L})/iu.test(after)) continue;
- if (RANGE_AFTER.test(after) || RANGE_BEFORE.test(before)) continue;
- const currency = currencyOf(g.before ?? g.after);
+ if (!rangeEnds && (RANGE_AFTER.test(after) || RANGE_BEFORE.test(before))) continue;
+ const code = g.before && !ISO.includes(g.before) ? CODE_AFTER.exec(after) : null;
+ const codeBefore = g.before === '$' ? CODE_BEFORE.exec(before) : null;
+ const dollar = codeBefore && (DOLLAR_OF[codeBefore[1]] ?? codeBefore[1]);
+ const currency = code ? code.groups!.code : (dollar ?? currencyOf(g.before ?? g.after));
const value = amountOf(g.a ?? g.b);
if (!currency || value === null) continue;
out.push({
- text: m[0].trim(),
+ text: code ? `${m[0].trim()}${code[0]}` : m[0].trim(),
amount: value,
currency,
period: MONTH.test(after) ? 'month' : YEAR.test(after) ? 'year' : null,
from: FROM.test(before) || FROM_AFTER.test(after),
at: m.index,
+ // Cents tie a sign to its amount: "129,00 € 2 Jahre Garantie" is 129 €.
+ loose:
+ /\s/u.test(g.b ?? '') ||
+ (!!g.after && /^\s\d/u.test(after) && !/[.,]\d{1,2}$/u.test(g.b ?? '')),
});
}
return out;
}
+/** A range's end: an amount with its currency before or after it, or neither ("$119–139"). */
+const rangeEnd = (name: string) =>
+ `(?:(?<${name}Before>${MARK}|${CODE})\\s?)?(?<${name}>${amount("[.,'’]")})(?:\\s?(?<${name}After>${MARK}|${CODE}|zł)(?!\\d))?`;
+const PRICE_RANGE = new RegExp(
+ `${rangeEnd('low')}(?<join>\\s?(?:[–—-]|(?<=\\s)(?:to|bis|à|a|ila|ile|and|und|et|y)(?=\\s))\\s?)${rangeEnd('high')}`,
+ 'giu',
+);
+
+/** A range a sentence gives ("$119–$139", "119–139 €", "€20 to €30"): its two ends, in one currency. */
+export interface PriceRange {
+ text: string;
+ low: number;
+ high: number;
+ currency: string;
+ at: number;
+ /**
+ * Written as ranges are: both ends with their currency, or one and a
+ * dash with no space ("$119–139"). Else ("$119 – 139", "119 bis 139 €")
+ * it may be one, or a price beside a number.
+ */
+ sure: boolean;
+}
+
+/**
+ * The price ranges a sentence gives (the ones pagePrices leaves out): the
+ * same currency on both ends, the low end first, and whether it's sure
+ * (PriceRange). A number before a price ("Ridge 28 – $129") or one after
+ * it with a word ("$129 – 150 reviews", "$19 - 100% cotton", "$25-30-day
+ * returns") is no range.
+ */
+export function priceRanges(sentence: string): PriceRange[] {
+ const out: PriceRange[] = [];
+ for (const m of sentence.matchAll(PRICE_RANGE)) {
+ const g = m.groups!;
+ const marks = [g.lowBefore, g.lowAfter, g.highBefore, g.highAfter].filter(Boolean);
+ const currencies = new Set(marks.map((mark) => currencyOf(mark)));
+ const low = amountOf(g.low);
+ const high = amountOf(g.high);
+ const after = sentence.slice(m.index + m[0].length, m.index + m[0].length + 40);
+ const lowMarked = !!(g.lowBefore || g.lowAfter);
+ const highMarked = !!(g.highBefore || g.highAfter);
+ if (currencies.size !== 1 || currencies.has(null) || MAGNITUDE.test(after)) continue;
+ if (low === null || high === null || !(low < high)) continue;
+ if (!lowMarked && g.highBefore) continue;
+ // "$19 - 100% cotton", "$25-30-day returns", "€20 – 30 Tage Rückgaberecht".
+ if (!highMarked && /^(?:%|-\p{L})/u.test(after)) continue;
+ if (!highMarked && /\s/u.test(g.join) && /^\s+\p{L}/u.test(after)) continue;
+ const sure =
+ (lowMarked && highMarked) || (/^[–—-]$/u.test(g.join) && !/^(?:%|-?\p{L})/u.test(after));
+ out.push({ text: m[0], low, high, currency: [...currencies][0]!, at: m.index, sure });
+ }
+ return out;
+}
+
/** A page's fact that differs from an approved one: Trust's suggestion names both. */
export interface FactMismatch {
kind: 'price' | 'trial' | 'founded';
diff --git a/web/src/lib/content/page-schema.test.ts b/web/src/lib/content/page-schema.test.ts
index 186b52627..1afaa7d8d 100644
--- a/web/src/lib/content/page-schema.test.ts
+++ b/web/src/lib/content/page-schema.test.ts
@@ -6,7 +6,11 @@ import {
isArticlePage,
jsonLdScript,
localDate,
+ pageName,
titleHasPlaceholder,
+ openingParagraph,
+ pageOffer,
+ productJsonLd,
} from './page-schema';
/** Before you publish's JSON-LD (Quality spec §7, CE-041): from the page's facts, nothing made up. */
@@ -89,6 +93,9 @@ describe('articleJsonLd', () => {
expect(result.author).toMatchObject({ name: 'Ada Lovelace' });
// A footnote, a checkbox or "[sic]" is no placeholder.
expect(titleHasPlaceholder('Pipeline [1] and [x] [sic]')).toBe(false);
+ expect(pageName(' Ridge 28 [1] ')).toBe('Ridge 28 [1]');
+ expect(pageName('Ridge [model name]')).toBeNull();
+ expect(pageName(' ')).toBeNull();
expect(titleHasPlaceholder('Interview with [name of a real person]')).toBe(true);
// The match keeps no state between titles.
expect(titleHasPlaceholder('Interview with [name of a real person]')).toBe(true);
@@ -137,3 +144,258 @@ describe('localDate', () => {
expect(localDate(new Date(2026, 0, 5, 23, 30))).toBe('2026-01-05');
});
});
+
+describe('pageOffer (Phase 3, CE-053)', () => {
+ const ridge = { price: 129, currency: 'USD' };
+
+ it('reads one price as the Offer, a range written as one as low and high', () => {
+ expect(pageOffer(['The Ridge 28 costs $129.'])).toEqual(ridge);
+ expect(pageOffer(['Small: $129.', 'In stock. Small: $129.'])).toEqual(ridge);
+ expect(pageOffer(['Sizes run $119–$139.'])).toEqual({ low: 119, high: 139, currency: 'USD' });
+ expect(pageOffer(['From $119-139, by size.', 'The large is $139.'])).toEqual({
+ low: 119,
+ high: 139,
+ currency: 'USD',
+ });
+ expect(pageOffer(['Zwischen 119 € und 139 €.'])).toEqual({
+ low: 119,
+ high: 139,
+ currency: 'EUR',
+ });
+ // A tax-inclusive price is the price.
+ expect(pageOffer(['129 € inkl. MwSt.'])).toEqual({ price: 129, currency: 'EUR' });
+ expect(pageOffer(['Fiyatı 1.299 TL, KDV dahil.'])).toEqual({ price: 1299, currency: 'TRY' });
+ expect(pageOffer(['$129, tax included.'])).toEqual(ridge);
+ // Words that name the product aren't another bill.
+ expect(pageOffer(['Extra large: $129.'])).toEqual(ridge);
+ expect(pageOffer(['The over-ear Ridge headphones cost $129.'])).toEqual(ridge);
+ expect(pageOffer(['A 2-person tent for $129.'])).toEqual(ridge);
+ expect(pageOffer(['Ridge 28 for $129.'])).toEqual(ridge);
+ expect(pageOffer(['Ridge Plus: $129.'])).toEqual(ridge);
+ expect(pageOffer(['Comes with all accessories for $129.'])).toEqual(ridge);
+ // A model's number before its price is no price, nor a range.
+ expect(pageOffer(['Ridge 28 $129.'])).toEqual(ridge);
+ expect(pageOffer(['Ridge 28 – $129.'])).toEqual(ridge);
+ expect(pageOffer(['$129 – 150 reviews.'])).toEqual(ridge);
+ // Free shipping is no bill.
+ expect(pageOffer(['The Ridge 28 with free shipping costs $129.'])).toEqual(ridge);
+ expect(pageOffer(['Versandkostenfrei für nur 129 €.'])).toEqual({
+ price: 129,
+ currency: 'EUR',
+ });
+ expect(pageOffer(['Kostenloser Versand für 129 €.'])).toEqual({ price: 129, currency: 'EUR' });
+ expect(pageOffer(['Ücretsiz kargo ile 999 TL.'])).toEqual({ price: 999, currency: 'TRY' });
+ // An old price that is the page's only one is its price.
+ expect(pageOffer(['Regular price $129.00'])).toEqual(ridge);
+ // A currency after its amount stays with it; a code after a sign says whose dollar.
+ expect(pageOffer(['129,00 € 2 Jahre Garantie.'])).toEqual({ price: 129, currency: 'EUR' });
+ expect(pageOffer(['1.299,00 TL 3 taksit imkânı.'])).toEqual({ price: 1299, currency: 'TRY' });
+ // A code before a dollar sign, a Swiss apostrophe.
+ expect(pageOffer(['CAD $129.00'])).toEqual({ price: 129, currency: 'CAD' });
+ expect(pageOffer(['MX$1,299'])).toEqual({ price: 1299, currency: 'MXN' });
+ expect(pageOffer(["CHF 1'299.–"])).toEqual({ price: 1299, currency: 'CHF' });
+ expect(pageOffer(["1'299.00 CHF"])).toEqual({ price: 1299, currency: 'CHF' });
+ expect(pageOffer(['$129.00 CAD'])).toEqual({ price: 129, currency: 'CAD' });
+ expect(pageOffer(['Galaxy S24+: $999.99 (Regular price $1,099.99)'])).toEqual({
+ price: 999.99,
+ currency: 'USD',
+ });
+ expect(pageOffer(['Heats up in 3 min. $129 (reg. $159).'])).toEqual(ridge);
+ expect(pageOffer(['Kids 10-12 $129.'])).toEqual(ridge);
+ });
+
+ it('keeps the price beside an amount that isn’t it', () => {
+ // A sale: the price now, not the old one.
+ expect(pageOffer(['Now $129, was $159.'])).toEqual(ridge);
+ expect(pageOffer(['$129 (was $159).'])).toEqual(ridge);
+ expect(pageOffer(['$129 (reg. $159).'])).toEqual(ridge);
+ expect(pageOffer(['$129 instead of $159.'])).toEqual(ridge);
+ expect(pageOffer(['Regular price $159.00 Sale price $129.00'])).toEqual(ridge);
+ expect(pageOffer(['129,00 € 159,00 € statt'])).toEqual({ several: ['129,00 €', '159,00 €'] });
+ expect(pageOffer(['Original price $159. Now $129.'])).toEqual(ridge);
+ expect(pageOffer(['Au lieu de 159 €, 129 €.'])).toEqual({ price: 129, currency: 'EUR' });
+ expect(pageOffer(['Antes 159 €, ahora 129 €.'])).toEqual({ price: 129, currency: 'EUR' });
+ expect(pageOffer(['Bisher 159 €, jetzt 129 €.'])).toEqual({ price: 129, currency: 'EUR' });
+ expect(pageOffer(['Eski fiyat 1.299 TL, şimdi 999 TL.'])).toEqual({
+ price: 999,
+ currency: 'TRY',
+ });
+ expect(pageOffer(['₺1.299 yerine ₺999.'])).toEqual({ price: 999, currency: 'TRY' });
+ // Savings, worth and rewards.
+ expect(pageOffer(['Save $30: it’s $129 this week.'])).toEqual(ridge);
+ expect(pageOffer(['$129.', 'Save up to $30.'])).toEqual(ridge);
+ expect(pageOffer(['$129.', 'You save 19% ($30).'])).toEqual(ridge);
+ expect(pageOffer(['$129.', 'Free gift worth $30.'])).toEqual(ridge);
+ expect(pageOffer(['$129 ($30 value).'])).toEqual(ridge);
+ expect(pageOffer(['$129.', 'Earn $13 in rewards.'])).toEqual(ridge);
+ expect(pageOffer(['$129.', 'Get up to $200 trade-in credit.'])).toEqual(ridge);
+ expect(pageOffer(['$129.', 'Get a $10 coupon.'])).toEqual(ridge);
+ // Thresholds and shipping.
+ expect(pageOffer(['It costs $129 and ships free on orders over $50.'])).toEqual(ridge);
+ expect(pageOffer(['Free shipping on orders over $50.', 'It costs $129.'])).toEqual(ridge);
+ expect(pageOffer(['Min. order $50.', 'Price $129.'])).toEqual(ridge);
+ expect(pageOffer(['$129 with free shipping.'])).toEqual(ridge);
+ expect(pageOffer(['Shipping: $5.99.', 'Price: $129.'])).toEqual(ridge);
+ expect(pageOffer(['$129 + $9.95 shipping.'])).toEqual(ridge);
+ expect(pageOffer(['Plus $9.95 shipping.', '$129.'])).toEqual(ridge);
+ expect(pageOffer(['129,00 € inkl. MwSt., zzgl. 4,95 € Versand'])).toEqual({
+ price: 129,
+ currency: 'EUR',
+ });
+ expect(pageOffer(['Kostenloser Versand ab 50 €.', 'Preis: 129 €.'])).toEqual({
+ price: 129,
+ currency: 'EUR',
+ });
+ expect(pageOffer(['Free shipping on all orders $50+', '$129.'])).toEqual(ridge);
+ expect(pageOffer(['Kostenloser Versand für Bestellungen über 50 €.', 'Preis: 129 €.'])).toEqual(
+ { price: 129, currency: 'EUR' },
+ );
+ expect(pageOffer(['Envío gratuito en pedidos superiores a 50 €.', '129 €.'])).toEqual({
+ price: 129,
+ currency: 'EUR',
+ });
+ expect(
+ pageOffer(['Livraison gratuite pour toute commande supérieure à 50 €.', '129 €.']),
+ ).toEqual({ price: 129, currency: 'EUR' });
+ // A member's, a subscriber's or a code's price, a unit price, PayPal, a balance.
+ expect(pageOffer(['Regular price $129, or $116 with Subscribe & Save.'])).toEqual(ridge);
+ expect(pageOffer(['Regular price $129.00 Member price $116.10'])).toEqual(ridge);
+ expect(pageOffer(['$129. With code SPRING10: $116.10'])).toEqual(ridge);
+ expect(pageOffer(['Regular price $129.00 Unit price $12.90 / per 100g'])).toEqual(ridge);
+ expect(pageOffer(['UVP 129,00 € Grundpreis 25,80 € / 1 kg'])).toEqual({
+ price: 129,
+ currency: 'EUR',
+ });
+ expect(pageOffer(['Regular price $129.00', 'Pay in 4 with PayPal: $32.25'])).toEqual(ridge);
+ expect(pageOffer(['Liste fiyatı 1.599 TL, 300 TL indirimle.'])).toEqual({
+ price: 1599,
+ currency: 'TRY',
+ });
+ expect(
+ pageOffer(['$129.', 'Pre-order with a $50 deposit, balance of $79 at shipping.']),
+ ).toEqual(ridge);
+ expect(pageOffer(['Free shipping on orders between $50 and $100.', '$129.'])).toEqual(ridge);
+ // Savings and installments in other languages.
+ expect(pageOffer(['129 €.', 'Sie sparen 30 €.'])).toEqual({ price: 129, currency: 'EUR' });
+ expect(pageOffer(['129 €.', 'Économisez 30 €.'])).toEqual({ price: 129, currency: 'EUR' });
+ expect(pageOffer(['129 €, o 3 plazos de 43 €.'])).toEqual({ price: 129, currency: 'EUR' });
+ expect(pageOffer(['129 €, oder 3 Raten à 43 €.'])).toEqual({ price: 129, currency: 'EUR' });
+ expect(pageOffer(['500 TL üzeri kargo bedava.', 'Fiyatı 999 TL.'])).toEqual({
+ price: 999,
+ currency: 'TRY',
+ });
+ // Financing, a unit price, a bundle, an add-on.
+ expect(pageOffer(['$129, or 4 interest-free payments of $32.25 with Klarna.'])).toEqual(ridge);
+ expect(pageOffer(['$129, or as low as $12/mo with Affirm.'])).toEqual(ridge);
+ expect(pageOffer(['$129 ($0.50/oz).'])).toEqual(ridge);
+ expect(pageOffer(['$129 each, or 2 for $239.'])).toEqual(ridge);
+ expect(pageOffer(['$129.', 'Add a waterproof rain cover for $19.'])).toEqual(ridge);
+ expect(pageOffer(['$129.', 'Accessories: rain cover $19.'])).toEqual(ridge);
+ expect(pageOffer(['$129.', 'Rain cover ($19, sold separately).'])).toEqual(ridge);
+ });
+
+ it('guesses none: no price, prices that differ, or a range that isn’t one', () => {
+ expect(pageOffer(['A sturdy daypack for wet hikes.'])).toBeNull();
+ expect(pageOffer(['Save $30 this week.'])).toBeNull();
+ // A subscription's price is a plan's, not the product's.
+ expect(pageOffer(['$12 a month.'])).toBeNull();
+ expect(pageOffer(['$10–$20 a month.'])).toBeNull();
+ expect(pageOffer(['10–20 € pro Monat.'])).toBeNull();
+ expect(pageOffer(['$10–$20 monthly.'])).toBeNull();
+ // Prices that differ, or in two currencies: the person says which.
+ expect(pageOffer(['Small: $119.', 'Large: $139.'])).toEqual({ several: ['$119', '$139'] });
+ expect(pageOffer(['Regular: $119.95', 'Long: $129.95'])).toEqual({
+ several: ['$119.95', '$129.95'],
+ });
+ expect(pageOffer(['$129.', 'Order now: $129.00.'])).toEqual(ridge);
+ // An old price with no lower one beside it isn't dropped for an amount elsewhere.
+ expect(pageOffer(['Regular price $129.00', 'Refer a friend and get $20.'])).toEqual({
+ several: ['$129.00', '$20'],
+ });
+ // "From" and a range written loosely may be several prices.
+ expect(pageOffer(['From $119.'])).toEqual({ several: ['$119'] });
+ for (const from of [
+ 'From just $129.',
+ 'Starting at just $129.',
+ 'As low as $129.',
+ 'Pricing begins at $129.',
+ 'A partir de 129 €.',
+ 'A partire da 129 €.',
+ 'Vanaf € 129.',
+ 'Ab nur 129 €.',
+ "1.299 TL'den başlıyor.",
+ ]) {
+ expect(pageOffer([from])).toHaveProperty('several');
+ }
+ // A number written onto a price: the person says which.
+ expect(pageOffer(['PlayStation 5 549,99 €'])).toEqual({ several: ['5 549,99 €'] });
+ expect(pageOffer(['Ridge 28 € 129'])).toEqual({ several: ['28 €'] });
+ expect(pageOffer(['999 TL 3 taksit imkânı.'])).toEqual({ several: ['999 TL'] });
+ // Another product's range beside the price isn't its range.
+ expect(pageOffer(['Comparable headphones cost $200–$400, but the X1 is $299.'])).toHaveProperty(
+ 'several',
+ );
+ expect(pageOffer(['From $119 up to $139.'])).toEqual({ several: ['$119'] });
+ expect(pageOffer(['Preis: 119 – 139 €.'])).toEqual({ several: ['119 – 139 €'] });
+ expect(pageOffer(['Fiyat: 999 - 1.299 TL.'])).toEqual({ several: ['999 - 1.299 TL'] });
+ expect(pageOffer(['$129 in the US, €119 in Europe.'])).toEqual({ several: ['$129', '€119'] });
+ // A number after a price isn't its range's end.
+ expect(pageOffer(['Only $19 - 100% organic cotton.'])).toEqual({ price: 19, currency: 'USD' });
+ expect(pageOffer(['$25 - 30-day money-back guarantee.'])).toEqual({
+ price: 25,
+ currency: 'USD',
+ });
+ expect(pageOffer(['€20 – 30 Tage Rückgaberecht.'])).toEqual({ price: 20, currency: 'EUR' });
+ expect(pageOffer(['$5 a 12-pack.'])).toEqual({ price: 5, currency: 'USD' });
+ });
+});
+
+describe('openingParagraph', () => {
+ it('gives the first paragraph past a byline, cut at a word under 500 characters', () => {
+ expect(
+ openingParagraph('<h1>Ridge 28</h1><p>By Acme Team</p><p>A daypack for wet hikes.</p>'),
+ ).toBe('A daypack for wet hikes.');
+ const long = openingParagraph(`<p>${'word '.repeat(150)}end.</p>`)!;
+ expect(long.length).toBeLessThanOrEqual(501);
+ expect(long.endsWith('…')).toBe(true);
+ expect(openingParagraph('<h1>Ridge 28</h1>')).toBeNull();
+ });
+});
+
+describe('productJsonLd', () => {
+ it('gives a Product with its Offer, or an AggregateOffer for a range, and only what the page states', () => {
+ expect(
+ productJsonLd({
+ name: 'Ridge 28 daypack',
+ description: 'A daypack for wet hikes.',
+ images: ['https://cdn.acme.com/ridge.jpg'],
+ brandName: 'Acme',
+ offer: { price: 129, currency: 'USD' },
+ }),
+ ).toEqual({
+ '@context': 'https://schema.org',
+ '@type': 'Product',
+ name: 'Ridge 28 daypack',
+ description: 'A daypack for wet hikes.',
+ image: 'https://cdn.acme.com/ridge.jpg',
+ brand: { '@type': 'Brand', name: 'Acme' },
+ offers: { '@type': 'Offer', price: '129.00', priceCurrency: 'USD' },
+ });
+ const range = productJsonLd({
+ name: 'Ridge 28',
+ description: 'A daypack in [color] for wet hikes.',
+ images: [],
+ brandName: 'Acme',
+ offer: { low: 119, high: 139, currency: 'EUR' },
+ });
+ // A placeholder in the description leaves it out; no availability or reviews are made up.
+ expect(range).not.toHaveProperty('description');
+ expect(range).not.toHaveProperty('image');
+ expect(range.offers).toEqual({
+ '@type': 'AggregateOffer',
+ lowPrice: '119.00',
+ highPrice: '139.00',
+ priceCurrency: 'EUR',
+ });
+ });
+});
diff --git a/web/src/lib/content/page-schema.ts b/web/src/lib/content/page-schema.ts
index 7fe0f8b8b..a2bdfb8a7 100644
--- a/web/src/lib/content/page-schema.ts
+++ b/web/src/lib/content/page-schema.ts
@@ -1,11 +1,14 @@
import type { ContentAuthor } from './authors';
+import { factValueText, pagePrices, priceRanges } from './brand-facts';
import type { PageType } from './drafts';
import { PLACEHOLDER } from './placeholders';
+import { isByline, sentencesOf } from './readability';
/**
* The page's JSON-LD for Before you publish (Content Editor Quality spec §7,
* CE-041): an Article from its title, author, brand and dates, for the
- * person to paste when their CMS doesn't write one. A person byline brings
+ * person to paste when their CMS doesn't write one; a product page's
+ * Product with its Offer in Phase 3 (CE-053). A person byline brings
* their Person schema (sameAs from their profile links); the organization
* byline, the brand's Organization. Nothing in it is made up: what isn't
* known is left out.
@@ -36,6 +39,10 @@ const ONE_PLACEHOLDER = new RegExp(PLACEHOLDER.source, 'iu');
/** Whether the title still has a placeholder: it would ship as the headline. */
export const titleHasPlaceholder = (title: string) => ONE_PLACEHOLDER.test(title);
+/** The title as the page's name (Article's headline, Product's name); none while empty or a placeholder's in it. */
+export const pageName = (title: string) =>
+ title.trim() && !titleHasPlaceholder(title) ? title.trim() : null;
+
/** The page's first image with a full https address, as JSON-LD's image. */
export function firstImage(html: string): string | null {
const doc = new DOMParser().parseFromString(html, 'text/html');
@@ -99,7 +106,7 @@ export function articleJsonLd({
'@context': 'https://schema.org',
'@type': 'Article',
// A placeholder isn't the page's headline: it is left out until filled.
- ...(title.trim() && !titleHasPlaceholder(title) ? { headline: title.trim() } : {}),
+ ...(pageName(title) ? { headline: pageName(title) } : {}),
...(image ? { image } : {}),
...(author !== undefined ? { author: person ?? organization } : {}),
publisher: organization,
@@ -108,6 +115,254 @@ export function articleJsonLd({
};
}
+/**
+ * A product page's price as the page states it (Quality spec §11, CE-053):
+ * one price is the product's Offer, a range it writes ("$119–$139") its low
+ * and high. Each amount is read by the words around it, so a sentence keeps
+ * its price beside one that isn't: "$129 (was $159)", "$129, or 4 payments
+ * of $32.25", "€129 inkl. MwSt.". A price on the page is pasted as the
+ * product's, so nothing is guessed: prices that differ ("Small: $119",
+ * "Large: $139", or one this reading missed) are `several`, for the person
+ * to write once or as a range.
+ */
+export type PageOffer =
+ | { price: number; currency: string }
+ | { low: number; high: number; currency: string };
+export type PageOfferRead = PageOffer | { several: string[] } | null;
+
+/**
+ * Just before an amount, words that make it no price of the product's: a
+ * threshold ("orders over $50", "spend $75", "minimum $20", "Versand ab
+ * 50 €"), an old price ("was", "instead of", "statt", "au lieu de"), a
+ * saving, a worth or an extra ("save up to $30", "worth $30", "an extra
+ * $10", "earn $13"), an installment ("4 payments of $32"), a bundle ("2 for
+ * $30"), or a bill added to it ("+ $9.95", "zzgl. 4,95 €").
+ */
+const NOT_THE_PRICE_BEFORE =
+ /(?:(?<![\p{L}-])(?:over|above|more\s+than|at\s+least|minimum(?:\s+(?:order|spend|purchase))?(?:\s+of)?|(?<!\d\s?)min\.?(?:\s+(?:order|spend|purchase))?(?:\s+of)?|spend(?:ing)?|orders?(?:\s+(?:over|above|of|from|between))?|bestellungen|commandes?|pedidos?|siparişler(?:de)?|über|superior(?:es)?\s+a|supérieure?s?\s+à|plus\s+de|más\s+de|was|were|instead\s+of|statt|bisher|vorher|au\s+lieu\s+de|prix\s+barré|antes|precio\s+anterior|save(?:\s+up\s+to)?|saving|savings(?:\s+of)?|you\s+save\b[^.;$€£¥₹₺]{0,12}|sie\s+sparen|sparen\s+sie|économisez|ahorr(?:a|as|o\s+de)|tasarruf(?:unuz)?|up\s+to|worth|valued\s+at|value\s+of|earn|(?:an?\s+)?extra|additional|discount\s+of|gift\s+cards?|(?:store\s+)?credit|(?:payments?|installments?|instalments?|deposit)\s+of|raten?\s+(?:à|von|zu|a)|plazos\s+de|mensualités\s+de|zzgl|zuzüglich|unit\s+price|grundpreis|prix\s+unitaire|precio\s+unitario|birim\s+fiyat(?:ı)?|members?(?:hip)?(?:\s+price)?|subscri\p{L}*(?:\s+(?:&|and)\s+save)?(?:\s+price)?|with\s+code\b[^.;$€£¥₹₺]{0,15}|balance(?:\s+of)?|remaining|restbetrag)|(?:shipping|delivery|versand|livraison|env[ií]o|kargo)\p{L}*(?:\s+\p{L}+){0,2}\s+(?:ab|dès|desde|from)|(?:^|\(|\bbuy\s+|\bany\s+|\bor\s+)\s*(?:\d{1,2}|two|three|four|five)\s+for|\d\s*[x×]|(?<![\p{L}\p{N}])\+)\s*:?\s*\(?\s*$/iu;
+/** "plus $9.95 shipping": lower case, so a model named Plus keeps its price. */
+const PLUS_BEFORE = /(?<!\p{L})plus\s*$/u;
+/**
+ * An old price ("Regular price", "Original price", "reg.", "compare at"):
+ * dropped beside a lower price in its block ("Regular price $159 Sale price
+ * $129"), else a price like the others. "Regular" alone is a size.
+ */
+const OLD_PRICE_BEFORE =
+ /(?<!\p{L})(?:(?:regular|original|retail|list|eski|liste|piyasa)\s+(?:price|fiyat(?:ı)?)|regularly|originally|reg\.|compare\s+at|msrp|rrp|uvp)\s*:?\s*$/iu;
+/**
+ * Within its last words before an amount, another bill: shipping (free
+ * shipping is no bill: "with free shipping costs $129") or financing
+ * ("Shipping: $5.99", "Pay in 4 with Klarna: $32.25").
+ */
+const ANOTHER_BILL =
+ /(?<!(?:free|kostenlose[rn]?|gratuite?|gratis|ücretsiz|bedava)\s+)(?<!\p{L})(?:shipping|delivery|postage|versand(?!kostenfrei)\p{L}*|livraison|env[ií]os?|kargo\p{L}*|frete|affirm|klarna|afterpay|sezzle|paypal|shop\s?pay|taksit\p{L}*|financing|finanzierung|interest[\s-]free)(?!\p{L})/iu;
+/**
+ * In its clause before an amount, an add-on, an accessory by its label or
+ * setup ("Add a waterproof rain cover for $19", "Accessories: rain cover
+ * $19", "Installation: $99").
+ */
+const ADD_ON =
+ /(?<!\p{L})(?:sold\s+separately|ayrı\s+satılır|add(?:-ons?|\s+an?|\s+on)|(?:accessor(?:y|ies)|aksesuar\p{L}*|zubehör)\s*:|installation|setup|set-up|kurulum)(?!\p{L})/iu;
+/**
+ * Right after an amount: a unit price ("$0.50/oz", "€12 pro kg"), a
+ * range's period ("$10–$20 a month"), what it isn't ("$30 off", "a $10
+ * coupon", "$5 fee", "$30 less", "($30 value)", "$13 in rewards", "500 TL
+ * üzeri", "₺1.299 yerine"), another bill ("$9.95 shipping", "$4 tax"), a
+ * bundle ("$25 for 2"), financing ("$32.25 with Klarna") or an add-on
+ * ("$19, sold separately").
+ */
+const NOT_THE_PRICE_AFTER =
+ /^(?:\+(?!\s?\d)|\s*(?:\/\s*|per\s+|pro\s+|a\s+)(?:per\s+)?(?:\d+\s?)?(?:kg|g|lb|lbs|oz|fl\.?\s?oz|ml|cl|l|unit|ct|count|piece|pc|item|sq\.?\s?ft|m²|m2|stück|adet|serving)(?!\p{L})|\s*(?:\/\s*|per\s+|pro\s+|im\s+|a\s+|an\s+|each\s+|par\s+|al\s+|por\s+)(?:mo|mth|month|monat|mois|mes|yr|year|jahr|an|año|wk|week|day)(?!\p{L})|\s*(?:monthly|yearly|annually|monatlich|jährlich|aylık|yıllık)(?!\p{L})|\s*(?:off|discount|coupon|(?:store\s+)?credits?|gift\s+cards?|fee|surcharge|deposit|rebate|cash\s?back|savings?|less|more|cheaper|value|in\s+(?:rewards?|credit|points)|trade-in|yerine|(?:ve\s+)?üzeri|minimum|or\s+(?:more|above|over))(?!\p{L})|\s*(?:shipping|delivery|postage|s&h|handling|versand\p{L}*|porto|livraison|env[ií]os?|kargo\p{L}*|frete|(?:sales\s+)?tax(?:es)?|vat|mwst|kdv)(?!\p{L})(?!\s+(?:incl\p{L}*|inkl\p{L}*|dahil|included))|\s*(?:with|for)\s+(?:subscri\p{L}*|members?|prime|(?:promo\s+)?code)(?!\p{L})|\s*(?:indirim|kupon)\p{L}*|\s*for\s+(?:\d|two|three|four|five)(?![\p{N}\p{L}])|\s*(?:today\s+)?(?:with|via|through)\s+(?:affirm|klarna|afterpay|sezzle)(?!\p{L})|[^.;]{0,6}(?:sold\s+)?separately|[^.;]{0,6}ayrı\s+satılır)/iu;
+
+/** How an amount reads by the words around it: another bill, an old price, or the product's. */
+function amountKind(
+ sentence: string,
+ at: number,
+ end: number,
+ since: number,
+): 'not' | 'old' | 'price' {
+ // Its clause, since the amount before it: "Free shipping, and it's $129" keeps $129.
+ const lead = sentence
+ .slice(Math.max(since, at - 48), at)
+ .split(/,\s|;|\s[–—]\s/u)
+ .pop()!;
+ const lastWords = lead.trim().split(/\s+/).slice(-5).join(' ');
+ if (
+ NOT_THE_PRICE_BEFORE.test(lead) ||
+ PLUS_BEFORE.test(lead) ||
+ ANOTHER_BILL.test(lastWords) ||
+ ADD_ON.test(lead) ||
+ NOT_THE_PRICE_AFTER.test(sentence.slice(end, end + 40))
+ ) {
+ return 'not';
+ }
+ return OLD_PRICE_BEFORE.test(lead) ? 'old' : 'price';
+}
+
+/** The prices that differ Before you publish lists, at most. */
+const SEVERAL_SHOWN = 5;
+/** A sale price this share of the old one or more reads as its sale (60% off at most). */
+const SALE_FLOOR = 0.4;
+
+/** Abbreviations a price sits beside, kept from ending a sentence ("$99 (reg. $129)", "zzgl. 4,95 €"). */
+const ABBREVIATION =
+ /(?<!\p{L})(?<!\d\s?)(reg|min|zzgl|inkl|exkl|incl|excl|approx|ca|uvp|vs)\.(?=\s)/giu;
+
+export function pageOffer(texts: readonly string[]): PageOfferRead {
+ const found: {
+ low: number;
+ high: number;
+ currency: string;
+ text: string;
+ block: number;
+ range: boolean;
+ old: boolean;
+ /** Not sure to be one price: "From $119", "$119 – 139", "119 bis 139 €". */
+ unsure: boolean;
+ }[] = [];
+ texts.forEach((text, block) => {
+ // A one-dot leader keeps the abbreviation's dot: sentencesOf puts it back.
+ for (const sentence of sentencesOf(text.replace(ABBREVIATION, '$1\u2024'))) {
+ const ranges = priceRanges(sentence);
+ const inRange = (at: number) => ranges.some((r) => at >= r.at && at < r.at + r.text.length);
+ const amounts = [
+ // A price by the month or the year is a plan's or a payment's, not a product's.
+ ...pagePrices(sentence, { rangeEnds: true })
+ .filter((price) => price.period === null && !inRange(price.at))
+ .map((price) => ({
+ ...price,
+ low: price.amount,
+ high: price.amount,
+ range: false,
+ unsure: price.from || price.loose,
+ })),
+ ...ranges.map((range) => ({ ...range, range: true, unsure: !range.sure })),
+ ].sort((a, b) => a.at - b.at);
+ let since = 0;
+ for (const amount of amounts) {
+ const end = amount.at + amount.text.length;
+ const kind = amountKind(sentence, amount.at, end, since);
+ if (amount.low > 0 && kind !== 'not') {
+ found.push({
+ low: amount.low,
+ high: amount.high,
+ currency: amount.currency,
+ text: amount.text.trim(),
+ block,
+ range: amount.range,
+ old: kind === 'old',
+ unsure: amount.unsure,
+ });
+ }
+ since = end;
+ }
+ }
+ });
+ // An old price is dropped beside the one lower price in its block that
+ // reads as its sale ("Regular price $159 Sale price $129"), not beside
+ // another amount ("Regular price $129 Unit price $12.90").
+ const prices = found.filter((p) => {
+ if (!p.old) return true;
+ const lower = found.filter((q) => !q.old && q.block === p.block && q.high < p.low);
+ return !(lower.length === 1 && lower[0].low >= p.low * SALE_FLOOR);
+ });
+ if (!prices.length) return null;
+ const [first] = prices;
+ const same = (p: (typeof prices)[number]) => p.currency === first.currency;
+ const ranges = prices.filter((p) => p.range);
+ const range = ranges[0];
+ if (
+ !prices.some((p) => p.unsure) &&
+ prices.every(same) &&
+ (range
+ ? ranges.every((r) => r.low === range.low && r.high === range.high) &&
+ // A price beside the range is one of its ends: "$200–$400, but the X1 is $299" isn't one.
+ prices.every((p) => p.range || p.low === range.low || p.low === range.high)
+ : prices.every((p) => p.low === first.low))
+ ) {
+ return range
+ ? { low: range.low, high: range.high, currency: range.currency }
+ : { price: first.low, currency: first.currency };
+ }
+ // Each price once, as the page first writes it ("$129" and "$129.00" are one).
+ const seen = new Map<string, string>();
+ for (const p of prices) {
+ const key = `${p.currency} ${p.low} ${p.high} ${p.unsure}`;
+ if (!seen.has(key)) seen.set(key, p.text);
+ }
+ return { several: [...seen.values()].slice(0, SEVERAL_SHOWN) };
+}
+
+/** The Offer's price as a person reads it ("$129", "€119–€139"), for them to check. */
+export function offerText(offer: PageOffer): string {
+ const money = (amount: number) =>
+ factValueText('price', {
+ plan: '',
+ amount,
+ currency: offer.currency,
+ period: null,
+ billed: null,
+ per: null,
+ });
+ return 'price' in offer ? money(offer.price) : `${money(offer.low)}–${money(offer.high)}`;
+}
+
+/** The page's opening paragraph (past a byline or a date), as Product's description: up to 500 characters. */
+export function openingParagraph(html: string): string | null {
+ const doc = new DOMParser().parseFromString(html, 'text/html');
+ for (const p of doc.body.querySelectorAll('p')) {
+ const text = (p.textContent ?? '').replace(/\s+/g, ' ').trim();
+ if (!text || isByline(text)) continue;
+ if (text.length <= 500) return text;
+ const cut = text.slice(0, 500);
+ return `${cut.slice(0, cut.lastIndexOf(' ') > 0 ? cut.lastIndexOf(' ') : 500)}…`;
+ }
+ return null;
+}
+
+export interface ProductSchemaInput {
+ /** The page's title (its H1) as pageName gives it: Product needs a name, so there is none without it. */
+ name: string;
+ /** The opening paragraph: left out while a placeholder's in it. */
+ description: string | null;
+ /** The person's own images on the page first (CE-049), else its first image. */
+ images: string[];
+ brandName: string;
+ offer: PageOffer;
+}
+
+/**
+ * A product page's JSON-LD (CE-053): Product with its Offer, from the page
+ * as written. Only what the page states: availability, SKU, GTIN and
+ * reviews come from a product feed and connected reviews, which aren't
+ * connected yet; a price the page doesn't state gives none (pageOffer).
+ */
+export function productJsonLd({
+ name,
+ description,
+ images,
+ brandName,
+ offer,
+}: ProductSchemaInput): Record<string, unknown> {
+ return {
+ '@context': 'https://schema.org',
+ '@type': 'Product',
+ name,
+ ...(description && !ONE_PLACEHOLDER.test(description) ? { description } : {}),
+ ...(images.length ? { image: images.length === 1 ? images[0] : images } : {}),
+ ...(brandName.trim() ? { brand: { '@type': 'Brand', name: brandName.trim() } } : {}),
+ offers:
+ 'price' in offer
+ ? { '@type': 'Offer', price: offer.price.toFixed(2), priceCurrency: offer.currency }
+ : {
+ '@type': 'AggregateOffer',
+ lowPrice: offer.low.toFixed(2),
+ highPrice: offer.high.toFixed(2),
+ priceCurrency: offer.currency,
+ },
+ };
+}
+
/**
* The JSON-LD as a script tag to paste into the page. A "<" in it is
* escaped, so a title can't close the tag.