@hamur/seo 0.0.0-stage → 0.0.2

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.
Files changed (112) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +26 -2
  3. package/dist/chunks/bun-index-rnjhv41c.js +62 -0
  4. package/dist/chunks/bun-index-xf4srhvn.js +97 -0
  5. package/dist/src/adapters/html.js +40 -0
  6. package/dist/src/adapters/preact.js +36 -0
  7. package/dist/src/adapters/react.js +36 -0
  8. package/dist/src/adapters/solid.js +35 -0
  9. package/dist/src/adapters/svelte.js +35 -0
  10. package/dist/src/adapters/vue.js +43 -0
  11. package/dist/src/index.js +1375 -0
  12. package/dist/src/schema.js +51 -0
  13. package/dist/src/server/index.js +1 -0
  14. package/dist/src/share.js +40 -0
  15. package/dist/types/src/adapters/html.d.ts +6 -0
  16. package/dist/types/src/adapters/preact.d.ts +5 -0
  17. package/dist/types/src/adapters/react.d.ts +5 -0
  18. package/dist/types/src/adapters/solid.d.ts +6 -0
  19. package/dist/types/src/adapters/svelte.d.ts +5 -0
  20. package/dist/types/src/adapters/vue.d.ts +9 -0
  21. package/dist/types/src/build/links.d.ts +4 -0
  22. package/dist/types/src/build/llms.d.ts +6 -0
  23. package/dist/types/src/build/own.d.ts +5 -0
  24. package/dist/types/src/build/robots.d.ts +3 -0
  25. package/dist/types/src/build/schema.d.ts +4 -0
  26. package/dist/types/src/build/sitemap.d.ts +7 -0
  27. package/dist/types/src/index.d.ts +2 -0
  28. package/dist/types/src/links/check.d.ts +7 -0
  29. package/dist/types/src/links/external.d.ts +6 -0
  30. package/dist/types/src/links/options.d.ts +12 -0
  31. package/dist/types/src/links/report.d.ts +4 -0
  32. package/dist/types/src/links/scan.d.ts +24 -0
  33. package/dist/types/src/llms/options.d.ts +9 -0
  34. package/dist/types/src/llms/paths.d.ts +8 -0
  35. package/dist/types/src/llms/text.d.ts +12 -0
  36. package/dist/types/src/module.d.ts +3 -0
  37. package/dist/types/src/robots/ai-bots.d.ts +2 -0
  38. package/dist/types/src/robots/body.d.ts +16 -0
  39. package/dist/types/src/robots/options.d.ts +9 -0
  40. package/dist/types/src/runtime/breadcrumbs.d.ts +37 -0
  41. package/dist/types/src/runtime/markdown.d.ts +15 -0
  42. package/dist/types/src/runtime/schema-plugin.d.ts +2 -0
  43. package/dist/types/src/runtime/schema-store.d.ts +34 -0
  44. package/dist/types/src/schema/graph.d.ts +13 -0
  45. package/dist/types/src/schema/helpers.d.ts +358 -0
  46. package/dist/types/src/schema/options.d.ts +7 -0
  47. package/dist/types/src/schema.d.ts +10 -0
  48. package/dist/types/src/server/agent.d.ts +4 -0
  49. package/dist/types/src/server/door.d.ts +5 -0
  50. package/dist/types/src/server/index.d.ts +1 -0
  51. package/dist/types/src/server/llms.d.ts +2 -0
  52. package/dist/types/src/server/render.d.ts +9 -0
  53. package/dist/types/src/server/robots.d.ts +1 -0
  54. package/dist/types/src/server/sitemap.d.ts +5 -0
  55. package/dist/types/src/share.d.ts +18 -0
  56. package/dist/types/src/sitemap/alternates.d.ts +14 -0
  57. package/dist/types/src/sitemap/answer.d.ts +6 -0
  58. package/dist/types/src/sitemap/entries.d.ts +9 -0
  59. package/dist/types/src/sitemap/options.d.ts +7 -0
  60. package/dist/types/src/sitemap/paths.d.ts +13 -0
  61. package/dist/types/src/sitemap/xml.d.ts +23 -0
  62. package/dist/types/src/types.d.ts +73 -0
  63. package/package.json +136 -4
  64. package/src/adapters/html.ts +43 -0
  65. package/src/adapters/preact.ts +37 -0
  66. package/src/adapters/react.ts +37 -0
  67. package/src/adapters/solid.ts +38 -0
  68. package/src/adapters/svelte.ts +37 -0
  69. package/src/adapters/vue.ts +55 -0
  70. package/src/build/links.ts +13 -0
  71. package/src/build/llms.ts +79 -0
  72. package/src/build/own.ts +46 -0
  73. package/src/build/robots.ts +58 -0
  74. package/src/build/schema.ts +23 -0
  75. package/src/build/sitemap.ts +101 -0
  76. package/src/env.d.ts +44 -0
  77. package/src/index.ts +14 -0
  78. package/src/links/check.ts +233 -0
  79. package/src/links/external.ts +41 -0
  80. package/src/links/options.ts +69 -0
  81. package/src/links/report.ts +49 -0
  82. package/src/links/scan.ts +123 -0
  83. package/src/llms/options.ts +49 -0
  84. package/src/llms/paths.ts +33 -0
  85. package/src/llms/text.ts +26 -0
  86. package/src/module.ts +22 -0
  87. package/src/robots/ai-bots.ts +14 -0
  88. package/src/robots/body.ts +51 -0
  89. package/src/robots/options.ts +75 -0
  90. package/src/runtime/breadcrumbs.ts +84 -0
  91. package/src/runtime/markdown.ts +381 -0
  92. package/src/runtime/schema-plugin.ts +22 -0
  93. package/src/runtime/schema-store.ts +144 -0
  94. package/src/schema/graph.ts +125 -0
  95. package/src/schema/helpers.ts +372 -0
  96. package/src/schema/options.ts +39 -0
  97. package/src/schema.ts +5 -0
  98. package/src/server/agent.ts +45 -0
  99. package/src/server/door.ts +8 -0
  100. package/src/server/index.ts +1 -0
  101. package/src/server/llms.ts +139 -0
  102. package/src/server/render.ts +33 -0
  103. package/src/server/robots.ts +19 -0
  104. package/src/server/sitemap.ts +222 -0
  105. package/src/share.ts +56 -0
  106. package/src/sitemap/alternates.ts +66 -0
  107. package/src/sitemap/answer.ts +42 -0
  108. package/src/sitemap/entries.ts +136 -0
  109. package/src/sitemap/options.ts +47 -0
  110. package/src/sitemap/paths.ts +39 -0
  111. package/src/sitemap/xml.ts +71 -0
  112. package/src/types.ts +89 -0
