@sonordev/contracts 2.1.1 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,26 @@
1
1
  # Changelog
2
2
 
3
+ ## 2.3.0
4
+
5
+ A minor release: one new entry, `@sonordev/contracts/error-page`. Every other entry is unchanged.
6
+
7
+ - **New entry `@sonordev/contracts/error-page`**: telling a rendered error page from a page's content, and copy that calls a working page missing from copy that describes it, so every writer and reader of page text answers the same way.
8
+ - `looksLikeErrorPage(text, wordCount?)` is true when stored text is an error page: a 404 in its common wordings, a framework or server error, a status page, a "something went wrong" or "temporarily unavailable" screen or a maintenance notice. It reads how the text opens, decodes entities, doesn't depend on word boundaries (scraped text runs elements together, as in "404This page could not be found."), and leaves phone numbers, addresses and pages that only describe an error alone.
9
+ - `usablePageText(text, wordCount?)` returns the text, or `null` for an error page.
10
+ - `findErrorPageClaim(copy, pageText?)` and `describesErrorPage(copy, pageText?)` find generated copy that says the page itself is missing or unavailable ("Page Not Found", "this page is currently unavailable", "returns a 404", "a page-not-found message"). A phrase the page's own text really says is allowed, and copy about something unavailable on a working page isn't a claim about the page.
11
+ - `findPageCopyClaim(fields, pageText?)` runs that check over every string in a set of fields, JSON-LD included, and returns the first `{ field, phrase }`.
12
+ - `ERROR_PAGE_MAX_WORDS` and `ERROR_PAGE_CONTRACT_VERSION` are exported alongside.
13
+
14
+ ## 2.2.0
15
+
16
+ A minor release: one new entry, `@sonordev/contracts/proposal-sitemap`. Every other entry is unchanged.
17
+
18
+ - **New entry `@sonordev/contracts/proposal-sitemap`**: how a website proposal's site plan counts its pages, so the app that writes a proposal, the API that checks it and the page that shows it state the same numbers.
19
+ - `countSitemapPlan(plan)` counts core pages, the plan's own pages, re-published articles, pages carried over and pages added, each address once.
20
+ - `sitemapTransformation(plan)` decides whether the plan's before-and-after comparison shows. It shows only when the build is bigger than today; a rebuild that keeps the same pages never reads as "N → 0".
21
+ - `normalizeSitemapLabels(labels)` keeps the words a plan may use for pages that aren't services sold to industries.
22
+ - `proposalPageCountIssues(sections)` and `describePageCountIssue(issue)` find page and URL counts stated in a proposal's headline, summary, pricing and plan copy that don't match the plan.
23
+
3
24
  ## 2.1.1
4
25
 
5
26
  - Docs only: the README names the Sonor API and the dashboard where it named internal repositories. No code changes.
package/README.md CHANGED
@@ -22,7 +22,9 @@ pnpm add @sonordev/contracts
22
22
  | `@sonordev/contracts/site-cache` | The cache tags a site's Sonor fetches carry and the revalidation route's path, payload and ping | site-kit tags and serves the route, the Sonor API calls it |
23
23
  | `@sonordev/contracts/site-edit` | Edit on page: the messages between a framed site and the dashboard, trusted origins, how a slot is found on the page | site-kit's overlay, the dashboard's visual editor |
24
24
  | `@sonordev/contracts/portfolio` | Where a portfolio number comes from and which may headline; the live-frame message | the Sonor API, Signal, agency-site-kit |
25
+ | `@sonordev/contracts/proposal-sitemap` | How a proposal's site plan counts its pages, when its before-and-after comparison may show, and whether the page counts stated elsewhere in the proposal add up | the Sonor API, Signal's proposal writer, the dashboard's proposal pages |
25
26
  | `@sonordev/contracts/voice` | The house voice's contraction rule. `findUncontracted` and `contractText` find and apply full forms that should be contractions; `findContracted` finds the contractions a question-form heading should expand | anything that checks or rewrites copy: Sonor's APIs and dashboard |
27
+ | `@sonordev/contracts/error-page` | Whether stored text is a rendered error page rather than a page's content, and whether generated copy says the page itself is missing | anything that stores page text or writes page copy from it: the Sonor API's page-view and sitemap writers, Signal's metadata generators |
26
28
 
27
29
  ## The voice contract
28
30
 
@@ -135,6 +137,121 @@ pinned by a test, so changing it later is a visible change.
135
137
  "*", "_" and masked text before it are skipped, so "Now: let us look at it."
136
138
  contracts.
137
139
 
