91 lines
4.2 KiB
TypeScript
91 lines
4.2 KiB
TypeScript
/**
|
|
* Built-in look of /sitemap.xml in a browser — served by the sitemap route itself
|
|
* (`/sitemap.xml?style`), so projects ship NO sitemap.css and copy nothing.
|
|
* Plain CSS on XML (W3C "Associating Style Sheets with XML", type="text/css") —
|
|
* not XSLT, which browsers are removing. Crawlers ignore it.
|
|
*/
|
|
|
|
export type SitemapStyleOptions = {
|
|
/** Accent colour (card hover). Default '#027bd0'. Any plain CSS colour. */
|
|
accent?: string
|
|
/**
|
|
* Line under the header — the technology provider. Default
|
|
* "Technology by Intecion.com". `false` hides it.
|
|
*/
|
|
credit?: false | string
|
|
/** Header line. Default "XML Sitemap". */
|
|
title?: string
|
|
}
|
|
|
|
/** Query parameter on the sitemap URL that returns the stylesheet instead of XML. */
|
|
export const SITEMAP_STYLE_PARAM = 'style'
|
|
export const DEFAULT_SITEMAP_CREDIT = 'Technology by Intecion.com'
|
|
|
|
/** CSS `content: "…"` value — escapes backslashes, quotes and newlines. */
|
|
const cssString = (s: string) => `"${s.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/[\r\n]+/g, ' ')}"`
|
|
|
|
/** Only plain colour values — the option ends up inside a stylesheet. */
|
|
const safeColor = (c: string | undefined, fallback: string) =>
|
|
c && /^(?:#[0-9a-f]{3,8}|[a-z]+|(?:rgb|hsl|oklch)a?\([\d\s.,%/-]+\))$/i.test(c.trim()) ? c.trim() : fallback
|
|
|
|
export function buildSitemapCss({ accent, credit, title }: SitemapStyleOptions = {}): string {
|
|
const heading = title?.trim() || 'XML Sitemap'
|
|
const note = credit === false ? '' : (credit ?? DEFAULT_SITEMAP_CREDIT).trim()
|
|
const acc = safeColor(accent, '#027bd0')
|
|
const divider = 'padding-bottom: 1.25rem; margin-bottom: 1.5rem; border-bottom: 1px solid var(--border);'
|
|
|
|
return `:root {
|
|
--bg: #fafafa; --card: #ffffff; --border: #e5e7eb; --border-hover: #d1d5db;
|
|
--text-main: #111827; --text-secondary: #4b5563; --text-muted: #9ca3af; --url-color: #1e293b;
|
|
--badge-bg: #f3f4f6; --badge-border: #e5e7eb; --badge-text: #4b5563; --accent: ${acc};
|
|
}
|
|
@media (prefers-color-scheme: dark) {
|
|
:root {
|
|
--bg: #090a0f; --card: #12131a; --border: #1e202e; --border-hover: #2e3247;
|
|
--text-main: #f9fafb; --text-secondary: #9ca3af; --text-muted: #6b7280; --url-color: #f3f4f6;
|
|
--badge-bg: #1a1c26; --badge-border: #282b3d; --badge-text: #9ca3af;
|
|
}
|
|
}
|
|
urlset {
|
|
display: flex; flex-direction: column; box-sizing: border-box; min-height: 100vh;
|
|
max-width: 1040px; margin: 0 auto; padding: 3rem 1.5rem; line-height: 1.5;
|
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, Helvetica, Arial, sans-serif;
|
|
background-color: var(--bg); color: var(--text-main);
|
|
}
|
|
urlset::before {
|
|
content: ${cssString(heading)};
|
|
display: block; order: -2; font-size: 1.35rem; font-weight: 600; letter-spacing: -0.02em;
|
|
color: var(--text-main); ${note ? 'padding-bottom: 0.4rem;' : divider}
|
|
}
|
|
${note ? `urlset::after {
|
|
content: ${cssString(note)};
|
|
display: block; order: -1; font-size: 0.8rem; color: var(--text-muted); ${divider}
|
|
}
|
|
` : ''}url {
|
|
display: block; width: 100%; order: 0; box-sizing: border-box;
|
|
background-color: var(--card); border: 1px solid var(--border); border-radius: 8px;
|
|
padding: 1rem 1.25rem; margin-bottom: 0.65rem;
|
|
transition: border-color 0.15s ease, box-shadow 0.15s ease;
|
|
}
|
|
url:hover { border-color: var(--accent); box-shadow: 0 2px 6px rgba(0, 0, 0, 0.03); }
|
|
loc {
|
|
display: block; margin-bottom: 0.45rem; word-break: break-all; line-height: 1.45;
|
|
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
|
|
font-size: 0.875rem; font-weight: 500; color: var(--url-color);
|
|
}
|
|
lastmod, changefreq, priority {
|
|
display: inline-block; font-size: 0.775rem; color: var(--text-secondary);
|
|
margin-right: 1.25rem; margin-top: 0.15rem;
|
|
}
|
|
lastmod::before { content: "Updated: "; color: var(--text-muted); }
|
|
changefreq::before { content: "Frequency: "; color: var(--text-muted); }
|
|
priority::before { content: "Priority: "; color: var(--text-muted); }
|
|
link {
|
|
display: inline-block; font-size: 0.7rem; font-weight: 600; text-transform: uppercase;
|
|
background-color: var(--badge-bg); border: 1px solid var(--badge-border); color: var(--badge-text);
|
|
padding: 0.1rem 0.45rem; border-radius: 4px; margin-right: 0.3rem; margin-top: 0.35rem;
|
|
}
|
|
link::before { content: attr(hreflang); }
|
|
`
|
|
}
|