package/package.json CHANGED
@@ -1,6 +1,138 @@
1
1
  {
2
2
  "name": "@hamur/seo",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.0.2",
4
+ "type": "module",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/productdevbook/hamur.git",
9
+ "directory": "packages/seo"
10
+ },
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/types/src/index.d.ts",
14
+ "bun": "./src/index.ts",
15
+ "hamur-source": "./src/index.ts",
16
+ "node": "./dist/src/index.js",
17
+ "default": "./src/index.ts"
18
+ },
19
+ "./server": {
20
+ "types": "./dist/types/src/server/index.d.ts",
21
+ "bun": "./src/server/index.ts",
22
+ "hamur-source": "./src/server/index.ts",
23
+ "node": "./dist/src/server/index.js",
24
+ "default": "./src/server/index.ts"
25
+ },
26
+ "./schema": {
27
+ "types": "./dist/types/src/schema.d.ts",
28
+ "bun": "./src/schema.ts",
29
+ "hamur-source": "./src/schema.ts",
30
+ "node": "./dist/src/schema.js",
31
+ "default": "./src/schema.ts"
32
+ },
33
+ "./share": {
34
+ "types": "./dist/types/src/share.d.ts",
35
+ "bun": "./src/share.ts",
36
+ "hamur-source": "./src/share.ts",
37
+ "node": "./dist/src/share.js",
38
+ "default": "./src/share.ts"
39
+ },
40
+ "./react": {
41
+ "types": "./dist/types/src/adapters/react.d.ts",
42
+ "bun": "./src/adapters/react.ts",
43
+ "hamur-source": "./src/adapters/react.ts",
44
+ "node": "./dist/src/adapters/react.js",
45
+ "default": "./src/adapters/react.ts"
46
+ },
47
+ "./vue": {
48
+ "types": "./dist/types/src/adapters/vue.d.ts",
49
+ "bun": "./src/adapters/vue.ts",
50
+ "hamur-source": "./src/adapters/vue.ts",
51
+ "node": "./dist/src/adapters/vue.js",
52
+ "default": "./src/adapters/vue.ts"
53
+ },
54
+ "./preact": {
55
+ "types": "./dist/types/src/adapters/preact.d.ts",
56
+ "bun": "./src/adapters/preact.ts",
57
+ "hamur-source": "./src/adapters/preact.ts",
58
+ "node": "./dist/src/adapters/preact.js",
59
+ "default": "./src/adapters/preact.ts"
60
+ },
61
+ "./svelte": {
62
+ "types": "./dist/types/src/adapters/svelte.d.ts",
63
+ "bun": "./src/adapters/svelte.ts",
64
+ "hamur-source": "./src/adapters/svelte.ts",
65
+ "node": "./dist/src/adapters/svelte.js",
66
+ "default": "./src/adapters/svelte.ts"
67
+ },
68
+ "./solid": {
69
+ "types": "./dist/types/src/adapters/solid.d.ts",
70
+ "bun": "./src/adapters/solid.ts",
71
+ "hamur-source": "./src/adapters/solid.ts",
72
+ "node": "./dist/src/adapters/solid.js",
73
+ "default": "./src/adapters/solid.ts"
74
+ },
75
+ "./html": {
76
+ "types": "./dist/types/src/adapters/html.d.ts",
77
+ "bun": "./src/adapters/html.ts",
78
+ "hamur-source": "./src/adapters/html.ts",
79
+ "node": "./dist/src/adapters/html.js",
80
+ "default": "./src/adapters/html.ts"
81
+ },
82
+ "./package.json": "./package.json"
83
+ },
84
+ "files": [
85
+ "dist",
86
+ "src"
87
+ ],
88
+ "dependencies": {},
89
+ "peerDependencies": {
90
+ "hamur": "~0.0.2",
91
+ "@hamur/adapter-react": "^0.0.2",
92
+ "@hamur/adapter-vue": "^0.0.2",
93
+ "@hamur/adapter-preact": "^0.0.2",
94
+ "@hamur/adapter-svelte": "^0.0.2",
95
+ "@hamur/adapter-solid": "^0.0.2",
96
+ "@hamur/adapter-html": "^0.0.2",
97
+ "react": "^19.3.1",
98
+ "preact": "^11.0.2",
99
+ "vue": "^3.5.44",
100
+ "svelte": "^5.57.2",
101
+ "solid-js": "^2.0.4"
102
+ },
103
+ "peerDependenciesMeta": {
104
+ "@hamur/adapter-react": {
105
+ "optional": true
106
+ },
107
+ "@hamur/adapter-vue": {
108
+ "optional": true
109
+ },
110
+ "@hamur/adapter-preact": {
111
+ "optional": true
112
+ },
113
+ "@hamur/adapter-svelte": {
114
+ "optional": true
115
+ },
116
+ "@hamur/adapter-solid": {
117
+ "optional": true
118
+ },
119
+ "@hamur/adapter-html": {
120
+ "optional": true
121
+ },
122
+ "react": {
123
+ "optional": true
124
+ },
125
+ "preact": {
126
+ "optional": true
127
+ },
128
+ "vue": {
129
+ "optional": true
130
+ },
131
+ "svelte": {
132
+ "optional": true
133
+ },
134
+ "solid-js": {
135
+ "optional": true
136
+ }
137
+ }
138
+ }
@@ -0,0 +1,43 @@
1
+ import { useApp, useId, useRoute } from "@hamur/adapter-html"
2
+ import type { AppContext } from "hamur/app"
3
+ import { appOfCall, nodeList, schemaStoreOf } from "../runtime/schema-store.ts"
4
+ import {
5
+ breadcrumbItems,
6
+ breadcrumbNodes,
7
+ i18nOf,
8
+ routeOf,
9
+ type BreadcrumbItem,
10
+ type BreadcrumbOptions,
11
+ } from "../runtime/breadcrumbs.ts"
12
+ import type { SchemaNode } from "../schema.ts"
13
+
14
+ export type { BreadcrumbItem, BreadcrumbOptions, SchemaNode }
15
+
16
+ const hooked = new WeakSet<AppContext>()
17
+ const last = new WeakMap<AppContext, number>()
18
+
19
+ /** The calls of a page render are numbered in order; a number that does not grow starts a new render, so the calls of the one before are dropped. */
20
+ export function useSchemaOrg(nodes: SchemaNode | SchemaNode[]): void {
21
+ const app = appOfCall(useApp)
22
+ const store = schemaStoreOf(app)
23
+ if (!hooked.has(app)) {
24
+ hooked.add(app)
25
+ app.hooks.hook("page:start", () => store.clear())
26
+ }
27
+ const id = useId()
28
+ const index = Number(id.slice(2))
29
+ if (index <= (last.get(app) ?? -1)) store.clear()
30
+ last.set(app, index)
31
+ store.set(id, nodeList(nodes))
32
+ }
33
+
34
+ export function useBreadcrumbItems(options: BreadcrumbOptions = {}): BreadcrumbItem[] {
35
+ const app = appOfCall(useApp)
36
+ const path = useRoute().path
37
+ const items = breadcrumbItems(
38
+ { path, resolve: (to) => routeOf(app, to), titles: schemaStoreOf(app).titles, i18n: i18nOf(app) },
39
+ options,
40
+ )
41
+ useSchemaOrg(breadcrumbNodes(app, items, options))
42
+ return items
43
+ }
@@ -0,0 +1,37 @@
1
+ import { useApp, useRoute } from "@hamur/adapter-preact"
2
+ import { useLayoutEffect, useRef } from "preact/hooks"
3
+ import { appOfCall, nodeList, schemaStoreOf } from "../runtime/schema-store.ts"
4
+ import {
5
+ breadcrumbItems,
6
+ breadcrumbNodes,
7
+ i18nOf,
8
+ routeOf,
9
+ type BreadcrumbItem,
10
+ type BreadcrumbOptions,
11
+ } from "../runtime/breadcrumbs.ts"
12
+ import type { SchemaNode } from "../schema.ts"
13
+
14
+ export type { BreadcrumbItem, BreadcrumbOptions, SchemaNode }
15
+
16
+ const isServer = typeof window === "undefined"
17
+
18
+ export function useSchemaOrg(nodes: SchemaNode | SchemaNode[]): void {
19
+ const store = schemaStoreOf(appOfCall(useApp))
20
+ const key = useRef({}).current
21
+ if (isServer) store.set(key, nodeList(nodes))
22
+ useLayoutEffect(() => {
23
+ store.set(key, nodeList(nodes))
24
+ })
25
+ useLayoutEffect(() => () => store.remove(key), [store, key])
26
+ }
27
+
28
+ export function useBreadcrumbItems(options: BreadcrumbOptions = {}): BreadcrumbItem[] {
29
+ const app = appOfCall(useApp)
30
+ const path = useRoute().path
31
+ const items = breadcrumbItems(
32
+ { path, resolve: (to) => routeOf(app, to), titles: schemaStoreOf(app).titles, i18n: i18nOf(app) },
33
+ options,
34
+ )
35
+ useSchemaOrg(breadcrumbNodes(app, items, options))
36
+ return items
37
+ }
@@ -0,0 +1,37 @@
1
+ import { useApp, useRoute } from "@hamur/adapter-react"
2
+ import { useId, useLayoutEffect } from "react"
3
+ import { appOfCall, nodeList, schemaStoreOf } from "../runtime/schema-store.ts"
4
+ import {
5
+ breadcrumbItems,
6
+ breadcrumbNodes,
7
+ i18nOf,
8
+ routeOf,
9
+ type BreadcrumbItem,
10
+ type BreadcrumbOptions,
11
+ } from "../runtime/breadcrumbs.ts"
12
+ import type { SchemaNode } from "../schema.ts"
13
+
14
+ export type { BreadcrumbItem, BreadcrumbOptions, SchemaNode }
15
+
16
+ const isServer = typeof window === "undefined"
17
+
18
+ export function useSchemaOrg(nodes: SchemaNode | SchemaNode[]): void {
19
+ const store = schemaStoreOf(appOfCall(useApp))
20
+ const key = useId()
21
+ if (isServer) store.set(key, nodeList(nodes))
22
+ useLayoutEffect(() => {
23
+ store.set(key, nodeList(nodes))
24
+ })
25
+ useLayoutEffect(() => () => store.remove(key), [store, key])
26
+ }
27
+
28
+ export function useBreadcrumbItems(options: BreadcrumbOptions = {}): BreadcrumbItem[] {
29
+ const app = appOfCall(useApp)
30
+ const path = useRoute().path
31
+ const items = breadcrumbItems(
32
+ { path, resolve: (to) => routeOf(app, to), titles: schemaStoreOf(app).titles, i18n: i18nOf(app) },
33
+ options,
34
+ )
35
+ useSchemaOrg(breadcrumbNodes(app, items, options))
36
+ return items
37
+ }
@@ -0,0 +1,38 @@
1
+ import { useApp, useHead, useRoute } from "@hamur/adapter-solid"
2
+ import { createMemo, onCleanup, type Accessor } from "solid-js"
3
+ import { appOfCall, nodeList, schemaStoreOf } from "../runtime/schema-store.ts"
4
+ import {
5
+ breadcrumbItems,
6
+ breadcrumbNodes,
7
+ i18nOf,
8
+ routeOf,
9
+ type BreadcrumbItem,
10
+ type BreadcrumbOptions,
11
+ } from "../runtime/breadcrumbs.ts"
12
+ import type { SchemaNode } from "../schema.ts"
13
+
14
+ export type { BreadcrumbItem, BreadcrumbOptions, SchemaNode }
15
+
16
+ export function useSchemaOrg(nodes: SchemaNode | SchemaNode[] | (() => SchemaNode | SchemaNode[])): void {
17
+ const store = schemaStoreOf(appOfCall(useApp))
18
+ const key = {}
19
+ // useHead tracks the getter in an effect and owns the apply.
20
+ useHead(() => {
21
+ store.set(key, nodeList(typeof nodes === "function" ? nodes() : nodes))
22
+ return {}
23
+ })
24
+ if (typeof document !== "undefined") onCleanup(() => store.remove(key))
25
+ }
26
+
27
+ export function useBreadcrumbItems(options: BreadcrumbOptions = {}): Accessor<BreadcrumbItem[]> {
28
+ const app = appOfCall(useApp)
29
+ const route = useRoute()
30
+ const items = createMemo(() =>
31
+ breadcrumbItems(
32
+ { path: route().path, resolve: (to) => routeOf(app, to), titles: schemaStoreOf(app).titles, i18n: i18nOf(app) },
33
+ options,
34
+ ),
35
+ )
36
+ useSchemaOrg(() => breadcrumbNodes(app, items(), options))
37
+ return items
38
+ }
@@ -0,0 +1,37 @@
1
+ import { useApp, useHead, useRoute } from "@hamur/adapter-svelte"
2
+ import { onDestroy } from "svelte"
3
+ import { appOfCall, nodeList, schemaStoreOf } from "../runtime/schema-store.ts"
4
+ import {
5
+ breadcrumbItems,
6
+ breadcrumbNodes,
7
+ i18nOf,
8
+ routeOf,
9
+ type BreadcrumbItem,
10
+ type BreadcrumbOptions,
11
+ } from "../runtime/breadcrumbs.ts"
12
+ import type { SchemaNode } from "../schema.ts"
13
+
14
+ export type { BreadcrumbItem, BreadcrumbOptions, SchemaNode }
15
+
16
+ export function useSchemaOrg(nodes: SchemaNode | SchemaNode[] | (() => SchemaNode | SchemaNode[])): void {
17
+ const store = schemaStoreOf(appOfCall(useApp))
18
+ const key = {}
19
+ // useHead reads the getter again in an $effect and owns the apply.
20
+ useHead(() => {
21
+ store.set(key, nodeList(typeof nodes === "function" ? nodes() : nodes))
22
+ return {}
23
+ })
24
+ if (typeof document !== "undefined") onDestroy(() => store.remove(key))
25
+ }
26
+
27
+ export function useBreadcrumbItems(options: BreadcrumbOptions = {}): BreadcrumbItem[] {
28
+ const app = appOfCall(useApp)
29
+ const route = useRoute()
30
+ const read = () =>
31
+ breadcrumbItems(
32
+ { path: route.path, resolve: (to) => routeOf(app, to), titles: schemaStoreOf(app).titles, i18n: i18nOf(app) },
33
+ options,
34
+ )
35
+ useSchemaOrg(() => breadcrumbNodes(app, read(), options))
36
+ return read()
37
+ }
@@ -0,0 +1,55 @@
1
+ import { useApp, useHead, useRoute } from "@hamur/adapter-vue"
2
+ import { computed, getCurrentScope, onScopeDispose, toValue, type ComputedRef, type MaybeRefOrGetter } from "vue"
3
+ import { appOfCall, nodeList, schemaStoreOf } from "../runtime/schema-store.ts"
4
+ import {
5
+ breadcrumbItems,
6
+ breadcrumbNodes,
7
+ i18nOf,
8
+ routeOf,
9
+ type BreadcrumbItem,
10
+ type BreadcrumbOptions,
11
+ } from "../runtime/breadcrumbs.ts"
12
+ import type { SchemaNode } from "../schema.ts"
13
+
14
+ export type { BreadcrumbItem, BreadcrumbOptions, SchemaNode }
15
+
16
+ type MaybeRefOrGetterDeep<T> = MaybeRefOrGetter<
17
+ T extends (...args: never[]) => unknown
18
+ ? T
19
+ : T extends readonly (infer U)[]
20
+ ? MaybeRefOrGetterDeep<U>[]
21
+ : T extends object
22
+ ? { [K in keyof T]: MaybeRefOrGetterDeep<T[K]> }
23
+ : T
24
+ >
25
+
26
+ function resolve(value: unknown): unknown {
27
+ const read = toValue(value as MaybeRefOrGetter<unknown>)
28
+ if (Array.isArray(read)) return read.map(resolve)
29
+ if (typeof read !== "object" || read === null || read instanceof Date) return read
30
+ return Object.fromEntries(Object.entries(read).map(([name, item]) => [name, resolve(item)]))
31
+ }
32
+
33
+ export function useSchemaOrg(nodes: MaybeRefOrGetterDeep<SchemaNode | SchemaNode[]>): void {
34
+ const store = schemaStoreOf(appOfCall(useApp))
35
+ const key = {}
36
+ // useHead re-reads the getter whenever a ref in it changes and owns the watch and the apply.
37
+ useHead(() => {
38
+ store.set(key, nodeList(resolve(nodes) as SchemaNode | SchemaNode[]))
39
+ return {}
40
+ })
41
+ if (typeof document !== "undefined" && getCurrentScope()) onScopeDispose(() => store.remove(key))
42
+ }
43
+
44
+ export function useBreadcrumbItems(options: BreadcrumbOptions = {}): ComputedRef<BreadcrumbItem[]> {
45
+ const app = appOfCall(useApp)
46
+ const route = useRoute()
47
+ const items = computed(() =>
48
+ breadcrumbItems(
49
+ { path: route.path, resolve: (to) => routeOf(app, to), titles: schemaStoreOf(app).titles, i18n: i18nOf(app) },
50
+ options,
51
+ ),
52
+ )
53
+ useSchemaOrg((() => breadcrumbNodes(app, items.value, options)) as Parameters<typeof useSchemaOrg>[0])
54
+ return items
55
+ }
@@ -0,0 +1,13 @@
1
+ import type { HamurContext } from "hamur/kit"
2
+ import { createLinkChecker } from "../links/check.ts"
3
+ import { resolveLinkChecker } from "../links/options.ts"
4
+ import type { SeoConfig } from "../types.ts"
5
+
6
+ /** The link rules over every page the prerender writes, reported in `prerender:done`. */
7
+ export function setupLinkChecker(value: SeoConfig["linkChecker"], hamur: HamurContext): void {
8
+ const options = resolveLinkChecker(value)
9
+ if (!options) return
10
+ const checker = createLinkChecker(hamur, options)
11
+ hamur.hooks.hook("prerender:page", checker.page)
12
+ hamur.hooks.hook("prerender:done", checker.done)
13
+ }
@@ -0,0 +1,79 @@
1
+ import { existsSync } from "node:fs"
2
+ import { join } from "node:path"
3
+ import { addTemplate, createRouteRules, type HamurContext } from "hamur/kit"
4
+ import { absoluteUrl } from "hamur/server/internal"
5
+ import { htmlToMarkdown } from "../runtime/markdown.ts"
6
+ import { checkOwner, resolveLlms, type ResolvedLlms } from "../llms/options.ts"
7
+ import { markdownPath } from "../llms/paths.ts"
8
+ import { yieldToApp } from "./own.ts"
9
+
10
+ const PATH = "/llms.txt"
11
+ const FULL = "/llms-full.txt"
12
+
13
+ /** `seo.llms` as it resolved, `undefined` while it is off; fails when `content.llms` serves the same files. */
14
+ export function readLlms(input: unknown, hamur: HamurContext): ResolvedLlms | undefined {
15
+ const llms = resolveLlms(input)
16
+ if (llms) checkOwner((hamur.options as unknown as { content?: { llms?: unknown } }).content?.llms)
17
+ return llms
18
+ }
19
+
20
+ /** `/llms.txt`, `/llms-full.txt` and the agent Markdown of every page; the app's `public/llms.txt` or own route wins. */
21
+ export function setupLlms(llms: ResolvedLlms | undefined, hamur: HamurContext): void {
22
+ const { baseURL, trailingSlash, rootId } = hamur.options.app
23
+ const own = yieldToApp(hamur, PATH, llms !== undefined, [FULL])
24
+ if (!llms) {
25
+ addTemplate({
26
+ filename: "seo/llms",
27
+ getContents: () =>
28
+ 'export const enabled = false;\nexport const title = null;\nexport const description = null;\nexport const exclude = [];\nexport const baseURL = "/";\nexport const trailingSlash = "ignore";\nexport const rootId = "";\n',
29
+ })
30
+ return
31
+ }
32
+ addTemplate({
33
+ filename: "seo/llms",
34
+ getContents: () =>
35
+ `${Object.entries({
36
+ enabled: true,
37
+ title: llms.title ?? null,
38
+ description: llms.description ?? null,
39
+ exclude: llms.exclude,
40
+ baseURL,
41
+ trailingSlash,
42
+ rootId,
43
+ })
44
+ .map(([name, value]) => `export const ${name} = ${JSON.stringify(value)};\n`)
45
+ .join("")}`,
46
+ })
47
+
48
+ const excluded = llms.exclude.length
49
+ ? createRouteRules(Object.fromEntries(llms.exclude.map((pattern) => [pattern, {}])))
50
+ : undefined
51
+ hamur.hooks.hook("prerender:routes", (routes) => {
52
+ if (hamur.options.preset !== "static" || !own.served()) return
53
+ routes.add(PATH)
54
+ routes.add(FULL)
55
+ })
56
+ const converted = new Map<string, string>()
57
+ hamur.hooks.hook("prerender:page", (page) => {
58
+ if (
59
+ page.status !== 200 ||
60
+ typeof page.body !== "string" ||
61
+ !(page.headers.get("content-type") ?? "").toLowerCase().startsWith("text/html")
62
+ )
63
+ return
64
+ if (excluded?.match(page.path).matches.length) return
65
+ const origin = hamur.options.site.url ?? process.env.HAMUR_SITE_URL ?? "http://localhost"
66
+ const text = htmlToMarkdown(page.body, { base: absoluteUrl(origin, baseURL, page.path), rootId })
67
+ if (text !== "") converted.set(markdownPath(page.path), text)
68
+ })
69
+ hamur.hooks.hook("prerender:done", async () => {
70
+ const publicDirs = [hamur.options.publicDir, ...hamur.options.layers.map((layer) => layer.publicDir)]
71
+ const out = join(hamur.options.outputDir, "public")
72
+ for (const [path, text] of converted) {
73
+ const name = path.slice(1)
74
+ if ([out, ...publicDirs].some((dir) => existsSync(join(dir, name)))) continue
75
+ await Bun.write(join(out, name), text)
76
+ }
77
+ converted.clear()
78
+ })
79
+ }
@@ -0,0 +1,46 @@
1
+ import { existsSync } from "node:fs"
2
+ import { join, relative } from "node:path"
3
+ import { useLogger, type HamurContext } from "hamur/kit"
4
+
5
+ /** With `enabled` false the module's record is dropped without a word; otherwise the app's `public/<file>` or its own `GET` handler of `path` wins; the module then drops its record in `server:extend` and logs once. `served()` tells whether the module's route stands. `companions` are the module's other routes that stand and fall with it. */
6
+ export function yieldToApp(
7
+ hamur: HamurContext,
8
+ path: string,
9
+ enabled: boolean,
10
+ companions: string[] = [],
11
+ ): { served(): boolean } {
12
+ const log = useLogger("seo")
13
+ const file = path.slice(1)
14
+ const { rootDir, layers } = hamur.options
15
+ const serverDirs = [hamur.options.serverDir, ...layers.map((layer) => layer.serverDir)]
16
+ const publicDirs = [hamur.options.publicDir, ...layers.map((layer) => layer.publicDir)]
17
+ const answers = (record: { path: string; method?: string }) =>
18
+ record.path === path && (record.method === undefined || record.method === "GET")
19
+ const ours = (record: { path: string; method?: string }) =>
20
+ answers(record) || (companions.includes(record.path) && (record.method === undefined || record.method === "GET"))
21
+ const said = new Set<string>()
22
+ let served = true
23
+ hamur.hooks.hook("server:extend", (records) => {
24
+ const dir = publicDirs.find((publicDir) => existsSync(join(publicDir, file)))
25
+ const own = records.find(
26
+ (record) => answers(record) && serverDirs.some((serverDir) => record.file.startsWith(`${serverDir}/`)),
27
+ )
28
+ const reason = dir
29
+ ? `${relative(rootDir, join(dir, file))} exists; @hamur/seo serves no ${path}`
30
+ : own
31
+ ? `${relative(rootDir, own.file)}#${own.fn.export} answers ${path}; @hamur/seo serves none`
32
+ : undefined
33
+ served = enabled && reason === undefined
34
+ if (served) return
35
+ if (reason !== undefined && enabled && !said.has(reason)) {
36
+ said.add(reason)
37
+ log.info(reason)
38
+ }
39
+ for (let index = records.length - 1; index >= 0; index--) {
40
+ const record = records[index]!
41
+ if (ours(record) && !serverDirs.some((serverDir) => record.file.startsWith(`${serverDir}/`)))
42
+ records.splice(index, 1)
43
+ }
44
+ })
45
+ return { served: () => served }
46
+ }
@@ -0,0 +1,58 @@
1
+ import { addRouteRules, addTemplate, diagnostic, useLogger, type HamurContext } from "hamur/kit"
2
+ import { yieldToApp } from "./own.ts"
3
+ import { robotsBody } from "../robots/body.ts"
4
+ import { resolveRobots } from "../robots/options.ts"
5
+
6
+ const PATH = "/robots.txt"
7
+
8
+ type I18nInput = { locales?: unknown; defaultLocale?: unknown; strategy?: unknown }
9
+
10
+ /** The codes whose pages `@hamur/i18n` copies under `/<code>`. */
11
+ function localePrefixes(hamur: HamurContext): string[] {
12
+ const i18n = (hamur.options as unknown as { i18n?: I18nInput }).i18n
13
+ if (!i18n || !Array.isArray(i18n.locales)) return []
14
+ const codes = i18n.locales
15
+ .map((locale: unknown) => (typeof locale === "string" ? locale : (locale as { code?: unknown } | null)?.code))
16
+ .filter((code): code is string => typeof code === "string")
17
+ return i18n.strategy === "prefix" ? codes : codes.filter((code) => code !== i18n.defaultLocale)
18
+ }
19
+
20
+ /** `/robots.txt` from `seo.robots`; the app's own `public/robots.txt` or route wins. */
21
+ export function setupRobots(input: unknown, hamur: HamurContext, sitemap: boolean): void {
22
+ const robots = resolveRobots(input)
23
+ const own = yieldToApp(hamur, PATH, robots !== undefined)
24
+ if (!robots) {
25
+ addTemplate({
26
+ filename: "seo/robots",
27
+ getContents: () => 'export const body = "";\nexport const sitemap = null;\n',
28
+ })
29
+ return
30
+ }
31
+ const log = useLogger("seo")
32
+ const { baseURL } = hamur.options.app
33
+ if (baseURL !== "/")
34
+ log.warn(
35
+ diagnostic(
36
+ "HAMUR_W6409",
37
+ `app.baseURL is "${baseURL}": crawlers read robots.txt only at the origin's root, so ${baseURL}robots.txt is not read`,
38
+ ),
39
+ )
40
+
41
+ addTemplate({
42
+ filename: "seo/robots",
43
+ getContents: (ctx) => {
44
+ const noindex = Object.entries(ctx.routeRules).flatMap(([pattern, rule]) =>
45
+ !Array.isArray(rule) && !/\s/.test(pattern) && rule.noindex === true ? [pattern] : [],
46
+ )
47
+ const body = robotsBody({ robots, noindex, baseURL, prefixes: localePrefixes(ctx) })
48
+ return `export const body = ${JSON.stringify(body)};\nexport const sitemap = ${JSON.stringify(sitemap ? `${baseURL}sitemap.xml` : null)};\n`
49
+ },
50
+ })
51
+ addRouteRules({ [PATH]: { swr: 600 } })
52
+
53
+ if (hamur.options.preset === "static") {
54
+ hamur.hooks.hook("prerender:routes", (routes) => {
55
+ if (own.served()) routes.add(PATH)
56
+ })
57
+ }
58
+ }
@@ -0,0 +1,23 @@
1
+ import { addAppPlugin, addTemplate, createResolver, type HamurContext, type RouteRecord } from "hamur/kit"
2
+ import { resolveSchemaOrg } from "../schema/options.ts"
3
+ import type { SeoConfig } from "../types.ts"
4
+
5
+ const titlesOf = (routes: RouteRecord[]): Record<string, string> =>
6
+ Object.fromEntries(
7
+ routes.flatMap((route) => [
8
+ ...(route.title === undefined ? [] : [[route.name, route.title]]),
9
+ ...Object.entries(titlesOf(route.children)),
10
+ ]),
11
+ )
12
+
13
+ /** The `#hamur/seo/schema-org` template and the app plugin that writes the page's graph on both sides. */
14
+ export function setupSchemaOrg(value: SeoConfig["schemaOrg"], hamur: HamurContext): void {
15
+ const { enabled, identity } = resolveSchemaOrg(value)
16
+ const { baseURL, trailingSlash } = hamur.options.app
17
+ addTemplate({
18
+ filename: "seo/schema-org",
19
+ getContents: (ctx) =>
20
+ `export default ${JSON.stringify({ enabled, identity: identity ?? null, baseURL, trailingSlash, titles: titlesOf(ctx.routes) })};\n`,
21
+ })
22
+ addAppPlugin({ src: createResolver(import.meta.url).resolve("../runtime/schema-plugin.ts"), mode: "all" })
23
+ }