140
+ ## The proposal-sitemap contract
141
+
142
+ A website proposal's site plan lists the pages a build delivers: core pages
143
+ (Home, About, Contact), top-level pages, and the pages under them. Its numbers
144
+ are what a buyer reads first, so they're computed here and never taken from
145
+ prose.
146
+
147
+ - **`countSitemapPlan(plan)`** returns `core`, `architecture` (the plan's own
148
+ pages), `articles` (existing posts re-published with the build), `full`
149
+ (all three), `existing` (pages carried over) and `added` (new pages). Each
150
+ address counts once: core pages first, then each top-level page and the
151
+ pages under it. A row without a slug always counts.
152
+ - **`sitemapTransformation(plan)`** returns the before-and-after comparison
153
+ to show (`{ before, after }`), or `null`. It shows only when the plan states
154
+ today's count and the build is bigger. A rebuild that keeps the same pages
155
+ isn't a before and after, so an after-count of zero, or one no bigger than
156
+ today's, hides it.
157
+ - **`normalizeSitemapLabels(labels)`** keeps the words a plan may use for its
158
+ own pages when they aren't services sold to industries: `architecture` (the
159
+ heading), `pillar` (a top-level page) and `child` (a page under one). Each is
160
+ one line of 1 to 40 characters.
161
+ - **`proposalPageCountIssues(sections)`** lists the page and URL counts a
162
+ proposal states in its headline, summary, pricing and plan copy that match
163
+ nothing the plan or its measured evidence adds up to.
164
+ `describePageCountIssue(issue)` puts one in a sentence for the person
165
+ reviewing it.
166
+
167
+ ```ts
168
+ import { countSitemapPlan, proposalPageCountIssues } from '@sonordev/contracts/proposal-sitemap'
169
+
170
+ countSitemapPlan({
171
+ corePages: [{ slug: '/' }, { slug: '/about/' }, { slug: '/locations/' }],
172
+ pages: [{ slug: '/locations/', children: [{ name: 'Springfield' }, { name: 'Shelbyville' }] }],
173
+ }).full
174
+ // 5: the top-level /locations/ page is the core one, counted once
175
+
176
+ proposalPageCountIssues([
177
+ { type: 'GlassHero', props: { stats: [{ value: '12', label: 'pages rebuilt' }] } },
178
+ { type: 'SitemapPlan', props: { pages: [{ slug: '/' }, { slug: '/about/' }] } },
179
+ ])
180
+ // [{ section: 'GlassHero', field: 'stats[0]', claimed: 12, unit: 'page', text: '12 pages', planned: 2 }]
181
+ ```
182
+
183
+ It reads "N pages", "an N-page site", "N existing URLs" and up to three words
184
+ between the number and "page" or "URL". A count written in words ("five
185
+ pages") isn't read, and a count of pages that aren't in the plan (say, a
186
+ competitor's) is reported like any other; the issues are for a person to look
187
+ at, not edits to apply.
188
+
189
+ ## The error-page contract
190
+
191
+ A page's stored text is whatever a browser or crawler rendered. Visit a URL
192
+ while it's briefly broken and the error page's words become that page's
193
+ content, and any title, description or summary written from that content
194
+ describes a working page as broken. `error-page` is how writers refuse both
195
+ halves of that.
196
+
197
+ - **`looksLikeErrorPage(text, wordCount?)`** is true when the text is a
198
+ rendered error page: a 404 in its common wordings ("404", "This page could
199
+ not be found", "Page not found"), a framework error ("a client-side
200
+ exception has occurred", "This page couldn't load", "Internal Server
201
+ Error", "Cannot GET /x"), a status page ("502 Bad Gateway", "HTTP Status
202
+ 404"), a "something went wrong" or "temporarily unavailable" screen, or a
203
+ maintenance notice. It reads how the text *opens* (the first 300
204
+ characters), decodes HTML entities, and doesn't depend on word boundaries,
205
+ because scraped text runs elements together ("404This page could not be
206
+ found."). Text up to 120 words is checked for every wording, though the
207
+ loosest ones (a 404 glued to its copy, "refresh the page and try again", a
208
+ route's "<name> not found") only read the first 60 or 40 words; up to 400
209
+ words only the check for how it opens after a meta description and skip link
210
+ applies; beyond that it's prose. A phone number, room number or street
211
+ address that starts with 404 isn't an error page, and neither is a help page
212
+ that only *describes* an error ("how to fix a 404 error", "if something went
213
+ wrong"). Pass the word count of the text itself, never a whole page's count
214
+ (a browser's includes the navigation and footer and can hide a short error
215
+ state): a supplied count can only raise the count the check uses.
216
+ - **`usablePageText(text, wordCount?)`** returns the text, or `null` when it's
217
+ an error page. It's what a model should be shown as "the page's content".
218
+ - **`findErrorPageClaim(copy, pageText?)`** returns the phrase in generated
219
+ copy that says the page itself is missing or unavailable ("Page Not Found",
220
+ "the pricing page is currently unavailable", "returns a 404", "a
221
+ page-not-found message"), or `null`. Copy about something unavailable on a
222
+ working page ("the patio is closed on Mondays", "tours are paused for
223
+ winter") isn't a claim about the page. Pass the page's own text and a phrase
224
+ the page really says is allowed, so a guide to fixing "page not found"
225
+ errors can be titled that way; the text of an error page is never accepted
226
+ as that exemption. `describesErrorPage` is the boolean form.
227
+ - **`findPageCopyClaim(fields, pageText?)`** runs `findErrorPageClaim` over
228
+ every string in a set of fields (a title, a description, keywords, a
229
+ JSON-LD object; any object or class instance) and returns the first
230
+ `{ field, phrase }`, or `null`. It's how a writer or a reader judges a whole
231
+ draft in one call. It reads at most 2,000 strings, 8 levels deep and 100,000
232
+ characters in all, and each string's first 5,000 characters, so a hostile
233
+ record can't make it slow.
234
+
235
+ ```ts
236
+ import { describesErrorPage, findPageCopyClaim, usablePageText } from '@sonordev/contracts/error-page'
237
+
238
+ usablePageText('404This page could not be found.') // null: don't generate from it
239
+ usablePageText('Weekly mowing for property managers.') // the text
240
+
241
+ describesErrorPage('Commercial Mowing Page Not Found | Example Co') // true: don't ship it
242
+ describesErrorPage('Seasonal tours are paused until April.') // false
243
+
244
+ findPageCopyClaim({
245
+ title: 'Commercial Mowing | Example Co',
246
+ schema: { '@type': 'WebPage', description: 'This URL currently returns a 404.' },
247
+ })
248
+ // { field: 'schema', phrase: 'returns a 404' }
249
+ ```
250
+
251
+ A text-only check can't prove a page failed. Where you observed the HTTP
252
+ status, require 400 or more as well and use this as the fallback; the header of
253
+ `src/contracts/error-page.ts` lists the other known limits.
254
+
138
255
  ## Why it exists
139
256
 
140
257
  A rule that lives in several copies is how two of them end up disagreeing.
@@ -0,0 +1,183 @@
1
+ /**
2
+ * @sonordev/contracts/error-page: telling an error page from a page (v1)
3
+ *
4
+ * Three questions every writer of page content and page copy has to answer the
5
+ * same way, so they live here once:
6
+ *
7
+ * 1. Is this stored text a rendered ERROR PAGE rather than the page's own
8
+ * content? (`looksLikeErrorPage`, `usablePageText`)
9
+ * 2. Does this generated COPY say the page itself is missing or unavailable?
10
+ * (`findErrorPageClaim`, `describesErrorPage`)
11
+ * 3. Does any string in a page's managed copy (a title, a keyword list, a
12
+ * JSON-LD block) say so? (`findPageCopyClaim`: question 2 over every
13
+ * string of a record, naming the field that holds it)
14
+ *
15
+ * Why it exists: a page's stored text is the text a visitor's browser (or a
16
+ * build's crawler) rendered. Visit a URL while it's briefly broken and the
17
+ * error page's words are stored as that page's content. Everything generated
18
+ * from that content then describes a working page as broken, and the result
19
+ * is published as the page's title, meta description, social tags, JSON-LD
20
+ * and AI-crawler notes. The outage ends in a deploy; the poisoned copy
21
+ * doesn't, because nothing downstream asks whether its input was ever the
22
+ * page.
23
+ *
24
+ * How the text check works
25
+ *
26
+ * - It reads the START of the text (300 characters), lowercased, with
27
+ * whitespace collapsed, HTML entities decoded and curly quotes made
28
+ * straight. Error copy leads; articles bury it.
29
+ * - The signatures are generic on purpose. Every site words its 404
30
+ * differently and frameworks ship their own ("This page could not be
31
+ * found.", "Application error: a client-side exception has occurred"), so
32
+ * the shape is what's matched, not any one site's copy. They're grouped by
33
+ * family below: not found, server and client errors, unavailable and
34
+ * maintenance, walls.
35
+ * - Stored text is often the page's elements run together with no separator
36
+ * ("404This page could not be found."), so no signature depends on a word
37
+ * boundary around the text it's matching.
38
+ * - A signature that follows a word that DESCRIBES errors rather than
39
+ * showing one ("if something went wrong", "how to fix a 404 error", a
40
+ * quote mark) doesn't count: a help page that names an error isn't one.
41
+ * Navigation is Title Case and prose isn't, so a describing word counts
42
+ * only as it's written in lower case, with only its first letter
43
+ * capitalized ("How to fix..."), or opening the text or a sentence;
44
+ * "Home Blog How To Something went wrong" is still an error page, and the
45
+ * words a menu uses too ("About", "Monitoring", "Alerts for") describe an
46
+ * error only in lower case. A describing word inside the matched phrase
47
+ * counts too, and after a described match the search resumes one character
48
+ * on, so a real error later in the same stretch is still found.
49
+ * - A not-found phrase that sits in a question ("Page not found? Use our site
50
+ * map", "Product not found in your size? Ask us") is a help line, and so is
51
+ * most copy that merely uses the words ("Rare parts can't be found
52
+ * elsewhere": the subject has to be a page, URL, link, file, site or app).
53
+ * - Up to ERROR_PAGE_MAX_WORDS words every check applies, but the loosest
54
+ * ones apply only to the shortest texts: a 404 glued to its copy and
55
+ * "refresh the page and try again" up to 60 words, a dynamic route's own
56
+ * "<name> not found" state up to 40. From there up to 400 words only the
57
+ * prefix-aware check applies: a text written as "<meta description> —
58
+ * <every string on the page>" carries its header and footer strings along,
59
+ * which pushes a rendered error state past the short-text ceiling, so the
60
+ * check looks at how the text opens after the description and a skip link,
61
+ * and an opener followed by "explained" or "how to" is an article. Beyond
62
+ * 400 words the text is prose.
63
+ *
64
+ * `wordCount` semantics: pass the word count of `text` itself. Pass the whole
65
+ * document's count only when `text` is a truncated excerpt of it. A supplied
66
+ * count can only RAISE the count the check uses (an excerpt of a long document
67
+ * is prose however short it is), never lower it: text that is longer than the
68
+ * count you gave is counted by what it holds. A browser's page-wide count
69
+ * (navigation and footer included) is therefore the wrong number for a short
70
+ * error state it scraped: pass the count of the text you pass.
71
+ *
72
+ * How the copy check works
73
+ *
74
+ * `findErrorPageClaim` looks for the copy saying THE PAGE is missing or
75
+ * unavailable ("Page Not Found | Acme", "This URL currently returns a 404",
76
+ * "This listing has been removed", "The page failed to load"). The subject
77
+ * has to be the page, its URL, link or listing, so copy about something
78
+ * unavailable on a working page ("sunset cruises are not available at this
79
+ * time") is left alone, and so is a qualified one ("unavailable in winter").
80
+ * Whitespace is collapsed and each string is read up to 5,000 characters, so
81
+ * hostile whitespace costs nothing. The page's own text exempts a phrase the
82
+ * page really says; a page that is itself an error page exempts nothing.
83
+ *
84
+ * `findPageCopyClaim(fields, pageText?)` is the same check over a record:
85
+ * each value of `fields` is checked if it's a string, and walked for every
86
+ * string it holds if it's an array or an object (8 levels deep, 2,000
87
+ * leaves at most; numbers, booleans and null are ignored). It returns the
88
+ * first claim as `{ field, phrase }`, `field` being the TOP-LEVEL key (the
89
+ * column or property that holds the copy), or null. Use it for a page row's
90
+ * managed title, description, keywords and JSON-LD in one call.
91
+ *
92
+ * Known limits (a text-only check can't close these)
93
+ *
94
+ * - Any error phrase in the first 300 characters of a text of 120 words or
95
+ * fewer reads as an error page, wherever in those 300 characters it sits, unless
96
+ * something describes it (see above): a short page ABOUT errors ("Page not
97
+ * found: how to design a helpful 404 page. Five examples.", or a web
98
+ * agency's line about its "custom 404 error page") can be misread. Texts of
99
+ * 121 to 400 words are only read for how they open, and longer ones are
100
+ * prose. The loosest wordings (a 404 glued to its copy, "refresh the page
101
+ * and try again", "<name> not found") apply only up to 60, 60 and 40 words.
102
+ * - A business whose name is an error phrase ("404 Brewing Co.", "Page Not
103
+ * Found Records") reads as an error page when its name leads the text.
104
+ * - Chrome in front hides the error state from the wordings anchored to the
105
+ * start of the text ("Not Found", "Be right back.", "Error: ...", "Oops!
106
+ * This page...", a permission wall, a dynamic route's "<name> not found").
107
+ * The wordings that hold anywhere in the first 300 characters (a status with
108
+ * its reason, "could not be found", "something went wrong") are what's
109
+ * left. Prefer the page's main content.
110
+ * - The text alone never proves a page failed. Where a call site observed
111
+ * the HTTP status (a browser reports it), require a status of 400 or more
112
+ * as well, and treat this check as the fallback for the sites and the
113
+ * paths that report none.
114
+ * - It reads English. A 404 in another language isn't matched.
115
+ * - Walls and pages served by the host in front of the site (a CDN block, a
116
+ * bot challenge, a parked domain, a default server page, a browser
117
+ * interstitial) never reach the writers this guards, so they aren't
118
+ * matched here.
119
+ * - Copy that claims the page is missing in words outside the closed list
120
+ * below isn't caught; the real protection is that error text never reaches
121
+ * the generator (`usablePageText`).
122
+ *
123
+ * Pure string logic: no I/O, no Node built-ins, no lookbehind (this ships to
124
+ * browsers, and Safari before 16.4 throws on a lookbehind at parse time).
125
+ */
126
+ /** Bump on breaking changes to what counts as an error page or an error claim. */
127
+ export declare const ERROR_PAGE_CONTRACT_VERSION: 1;
128
+ /**
129
+ * Above this many words, text is real prose even if it mentions a 404: only
130
+ * the prefix-aware check (up to 400 words, see the header) still looks at it.
131
+ *
132
+ * The gate is what makes the check safe: an article about broken links ("How
133
+ * to fix 404 errors on your site") legitimately holds every phrase below and
134
+ * must not be discarded. A rendered error page is short, and this ceiling
135
+ * still separates the two by an order of magnitude.
136
+ */
137
+ export declare const ERROR_PAGE_MAX_WORDS = 120;
138
+ /**
139
+ * True when this text is a rendered error page rather than page content.
140
+ *
141
+ * @param text the scraped or extracted body text
142
+ * @param wordCount the word count of `text` itself, or the whole document's
143
+ * when `text` is a truncated excerpt of it. It can only raise
144
+ * the count the check uses, never lower it; derived from
145
+ * `text` when absent.
146
+ */
147
+ export declare function looksLikeErrorPage(text?: string | null, wordCount?: number | null): boolean;
148
+ /**
149
+ * The page's stored text, or null when it's an error page. What a model may
150
+ * be shown as "the page's content".
151
+ */
152
+ export declare function usablePageText(text?: string | null, wordCount?: number | null): string | null;
153
+ /**
154
+ * The phrase in `copy` that says the page itself is missing or unavailable, or
155
+ * null when the copy is fine.
156
+ *
157
+ * @param pageText the page's own text. A phrase the page really says (an
158
+ * article about fixing "page not found" errors) is allowed;
159
+ * only copy that claims what the page doesn't is a finding.
160
+ * A pageText that is itself an error page allows nothing: its
161
+ * words are the poison, so the copy that repeats them would
162
+ * otherwise vouch for itself.
163
+ */
164
+ export declare function findErrorPageClaim(copy?: string | null, pageText?: string | null): string | null;
165
+ /** True when `copy` says the page itself is missing or unavailable (see `findErrorPageClaim`). */
166
+ export declare function describesErrorPage(copy?: string | null, pageText?: string | null): boolean;
167
+ /**
168
+ * The first string anywhere in a record of managed copy that says the page
169
+ * itself is missing or unavailable, or null. `findErrorPageClaim` over every
170
+ * string of `fields`, and the record's own field name with the phrase.
171
+ *
172
+ * Each value is checked if it's a string, and walked for the strings it holds
173
+ * if it's an array or an object (a JSON-LD block, a keyword list), up to 8
174
+ * levels deep, 2,000 strings and 100,000 characters in all; numbers, booleans and
175
+ * null are ignored (and don't count toward those limits).
176
+ * `field` is the TOP-LEVEL key, the column or property that holds the copy.
177
+ * `pageText` exempts what the page really says, as in `findErrorPageClaim`; the
178
+ * page's own text is read once for the whole record.
179
+ */
180
+ export declare function findPageCopyClaim(fields: object, pageText?: string | null): {
181
+ field: string;
182
+ phrase: string;
183
+ } | null;