@hamur/seo 0.0.0-stage → 0.0.1
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/LICENSE +22 -0
- package/README.md +26 -2
- package/dist/chunks/bun-index-rnjhv41c.js +62 -0
- package/dist/chunks/bun-index-xf4srhvn.js +97 -0
- package/dist/src/adapters/html.js +40 -0
- package/dist/src/adapters/preact.js +36 -0
- package/dist/src/adapters/react.js +36 -0
- package/dist/src/adapters/solid.js +35 -0
- package/dist/src/adapters/svelte.js +35 -0
- package/dist/src/adapters/vue.js +43 -0
- package/dist/src/index.js +1375 -0
- package/dist/src/schema.js +51 -0
- package/dist/src/server/index.js +1 -0
- package/dist/src/share.js +40 -0
- package/dist/types/src/adapters/html.d.ts +6 -0
- package/dist/types/src/adapters/preact.d.ts +5 -0
- package/dist/types/src/adapters/react.d.ts +5 -0
- package/dist/types/src/adapters/solid.d.ts +6 -0
- package/dist/types/src/adapters/svelte.d.ts +5 -0
- package/dist/types/src/adapters/vue.d.ts +9 -0
- package/dist/types/src/build/links.d.ts +4 -0
- package/dist/types/src/build/llms.d.ts +6 -0
- package/dist/types/src/build/own.d.ts +5 -0
- package/dist/types/src/build/robots.d.ts +3 -0
- package/dist/types/src/build/schema.d.ts +4 -0
- package/dist/types/src/build/sitemap.d.ts +7 -0
- package/dist/types/src/index.d.ts +2 -0
- package/dist/types/src/links/check.d.ts +7 -0
- package/dist/types/src/links/external.d.ts +6 -0
- package/dist/types/src/links/options.d.ts +12 -0
- package/dist/types/src/links/report.d.ts +4 -0
- package/dist/types/src/links/scan.d.ts +24 -0
- package/dist/types/src/llms/options.d.ts +9 -0
- package/dist/types/src/llms/paths.d.ts +8 -0
- package/dist/types/src/llms/text.d.ts +12 -0
- package/dist/types/src/module.d.ts +3 -0
- package/dist/types/src/robots/ai-bots.d.ts +2 -0
- package/dist/types/src/robots/body.d.ts +16 -0
- package/dist/types/src/robots/options.d.ts +9 -0
- package/dist/types/src/runtime/breadcrumbs.d.ts +37 -0
- package/dist/types/src/runtime/markdown.d.ts +15 -0
- package/dist/types/src/runtime/schema-plugin.d.ts +2 -0
- package/dist/types/src/runtime/schema-store.d.ts +34 -0
- package/dist/types/src/schema/graph.d.ts +13 -0
- package/dist/types/src/schema/helpers.d.ts +358 -0
- package/dist/types/src/schema/options.d.ts +7 -0
- package/dist/types/src/schema.d.ts +10 -0
- package/dist/types/src/server/agent.d.ts +4 -0
- package/dist/types/src/server/door.d.ts +5 -0
- package/dist/types/src/server/index.d.ts +1 -0
- package/dist/types/src/server/llms.d.ts +2 -0
- package/dist/types/src/server/render.d.ts +9 -0
- package/dist/types/src/server/robots.d.ts +1 -0
- package/dist/types/src/server/sitemap.d.ts +5 -0
- package/dist/types/src/share.d.ts +18 -0
- package/dist/types/src/sitemap/alternates.d.ts +14 -0
- package/dist/types/src/sitemap/answer.d.ts +6 -0
- package/dist/types/src/sitemap/entries.d.ts +9 -0
- package/dist/types/src/sitemap/options.d.ts +7 -0
- package/dist/types/src/sitemap/paths.d.ts +13 -0
- package/dist/types/src/sitemap/xml.d.ts +23 -0
- package/dist/types/src/types.d.ts +73 -0
- package/package.json +106 -4
- package/src/adapters/html.ts +43 -0
- package/src/adapters/preact.ts +37 -0
- package/src/adapters/react.ts +37 -0
- package/src/adapters/solid.ts +38 -0
- package/src/adapters/svelte.ts +37 -0
- package/src/adapters/vue.ts +55 -0
- package/src/build/links.ts +13 -0
- package/src/build/llms.ts +79 -0
- package/src/build/own.ts +46 -0
- package/src/build/robots.ts +58 -0
- package/src/build/schema.ts +23 -0
- package/src/build/sitemap.ts +101 -0
- package/src/env.d.ts +44 -0
- package/src/index.ts +14 -0
- package/src/links/check.ts +233 -0
- package/src/links/external.ts +41 -0
- package/src/links/options.ts +69 -0
- package/src/links/report.ts +49 -0
- package/src/links/scan.ts +123 -0
- package/src/llms/options.ts +49 -0
- package/src/llms/paths.ts +33 -0
- package/src/llms/text.ts +26 -0
- package/src/module.ts +22 -0
- package/src/robots/ai-bots.ts +14 -0
- package/src/robots/body.ts +51 -0
- package/src/robots/options.ts +75 -0
- package/src/runtime/breadcrumbs.ts +84 -0
- package/src/runtime/markdown.ts +381 -0
- package/src/runtime/schema-plugin.ts +22 -0
- package/src/runtime/schema-store.ts +144 -0
- package/src/schema/graph.ts +125 -0
- package/src/schema/helpers.ts +372 -0
- package/src/schema/options.ts +39 -0
- package/src/schema.ts +5 -0
- package/src/server/agent.ts +45 -0
- package/src/server/door.ts +8 -0
- package/src/server/index.ts +1 -0
- package/src/server/llms.ts +139 -0
- package/src/server/render.ts +33 -0
- package/src/server/robots.ts +19 -0
- package/src/server/sitemap.ts +222 -0
- package/src/share.ts +56 -0
- package/src/sitemap/alternates.ts +66 -0
- package/src/sitemap/answer.ts +42 -0
- package/src/sitemap/entries.ts +136 -0
- package/src/sitemap/options.ts +47 -0
- package/src/sitemap/paths.ts +39 -0
- package/src/sitemap/xml.ts +71 -0
- package/src/types.ts +89 -0
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { LlmsConfig } from "../types.ts"
|
|
2
|
+
import { DiagnosticError } from "hamur/kit"
|
|
3
|
+
|
|
4
|
+
export interface ResolvedLlms {
|
|
5
|
+
title?: string
|
|
6
|
+
description?: string
|
|
7
|
+
exclude: string[]
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const KEYS = ["title", "description", "exclude"]
|
|
11
|
+
|
|
12
|
+
const show = (value: unknown): string => JSON.stringify(value) ?? String(value)
|
|
13
|
+
|
|
14
|
+
function fail(message: string): never {
|
|
15
|
+
throw new DiagnosticError("HAMUR_E6404", message)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** `seo.llms`, off by default; `undefined` when it is off. */
|
|
19
|
+
export function resolveLlms(input: unknown): ResolvedLlms | undefined {
|
|
20
|
+
if (input === undefined || input === false) return undefined
|
|
21
|
+
if (input === true) return { exclude: [] }
|
|
22
|
+
if (typeof input !== "object" || input === null || Array.isArray(input))
|
|
23
|
+
fail(`seo.llms must be a boolean or an object (got ${show(input)})`)
|
|
24
|
+
for (const key of Object.keys(input)) if (!KEYS.includes(key)) fail(`seo.llms.${key} is not an option`)
|
|
25
|
+
const { title, description, exclude = [] } = input as LlmsConfig
|
|
26
|
+
for (const [key, value] of [
|
|
27
|
+
["title", title],
|
|
28
|
+
["description", description],
|
|
29
|
+
] as const) {
|
|
30
|
+
if (value !== undefined && (typeof value !== "string" || value === ""))
|
|
31
|
+
fail(`seo.llms.${key} must be a non-empty string (got ${show(value)})`)
|
|
32
|
+
}
|
|
33
|
+
if (!Array.isArray(exclude)) fail(`seo.llms.exclude must be an array of route rule patterns (got ${show(exclude)})`)
|
|
34
|
+
exclude.forEach((pattern, index) => {
|
|
35
|
+
if (typeof pattern !== "string" || !pattern.startsWith("/"))
|
|
36
|
+
fail(`seo.llms.exclude[${index}] must be a route rule pattern starting with "/"`)
|
|
37
|
+
})
|
|
38
|
+
return {
|
|
39
|
+
...(title === undefined ? {} : { title }),
|
|
40
|
+
...(description === undefined ? {} : { description }),
|
|
41
|
+
exclude: [...exclude],
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** `/llms.txt` and `/llms-full.txt` have one owner. */
|
|
46
|
+
export function checkOwner(contentLlms: unknown): void {
|
|
47
|
+
if (contentLlms !== undefined && contentLlms !== false)
|
|
48
|
+
fail("seo.llms and content.llms both serve /llms.txt; set one")
|
|
49
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** `/` is `/index.md`, a trailing slash is dropped, so `/docs/` is `/docs.md`. */
|
|
2
|
+
export const markdownPath = (path: string): string => (path === "/" ? "/index.md" : `${path.replace(/\/+$/, "")}.md`)
|
|
3
|
+
|
|
4
|
+
/** The page path a `<path>.md` request stands for; `undefined` for a path that does not end in `.md`. */
|
|
5
|
+
export function pageOfMarkdown(path: string): string | undefined {
|
|
6
|
+
if (!path.endsWith(".md") || path.endsWith("/.md")) return undefined
|
|
7
|
+
const page = path.slice(0, -3)
|
|
8
|
+
return page === "/index" ? "/" : page
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** Whether an `Accept` header names `text/markdown` with a higher quality than `text/html`, or names no `text/html`. */
|
|
12
|
+
export function prefersMarkdown(accept: string | null): boolean {
|
|
13
|
+
if (!accept) return false
|
|
14
|
+
const quality = new Map<string, number>()
|
|
15
|
+
for (const part of accept.split(",")) {
|
|
16
|
+
const [type = "", ...params] = part.split(";").map((piece) => piece.trim())
|
|
17
|
+
const q = params.map((param) => /^q\s*=\s*([\d.]+)$/i.exec(param)?.[1]).find((value) => value !== undefined)
|
|
18
|
+
const value = q === undefined ? 1 : Number(q)
|
|
19
|
+
const name = type.toLowerCase()
|
|
20
|
+
quality.set(name, Math.max(quality.get(name) ?? 0, Number.isNaN(value) ? 0 : value))
|
|
21
|
+
}
|
|
22
|
+
const markdown = quality.get("text/markdown") ?? 0
|
|
23
|
+
const html = quality.get("text/html")
|
|
24
|
+
return markdown > 0 && (html === undefined || markdown > html)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The pathname below `app.baseURL`, `undefined` outside it. */
|
|
28
|
+
export function pathBelow(pathname: string, baseURL: string): string | undefined {
|
|
29
|
+
const base = baseURL.replace(/\/+$/, "")
|
|
30
|
+
if (base === "") return pathname
|
|
31
|
+
if (pathname === base) return "/"
|
|
32
|
+
return pathname.startsWith(`${base}/`) ? pathname.slice(base.length) : undefined
|
|
33
|
+
}
|
package/src/llms/text.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export interface LlmsPage {
|
|
2
|
+
title: string
|
|
3
|
+
url: string
|
|
4
|
+
description?: string
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
const oneLine = (text: string): string => text.replace(/\s+/g, " ").trim()
|
|
8
|
+
|
|
9
|
+
const linkText = (text: string): string => oneLine(text).replace(/[[\]]/g, "\\$&")
|
|
10
|
+
|
|
11
|
+
/** The llmstxt.org layout, for `content.llms`. */
|
|
12
|
+
export function llmsText(
|
|
13
|
+
head: { title: string; description?: string | undefined },
|
|
14
|
+
pages: readonly LlmsPage[],
|
|
15
|
+
): string {
|
|
16
|
+
const lines = [`# ${oneLine(head.title)}`, ""]
|
|
17
|
+
if (head.description) lines.push(`> ${oneLine(head.description)}`, "")
|
|
18
|
+
lines.push("## Pages", "")
|
|
19
|
+
for (const page of pages)
|
|
20
|
+
lines.push(`- [${linkText(page.title)}](${page.url})${page.description ? `: ${oneLine(page.description)}` : ""}`)
|
|
21
|
+
return `${lines.join("\n").trimEnd()}\n`
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Every page's Markdown joined by a rule. */
|
|
25
|
+
export const llmsFullText = (sources: readonly string[]): string =>
|
|
26
|
+
`${sources.map((source) => source.trimEnd()).join("\n\n---\n\n")}\n`
|
package/src/module.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { addServer, createResolver, defineModule } from "hamur/kit"
|
|
2
|
+
import { setupLinkChecker } from "./build/links.ts"
|
|
3
|
+
import { readLlms, setupLlms } from "./build/llms.ts"
|
|
4
|
+
import { setupRobots } from "./build/robots.ts"
|
|
5
|
+
import { setupSchemaOrg } from "./build/schema.ts"
|
|
6
|
+
import { setupSitemap } from "./build/sitemap.ts"
|
|
7
|
+
import { resolveSitemap } from "./sitemap/options.ts"
|
|
8
|
+
import type { SeoConfig } from "./types.ts"
|
|
9
|
+
|
|
10
|
+
export default defineModule<SeoConfig>({
|
|
11
|
+
meta: { name: "seo", configKey: "seo" },
|
|
12
|
+
setup(input, hamur) {
|
|
13
|
+
const sitemap = resolveSitemap(input.sitemap)
|
|
14
|
+
setupRobots(input.robots, hamur, sitemap !== undefined)
|
|
15
|
+
const llms = readLlms(input.llms, hamur)
|
|
16
|
+
setupSitemap(sitemap, hamur, llms !== undefined)
|
|
17
|
+
setupSchemaOrg(input.schemaOrg, hamur)
|
|
18
|
+
setupLinkChecker(input.linkChecker, hamur)
|
|
19
|
+
setupLlms(llms, hamur)
|
|
20
|
+
addServer(createResolver(import.meta.url).resolve("./server/door.ts"))
|
|
21
|
+
},
|
|
22
|
+
})
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** Training and indexing crawlers; agents that fetch one page for a user who asked (`ChatGPT-User`) are not listed. */
|
|
2
|
+
export const AI_BOTS = [
|
|
3
|
+
"GPTBot",
|
|
4
|
+
"ClaudeBot",
|
|
5
|
+
"anthropic-ai",
|
|
6
|
+
"CCBot",
|
|
7
|
+
"Google-Extended",
|
|
8
|
+
"Applebot-Extended",
|
|
9
|
+
"PerplexityBot",
|
|
10
|
+
"Bytespider",
|
|
11
|
+
"meta-externalagent",
|
|
12
|
+
"Amazonbot",
|
|
13
|
+
"cohere-ai",
|
|
14
|
+
] as const
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { AI_BOTS } from "./ai-bots.ts"
|
|
2
|
+
import type { ResolvedRobots } from "./options.ts"
|
|
3
|
+
|
|
4
|
+
export interface BodyInput {
|
|
5
|
+
robots: ResolvedRobots
|
|
6
|
+
/** The patterns of `routeRules` whose merged rule has `noindex: true`, in table order. */
|
|
7
|
+
noindex: string[]
|
|
8
|
+
/** `app.baseURL`. */
|
|
9
|
+
baseURL: string
|
|
10
|
+
/** The locale codes whose pages `@hamur/i18n` copies under a prefix. */
|
|
11
|
+
prefixes: string[]
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** The answer when `site.indexable` is false. */
|
|
15
|
+
export const NOINDEX_BODY = "User-agent: *\nDisallow: /\n"
|
|
16
|
+
|
|
17
|
+
/** A route rule pattern as an RFC 9309 path below `prefix`; a trailing `/**` or `/*` becomes the folder, any other path ends in `$`. */
|
|
18
|
+
export function rulePath(pattern: string, prefix: string): string {
|
|
19
|
+
const segments = pattern.split("/").map((segment) => (segment.startsWith(":") || segment === "**" ? "*" : segment))
|
|
20
|
+
if (pattern.endsWith("/**") || pattern.endsWith("/*")) return `${prefix}${segments.slice(0, -1).join("/")}/`
|
|
21
|
+
return `${prefix}${segments.join("/")}$`
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function ruleLines({ noindex, baseURL, prefixes }: BodyInput): string[] {
|
|
25
|
+
const base = baseURL.replace(/\/$/, "")
|
|
26
|
+
const lines = new Set<string>()
|
|
27
|
+
for (const pattern of noindex) {
|
|
28
|
+
lines.add(`Disallow: ${rulePath(pattern, base)}`)
|
|
29
|
+
for (const code of prefixes)
|
|
30
|
+
lines.add(`Disallow: ${pattern === "/" ? `${base}/${code}$` : rulePath(pattern, `${base}/${code}`)}`)
|
|
31
|
+
}
|
|
32
|
+
return [...lines]
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The robots file of an indexable site, without its `Sitemap:` line (the answer adds it, it needs `site.url`). */
|
|
36
|
+
export function robotsBody(input: BodyInput): string {
|
|
37
|
+
const { robots } = input
|
|
38
|
+
const rules = robots.fromRules ? ruleLines(input) : []
|
|
39
|
+
const groups = robots.groups.map((group) => {
|
|
40
|
+
const agents = Array.isArray(group.userAgent) ? group.userAgent : [group.userAgent]
|
|
41
|
+
const own = (group.disallow ?? []).map((path) => `Disallow: ${path}`)
|
|
42
|
+
return [
|
|
43
|
+
...agents.map((agent) => `User-agent: ${agent}`),
|
|
44
|
+
...(group.allow ?? []).map((path) => `Allow: ${path}`),
|
|
45
|
+
...new Set([...own, ...rules]),
|
|
46
|
+
...(group.crawlDelay === undefined ? [] : [`Crawl-delay: ${group.crawlDelay}`]),
|
|
47
|
+
].join("\n")
|
|
48
|
+
})
|
|
49
|
+
if (robots.blockAiBots) for (const bot of AI_BOTS) groups.push(`User-agent: ${bot}\nDisallow: /`)
|
|
50
|
+
return `${groups.join("\n\n")}\n`
|
|
51
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { RobotsConfig, RobotsGroup } from "../types.ts"
|
|
2
|
+
import { DiagnosticError } from "hamur/kit"
|
|
3
|
+
|
|
4
|
+
export interface ResolvedRobots {
|
|
5
|
+
groups: RobotsGroup[]
|
|
6
|
+
blockAiBots: boolean
|
|
7
|
+
fromRules: boolean
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const DEFAULT_GROUPS: RobotsGroup[] = [{ userAgent: "*", allow: ["/"] }]
|
|
11
|
+
|
|
12
|
+
const KEYS = ["groups", "blockAiBots", "fromRules"]
|
|
13
|
+
const GROUP_KEYS = ["userAgent", "allow", "disallow", "crawlDelay"]
|
|
14
|
+
|
|
15
|
+
const show = (value: unknown): string => JSON.stringify(value) ?? String(value)
|
|
16
|
+
|
|
17
|
+
function fail(message: string): never {
|
|
18
|
+
throw new DiagnosticError("HAMUR_E6403", message)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const isObject = (value: unknown): value is Record<string, unknown> =>
|
|
22
|
+
typeof value === "object" && value !== null && !Array.isArray(value)
|
|
23
|
+
|
|
24
|
+
function checkLine(path: string, value: string): void {
|
|
25
|
+
if (/[\r\n]/.test(value)) fail(`${path} cannot contain a line break`)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function checkPaths(path: string, value: unknown): void {
|
|
29
|
+
if (!Array.isArray(value)) fail(`${path} must be an array of paths (got ${show(value)})`)
|
|
30
|
+
value.forEach((entry, index) => {
|
|
31
|
+
const at = `${path}[${index}]`
|
|
32
|
+
if (typeof entry !== "string") fail(`${at} must be a string (got ${show(entry)})`)
|
|
33
|
+
checkLine(at, entry)
|
|
34
|
+
if (!entry.startsWith("/") && !entry.startsWith("*"))
|
|
35
|
+
fail(`${at} ${show(entry)} must be a path starting with "/" or "*"`)
|
|
36
|
+
})
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function checkGroup(group: unknown, index: number): void {
|
|
40
|
+
const path = `seo.robots.groups[${index}]`
|
|
41
|
+
if (!isObject(group)) fail(`${path} must be an object (got ${show(group)})`)
|
|
42
|
+
for (const key of Object.keys(group)) if (!GROUP_KEYS.includes(key)) fail(`${path}.${key} is not an option`)
|
|
43
|
+
const agent = group.userAgent
|
|
44
|
+
const agents = Array.isArray(agent) ? agent : [agent]
|
|
45
|
+
if (!agents.length || agents.some((name) => typeof name !== "string" || name === ""))
|
|
46
|
+
fail(`${path}.userAgent must be a non-empty string or an array of them (got ${show(agent)})`)
|
|
47
|
+
for (const name of agents as string[]) checkLine(`${path}.userAgent`, name)
|
|
48
|
+
if (group.allow !== undefined) checkPaths(`${path}.allow`, group.allow)
|
|
49
|
+
if (group.disallow !== undefined) checkPaths(`${path}.disallow`, group.disallow)
|
|
50
|
+
const delay = group.crawlDelay
|
|
51
|
+
if (delay !== undefined && (typeof delay !== "number" || !Number.isFinite(delay) || delay <= 0))
|
|
52
|
+
fail(`${path}.crawlDelay must be a positive number (got ${show(delay)})`)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** `seo.robots` with its defaults, `undefined` when it is `false`; the first wrong value throws. */
|
|
56
|
+
export function resolveRobots(input: unknown): ResolvedRobots | undefined {
|
|
57
|
+
if (input === undefined || input === true) return { groups: DEFAULT_GROUPS, blockAiBots: false, fromRules: true }
|
|
58
|
+
if (input === false) return undefined
|
|
59
|
+
if (!isObject(input)) fail(`seo.robots must be a boolean or an object (got ${show(input)})`)
|
|
60
|
+
for (const key of Object.keys(input)) if (!KEYS.includes(key)) fail(`seo.robots.${key} is not an option`)
|
|
61
|
+
for (const key of ["blockAiBots", "fromRules"] as const) {
|
|
62
|
+
if (input[key] !== undefined && typeof input[key] !== "boolean")
|
|
63
|
+
fail(`seo.robots.${key} must be a boolean (got ${show(input[key])})`)
|
|
64
|
+
}
|
|
65
|
+
const { groups } = input as RobotsConfig
|
|
66
|
+
if (groups !== undefined) {
|
|
67
|
+
if (!Array.isArray(groups)) fail(`seo.robots.groups must be an array of groups (got ${show(groups)})`)
|
|
68
|
+
groups.forEach(checkGroup)
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
groups: groups?.length ? groups : DEFAULT_GROUPS,
|
|
72
|
+
blockAiBots: input.blockAiBots === true,
|
|
73
|
+
fromRules: input.fromRules !== false,
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { matchRoute, type AppContext } from "hamur/app"
|
|
2
|
+
import { defineBreadcrumbList } from "../schema/helpers.ts"
|
|
3
|
+
import type { SchemaNode } from "../schema.ts"
|
|
4
|
+
import { schemaStoreOf } from "./schema-store.ts"
|
|
5
|
+
|
|
6
|
+
export interface BreadcrumbItem {
|
|
7
|
+
label: string
|
|
8
|
+
to: string | undefined
|
|
9
|
+
current: boolean
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface BreadcrumbOptions {
|
|
13
|
+
hideRoot?: boolean
|
|
14
|
+
overrides?: (Partial<BreadcrumbItem> | false | undefined)[]
|
|
15
|
+
schemaOrg?: boolean
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface I18nLike {
|
|
19
|
+
localeOf(path: string): { code: string }
|
|
20
|
+
localePath(to: string, code: string): string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface BreadcrumbEnv {
|
|
24
|
+
/** The route's app path, below `app.baseURL`, with the locale prefix when it has one. */
|
|
25
|
+
path: string
|
|
26
|
+
resolve(path: string): { name?: string }
|
|
27
|
+
titles: Record<string, string>
|
|
28
|
+
i18n?: I18nLike
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const SEPARATOR = "___"
|
|
32
|
+
|
|
33
|
+
const fromSegment = (segment: string): string => {
|
|
34
|
+
let text = segment
|
|
35
|
+
try {
|
|
36
|
+
text = decodeURIComponent(segment)
|
|
37
|
+
} catch {}
|
|
38
|
+
text = text.replaceAll("-", " ")
|
|
39
|
+
return text.charAt(0).toUpperCase() + text.slice(1)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** One item per path prefix of `env.path`, root first. */
|
|
43
|
+
export function breadcrumbItems(env: BreadcrumbEnv, options: BreadcrumbOptions = {}): BreadcrumbItem[] {
|
|
44
|
+
const code = env.i18n?.localeOf(env.path).code
|
|
45
|
+
const segments = env.path.split("/").filter(Boolean)
|
|
46
|
+
if (segments[0] === code) segments.shift()
|
|
47
|
+
const items: BreadcrumbItem[] = []
|
|
48
|
+
for (let count = 0; count <= segments.length; count++) {
|
|
49
|
+
const prefix = `/${segments.slice(0, count).join("/")}`
|
|
50
|
+
const path = env.i18n && code ? env.i18n.localePath(prefix, code) : prefix
|
|
51
|
+
const name = env.resolve(path).name
|
|
52
|
+
const title = name === undefined ? undefined : (env.titles[name] ?? env.titles[name.split(SEPARATOR)[0]!])
|
|
53
|
+
const label = title ?? (count === 0 ? "Home" : fromSegment(segments[count - 1]!))
|
|
54
|
+
items.push({ label, to: name === undefined ? undefined : path, current: count === segments.length })
|
|
55
|
+
}
|
|
56
|
+
const shown = options.hideRoot ? items.slice(1) : items
|
|
57
|
+
return shown.flatMap((item, index) => {
|
|
58
|
+
const override = options.overrides?.[index]
|
|
59
|
+
if (override === false) return []
|
|
60
|
+
return [override ? { ...item, ...override } : item]
|
|
61
|
+
})
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The `BreadcrumbList` of `items`, or no node when `schemaOrg` is off. */
|
|
65
|
+
export function breadcrumbNodes(app: AppContext, items: BreadcrumbItem[], options: BreadcrumbOptions): SchemaNode[] {
|
|
66
|
+
if (options.schemaOrg === false) return []
|
|
67
|
+
const store = schemaStoreOf(app)
|
|
68
|
+
return [
|
|
69
|
+
defineBreadcrumbList({
|
|
70
|
+
itemListElement: items.map(({ label, to }) => {
|
|
71
|
+
const item = to === undefined ? undefined : store.urlOf(to)
|
|
72
|
+
return { name: label, ...(item === undefined ? {} : { item }) }
|
|
73
|
+
}),
|
|
74
|
+
}),
|
|
75
|
+
]
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export const i18nOf = (app: AppContext): I18nLike | undefined => (app as { i18n?: I18nLike }).i18n
|
|
79
|
+
|
|
80
|
+
/** The page route an app path reaches, from `#hamur/route-paths`, so a page whose record the client has not read still matches. */
|
|
81
|
+
export function routeOf(app: AppContext, path: string): { name?: string } {
|
|
82
|
+
const name = matchRoute(`${schemaStoreOf(app).base}${path}`)?.name
|
|
83
|
+
return name === undefined ? {} : { name }
|
|
84
|
+
}
|