Files
ipal-kit/src/modules/seo/plainText.ts
T
Radosław SmolińskiandClaude Opus 5.5 ecf03c8dc0 IAWL ideas, group B: sizes before work, one link rule, no stand-in locale, strict switches
- Request bodies from strangers are read only up to a cap, declared length or not:
  mail-service confirm, CSP reports, page views, MCP (maxBytes, 413). readCappedBody moves
  to shared/ (the consent log keeps exporting it).
- searchContent / createSearchRoute / MCP search: queryLimit (default 200 characters,
  10 words) before the database; an unknown ?locale is the default locale throughout.
- Rich text: autolinks and links marked internal pass safeHref too; lexicalToMarkdown
  drops unsafe link targets.
- truncate() never returns more than its limit (summary / meta autofill after validation).
- resolveRoute answers null for a locale the site does not have (xx, all, *): no
  default-locale page under another code (page route past the proxy, /md/xx, MCP).
- A 404 page that is a draft is not shown (resolveNotFoundPage, getNotFoundPage).
- readPublished() access rule (exported); Site integrations readVersions: admin.
- Plugin switches, buildCsp, buildSecurityHeaders and the seed guard refuse nonsense
  values at start; an unknown role name is met by nobody, requireRole throws on one.
- Docs, AGENT.md, iapl-collection and iapl-audit skills.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-26 20:25:26 +01:00

53 lines
2.1 KiB
TypeScript

/** Rough Markdown → plain text, enough for a summary (drops syntax, keeps words). */
export function mdToPlain(md: string): string {
return md
.replace(/!\[[^\]]*\]\([^)]*\)/g, '') // images
.replace(/\[([^\]]*)\]\([^)]*\)/g, '$1') // links → their text
.replace(/^#{1,6}\s+/gm, '') // heading markers
.replace(/^[>\-*]\s+/gm, '') // quote/list markers
.replace(/[*_~`]/g, '') // inline emphasis/code marks
.replace(/\s+/g, ' ')
.trim()
}
/** Truncate at a word boundary, adding an ellipsis when cut. */
export function truncate(text: string, max: number): string {
if (text.length <= max) {return text}
// one place kept for the "…": the result is never longer than `max` (a field's maxLength, a summary limit)
const slice = text.slice(0, Math.max(0, max - 1))
const lastSpace = slice.lastIndexOf(' ')
return `${(lastSpace > max * 0.6 ? slice.slice(0, lastSpace) : slice).trimEnd()}…`
}
/** Ensure a fragment ends like a sentence, so joined fragments don't run together. */
function terminate(text: string): string {
return /[.!?…:]$/.test(text) ? text : `${text}.`
}
/**
* A meta description from a page's Markdown (blocksToMarkdown output).
*
* Headings are skipped while there's body text — they usually repeat the page
* title, which Google already shows above the snippet. Whole sentences are
* kept up to `max`; a first sentence longer than that is cut at a word
* boundary. Returns '' when the content has no text.
*/
export function describeFromMarkdown(md: string, max = 155): string {
const parts = md.split(/\n{2,}/).map((p) => p.trim()).filter(Boolean)
const body = parts.filter((p) => !/^#{1,6}\s/.test(p) && !/^!\[/.test(p))
const source = (body.length ? body : parts).map(mdToPlain).filter(Boolean).map(terminate)
const text = source.join(' ')
if (!text) {return ''}
if (text.length <= max) {return text}
// Longest run of whole sentences that fits.
const sentences = text.match(/[^.!?…]+[.!?…]+(?=\s|$)/g) ?? []
let out = ''
for (const s of sentences) {
const next = `${out} ${s.trim()}`.trim()
if (next.length > max) {break}
out = next
}
return out || truncate(text, max)
}