@rimelight/seo 0.0.1 → 0.0.3

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/src/sitemap.ts DELETED
@@ -1,166 +0,0 @@
1
- import type { LocaleConfig, SeoEntry, SitemapUrl } from "./types.js"
2
-
3
- /**
4
- * Build fully-resolved sitemap URLs from entries and locale config
5
- */
6
- export function buildSitemapUrls(entries: SeoEntry[], config: LocaleConfig): SitemapUrl[] {
7
- const urls: SitemapUrl[] = []
8
- const { site, locales, defaultLocale, prefixDefaultLocale, trailingSlash } = config
9
-
10
- for (const entry of entries) {
11
- const entryLocales =
12
- entry.locales && entry.locales.length > 0 ? entry.locales : Object.keys(locales)
13
-
14
- const entryUrls: SitemapUrl[] = []
15
-
16
- for (const locale of entryLocales) {
17
- const isDefault = locale === defaultLocale
18
- const localePrefix = !isDefault || prefixDefaultLocale ? `/${locale}` : ""
19
-
20
- let path = entry.path
21
- if (trailingSlash === "always" && !path.endsWith("/")) {
22
- path += "/"
23
- } else if (trailingSlash === "never" && path.endsWith("/")) {
24
- path = path.replace(/\/$/, "")
25
- }
26
-
27
- const loc = `${site}${localePrefix}${path}`
28
-
29
- const item: SitemapUrl = { loc }
30
- if (entry.lastmod) {
31
- item.lastmod = entry.lastmod.toISOString()
32
- }
33
- if (entry.changefreq) {
34
- item.changefreq = entry.changefreq
35
- }
36
- if (entry.priority !== undefined) {
37
- item.priority = entry.priority
38
- }
39
- entryUrls.push(item)
40
- }
41
-
42
- // Add alternates if multiple locales
43
- if (entryUrls.length > 1) {
44
- const defaultUrl = entryUrls.find(
45
- (u) => u.loc === `${site}${prefixDefaultLocale ? `/${defaultLocale}` : ""}${entry.path}`
46
- )
47
-
48
- for (const url of entryUrls) {
49
- url.alternates = entryUrls
50
- .filter((u) => u.loc !== url.loc)
51
- .map((u) => ({
52
- hreflang: extractHreflang(u.loc, locales, defaultLocale),
53
- href: u.loc
54
- }))
55
-
56
- // Add x-default pointing to default locale
57
- // x-default should be on the default URL itself, pointing to itself
58
- if (defaultUrl && url.loc === defaultUrl.loc) {
59
- url.alternates.push({
60
- hreflang: "x-default",
61
- href: defaultUrl.loc
62
- })
63
- }
64
- }
65
- }
66
-
67
- urls.push(...entryUrls)
68
- }
69
-
70
- return urls
71
- }
72
-
73
- /**
74
- * Build sitemap XML from resolved URLs
75
- */
76
- export function buildSitemapXml(urls: SitemapUrl[]): string {
77
- const urlElements = urls
78
- .map((url) => {
79
- let xml = ` <url>\n <loc>${escapeXml(url.loc)}</loc>`
80
-
81
- if (url.lastmod) {
82
- xml += `\n <lastmod>${url.lastmod}</lastmod>`
83
- }
84
-
85
- if (url.changefreq) {
86
- xml += `\n <changefreq>${url.changefreq}</changefreq>`
87
- }
88
-
89
- if (url.priority !== undefined) {
90
- xml += `\n <priority>${url.priority.toFixed(1)}</priority>`
91
- }
92
-
93
- if (url.alternates && url.alternates.length > 0) {
94
- for (const alt of url.alternates) {
95
- xml += `\n <xhtml:link rel="alternate" hreflang="${alt.hreflang}" href="${escapeXml(alt.href)}" />`
96
- }
97
- }
98
-
99
- xml += "\n </url>"
100
- return xml
101
- })
102
- .join("\n")
103
-
104
- return `<?xml version="1.0" encoding="UTF-8"?>
105
- <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
106
- ${urlElements}
107
- </urlset>`
108
- }
109
-
110
- /**
111
- * Build sitemap index XML from sitemap URLs
112
- */
113
- export function buildSitemapIndexXml(sitemapUrls: string[]): string {
114
- const sitemapElements = sitemapUrls
115
- .map(
116
- (url) => ` <sitemap>
117
- <loc>${escapeXml(url)}</loc>
118
- </sitemap>`
119
- )
120
- .join("\n")
121
-
122
- return `<?xml version="1.0" encoding="UTF-8"?>
123
- <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
124
- ${sitemapElements}
125
- </sitemapindex>`
126
- }
127
-
128
- /**
129
- * Chunk sitemap URLs into multiple sitemaps (max 50k URLs per sitemap)
130
- */
131
- export function chunkSitemapUrls(urls: SitemapUrl[], limit: number = 45000): SitemapUrl[][] {
132
- const chunks: SitemapUrl[][] = []
133
- for (let i = 0; i < urls.length; i += limit) {
134
- chunks.push(urls.slice(i, i + limit))
135
- }
136
- return chunks
137
- }
138
-
139
- /**
140
- * Extract hreflang from a URL based on locale config
141
- */
142
- function extractHreflang(
143
- url: string,
144
- locales: Record<string, string>,
145
- defaultLocale: string
146
- ): string {
147
- for (const [segment, hreflang] of Object.entries(locales)) {
148
- if (url.includes(`/${segment}/`) || url.endsWith(`/${segment}`)) {
149
- return hreflang
150
- }
151
- }
152
- // Default to the first locale's hreflang if no match
153
- return locales[defaultLocale] || "en"
154
- }
155
-
156
- /**
157
- * Escape XML special characters
158
- */
159
- function escapeXml(str: string): string {
160
- return str
161
- .replace(/&/g, "&amp;")
162
- .replace(/</g, "&lt;")
163
- .replace(/>/g, "&gt;")
164
- .replace(/"/g, "&quot;")
165
- .replace(/'/g, "&apos;")
166
- }
package/src/types.ts DELETED
@@ -1,219 +0,0 @@
1
- /**
2
- * Change frequency for sitemap entries
3
- */
4
- export type ChangeFreq = "always" | "hourly" | "daily" | "weekly" | "monthly" | "yearly" | "never"
5
-
6
- /**
7
- * SEO entry — a single page to include in sitemaps
8
- */
9
- export interface SeoEntry {
10
- /**
11
- * Locale-less path, e.g. "/company/blog/hello". Leading slash required.
12
- */
13
- path: string
14
- lastmod?: Date
15
- changefreq?: ChangeFreq
16
- priority?: number
17
- /**
18
- * Restrict to a subset of locales; defaults to all configured locales.
19
- */
20
- locales?: readonly string[]
21
- }
22
-
23
- /**
24
- * Locale configuration for SEO builders
25
- */
26
- export interface LocaleConfig {
27
- /**
28
- * Absolute origin, e.g. "https://rimelight.com"
29
- */
30
- site: string
31
- /**
32
- * Map of locale segment -> hreflang, e.g. { en: "en-US", pt: "pt-BR" }
33
- */
34
- locales: Record<string, string>
35
- defaultLocale: string
36
- /**
37
- * Emit the default locale without a prefix. Default: false.
38
- */
39
- prefixDefaultLocale?: boolean
40
- trailingSlash?: "always" | "never"
41
- }
42
-
43
- /**
44
- * Fully resolved sitemap URL with alternates
45
- */
46
- export interface SitemapUrl {
47
- loc: string
48
- lastmod?: string
49
- changefreq?: ChangeFreq
50
- priority?: number
51
- alternates?: SitemapAlternate[]
52
- }
53
-
54
- /**
55
- * Language alternate for a sitemap URL
56
- */
57
- export interface SitemapAlternate {
58
- hreflang: string
59
- href: string
60
- }
61
-
62
- /**
63
- * Options for robots.txt generation
64
- */
65
- export interface RobotsOptions {
66
- /**
67
- * The sitemap URL to reference
68
- */
69
- sitemapUrl: string
70
- /**
71
- * Paths to disallow for all user agents
72
- */
73
- disallow?: string[]
74
- /**
75
- * Paths to allow for all user agents
76
- */
77
- allow?: string[]
78
- /**
79
- * User-agent specific rules
80
- */
81
- userAgentRules?: UserAgentRule[]
82
- }
83
-
84
- /**
85
- * User-agent specific robots.txt rule
86
- */
87
- export interface UserAgentRule {
88
- userAgent: string
89
- disallow?: string[]
90
- allow?: string[]
91
- }
92
-
93
- /**
94
- * Site identity and SEO defaults shared across all Rimelight Astro sites. The UI-specific overrides
95
- * (component themes, shortcuts, etc.) live in UIConfig (packages/ui) and are passed directly to the
96
- * ui() integration.
97
- */
98
- export interface SiteConfig {
99
- id: string
100
- name: string
101
- description: string
102
- url: string
103
- ogImage: string
104
- author: string
105
- email: string
106
- branding: {
107
- logo: {
108
- alt: string
109
- }
110
- favicon: {
111
- svg: string
112
- }
113
- colors: {
114
- themeColor: string
115
- backgroundColor: string
116
- }
117
- }
118
- seo: {
119
- titleTemplate: string
120
- ogImageFallback: string
121
- maxDescriptionLength: number
122
- authorHandle?: string
123
- }
124
- }
125
-
126
- /**
127
- * Open Graph image with alt text
128
- */
129
- export interface OGImage {
130
- /**
131
- * Image URL (relative or absolute)
132
- */
133
- src: string
134
- /**
135
- * Alt text for the image
136
- */
137
- alt: string
138
- }
139
-
140
- /**
141
- * RSS feed link configuration
142
- */
143
- export interface RSSFeed {
144
- /**
145
- * RSS feed URL
146
- */
147
- href: string
148
- /**
149
- * Feed title (defaults to "RSS")
150
- */
151
- title?: string
152
- }
153
-
154
- /**
155
- * Props for the RLAHead Astro component. Defined here so consumers can construct head props without
156
- * importing @rimelight/ui.
157
- */
158
- export interface HeadProps {
159
- /**
160
- * Page title
161
- */
162
- title?: string
163
- /**
164
- * Page description
165
- */
166
- description?: string
167
- /**
168
- * Open Graph image with alt text
169
- */
170
- ogImage?: OGImage
171
- /**
172
- * Open Graph type (e.g., "website", "article")
173
- */
174
- ogType?: string
175
- /**
176
- * Open Graph locale (defaults to "en_US")
177
- */
178
- ogLocale?: string
179
- /**
180
- * Whether to prevent indexing
181
- */
182
- noindex?: boolean
183
- /**
184
- * Whether this is a 404 page
185
- */
186
- is404?: boolean
187
- /**
188
- * Robots metadata (overrides noindex/is404 logic if provided)
189
- */
190
- robots?: string
191
- /**
192
- * Canonical URL
193
- */
194
- canonical?: string
195
- /**
196
- * Language alternates for i18n
197
- */
198
- languageAlternates?: Array<{ hreflang: string; href: string }>
199
- /**
200
- * Site configuration for defaults
201
- */
202
- config?: SiteConfig
203
- /**
204
- * Whether to enable View Transitions (ClientRouter)
205
- */
206
- transitions?: boolean
207
- /**
208
- * Meta charset
209
- */
210
- charset?: string
211
- /**
212
- * Meta viewport
213
- */
214
- viewport?: string
215
- /**
216
- * RSS feed configuration
217
- */
218
- rssFeed?: RSSFeed
219
- }