-
Notifications
You must be signed in to change notification settings - Fork 347
Expand file tree
/
Copy pathastro.config.mjs
More file actions
159 lines (154 loc) · 5.24 KB
/
astro.config.mjs
File metadata and controls
159 lines (154 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { defineConfig, fontProviders } from "astro/config";
import starlight from "@astrojs/starlight";
import sitemap from "@astrojs/sitemap";
import GithubSlugger from "github-slugger";
import { SITE_URL, BASE_PATH } from "./config/site.js";
const headingText = (node) =>
node.type === "text"
? node.value
: (node.children ?? []).map(headingText).join("");
// Base-prefixes root-absolute internal links (e.g. `/reference/core-keys/`)
// with the deploy base. Astro rewrites relative image refs but not cross-page
// links, so without this an authored `/reference/...` link would 404 on a
// based deploy. Authors write base-agnostic paths; the base lives only in
// config/site.ts. Mirrors the bundle rewriting in scripts/build-llms.ts.
function rehypeBaseInternalLinks() {
const prefix = (href) => {
if (typeof href !== "string") return href;
if (!href.startsWith("/") || href.startsWith("//")) return href;
if (href === BASE_PATH || href.startsWith(`${BASE_PATH}/`)) return href;
return `${BASE_PATH}${href}`;
};
const walk = (node) => {
if (
node.type === "element" &&
node.tagName === "a" &&
node.properties &&
typeof node.properties.href === "string"
) {
node.properties.href = prefix(node.properties.href);
}
for (const child of node.children ?? []) walk(child);
};
return (tree) => {
walk(tree);
};
}
/**
* Assign each content heading (h2–h6) a slug id and append a clickable
* anchor link so readers can copy a deep link to any section.
*
* This runs before Astro's heading-id collector, which defers to any id
* already set (see @astrojs/markdown-remark's rehype-collect-headings:
* `if (typeof node.properties.id !== "string")`). So the id we set here
* wins and is reused by the on-this-page TOC. We slug with `github-slugger`
* — the same library that collector uses, and the same one
* scripts/build-docs.ts uses for `{@link}` anchors — making it the single
* source of truth for every heading id. A fresh slugger per page, fed
* headings in document order, reproduces the `-1`/`-2` suffixes that
* disambiguate repeated headings.
*/
function rehypeHeadingAnchors() {
return (tree) => {
const slugger = new GithubSlugger();
const visit = (node) => {
if (node.type === "element" && /^h[2-6]$/.test(node.tagName)) {
const id = slugger.slug(headingText(node));
node.properties = node.properties ?? {};
node.properties.id = id;
// The visible `#` is rendered via CSS (`.heading-anchor::after`),
// not as a text node, so it does not leak into the heading text
// that Starlight extracts for the on-this-page TOC.
node.children.push({
type: "element",
tagName: "a",
properties: {
href: `#${id}`,
className: ["heading-anchor"],
"aria-label": "Link to this section",
},
children: [],
});
}
for (const child of node.children ?? []) visit(child);
};
visit(tree);
};
}
export default defineConfig({
site: SITE_URL,
base: BASE_PATH,
outDir: "./dist/site",
prefetch: { prefetchAll: true },
markdown: {
rehypePlugins: [rehypeBaseInternalLinks, rehypeHeadingAnchors],
},
fonts: [
{
name: "Inter",
cssVariable: "--font-inter",
provider: fontProviders.google(),
weights: [400, 500, 600, 700],
styles: ["normal"],
subsets: ["latin"],
},
{
name: "Inconsolata",
cssVariable: "--font-inconsolata",
provider: fontProviders.google(),
weights: [400, 700],
styles: ["normal"],
subsets: ["latin"],
},
],
integrations: [
starlight({
title: "@stellar/stellar-sdk",
components: {
PageTitle: "./src/starlightComponents/PageTitle.astro",
Head: "./src/starlightComponents/Head.astro",
},
customCss: ["./src/styles/overrides.css"],
social: [
{
icon: "github",
label: "GitHub",
href: "https://github.com/stellar/js-stellar-sdk",
},
],
// P17: per-page <link rel="alternate"> pointing at the LLMs
// sitemap so AI agents can discover the bundle from any rendered
// page. Hardcoded with the base prefix because Starlight emits
// `head` entries verbatim — Astro doesn't auto-prefix arbitrary
// attributes.
head: [
{
tag: "link",
attrs: {
rel: "alternate",
type: "text/plain",
title: "LLMs sitemap",
href: `${BASE_PATH}/llms.txt`,
},
},
],
sidebar: [
{
label: "Guides",
// The `docs/` prefix is required because Starlight's autogenerate
// filter strips a hardcoded `src/content/docs/` prefix from each
// route's `filePath` before matching `directory`. With our custom
// content collection rooted at `./docs/`, that strip is a no-op,
// so paths still start with `docs/` at filter time.
items: [{ autogenerate: { directory: "docs/guides" } }],
},
{
label: "Reference",
items: [{ autogenerate: { directory: "docs/reference" } }],
},
{ slug: "agents" },
],
}),
sitemap(),
],
});