Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,28 @@ The package's `defineConfig` defaults Markdown and MDX syntax highlighting to Sh

See [the basic site](./demos/basic-site) for a complete example.

## robots.txt

The theme provides a prerendered `/robots.txt` endpoint by default:

```text
User-agent: *
Allow: /

Sitemap: https://your-site.example.com/sitemap-index.xml
```

To customize crawl rules or sitemap locations, create `robots.txt` in Astro's
`publicDir` (`public/` by default). The theme then skips its endpoint and Astro
copies your file unchanged. Custom `publicDir` paths are supported.

For a site deployed under a base such as `/project/`, the generated Sitemap URL
is `https://your-site.example.com/project/sitemap-index.xml`, and Astro serves
the endpoint under `/project/robots.txt`. Crawlers only discover robots rules at
the origin root `/robots.txt`: the domain owner must add this Sitemap declaration
to that root file. A subpath deployment cannot configure other sites on the same
domain.

## Open Graph sharing metadata

The theme renders Open Graph tags directly in the HTML `<head>` for home,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@antv/astro-theme-antv",
"version": "0.1.0-beta.14",
"version": "0.1.0-beta.15",
"description": "Astro integration for static open-source documentation and examples",
"license": "MIT",
"repository": {
Expand Down
1 change: 1 addition & 0 deletions src/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export function antvSite(input: AntVSiteConfig): AstroIntegration {

const theme = await prepareThemeIntegration({
config,
publicDir: astroConfig.publicDir,
markdownProcessor: astroConfig.markdown.processor,
getBase: () => astroConfig.base,
});
Expand Down
14 changes: 13 additions & 1 deletion src/integration/theme.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { access } from "node:fs/promises";
import { existsSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import tailwindcss from "@tailwindcss/vite";
Expand Down Expand Up @@ -33,6 +34,7 @@ const themeRoutes = [

interface ThemeIntegrationOptions {
config: ResolvedSiteConfig;
publicDir: URL;
markdownProcessor: MarkdownProcessor;
getBase: () => string;
}
Expand Down Expand Up @@ -65,6 +67,7 @@ async function validateHomeSlots(config: ResolvedSiteConfig): Promise<void> {

export async function prepareThemeIntegration({
config,
publicDir,
markdownProcessor,
getBase,
}: ThemeIntegrationOptions): Promise<ThemeIntegrationContribution> {
Expand All @@ -79,13 +82,22 @@ export async function prepareThemeIntegration({
const sharedSources = Object.entries(config.demo.dependencies)
.filter(([name, source]) => name.startsWith("/") && source.startsWith("."))
.map(([, source]) => resolve(config.root, source));
const watchFiles = [config.content.docs, ...examplePaths, ...slotPaths, ...sharedSources,
const robotsFile = fileURLToPath(new URL("robots.txt", publicDir));
const watchFiles = [robotsFile, config.content.docs, ...examplePaths, ...slotPaths, ...sharedSources,
...Object.keys(registry.demoFiles.files).map((file) => resolve(config.root, file))];
const routes: InjectedRoute[] = themeRoutes.map(([pattern, entrypoint]) => ({
pattern,
entrypoint: pathToFileURL(resolve(themeRoot, entrypoint)),
prerender: true,
}));
// Consumer crawl policies take precedence over the theme's public-site default.
if (!existsSync(robotsFile)) {
routes.push({
pattern: "/robots.txt",
entrypoint: pathToFileURL(resolve(themeRoot, "pages/robots.txt.ts")),
prerender: true,
});
}
if (config.qa) {
routes.unshift({
pattern: `/[locale]/${config.qa.path}`,
Expand Down
6 changes: 5 additions & 1 deletion src/theme/pages/[locale]/[...route].astro
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const documentItems = page.type === 'document'
: [];
const renderedDocument = page.type === 'document' ? await render(page.entry) : undefined;
const DocumentContent = renderedDocument?.Content;
const hasToc = renderedDocument?.headings.some(({ depth }) => depth >= 2 && depth <= 3) ?? false;
const category = page.type === 'example-category'
? categoriesBySlug.get(page.categorySlug)
: undefined;
Expand Down Expand Up @@ -124,7 +125,10 @@ if (page.type === 'document') {
<a href={withBase('/llms.txt')} type="text/plain" class="sr-only focus:not-sr-only focus:block focus:mx-auto focus:max-w-4xl focus:px-6 focus:py-4 text-sm focus-visible:outline-2 focus-visible:outline-offset-4" data-pagefind-ignore>{isZh ? '资料索引' : 'Resource index'}</a>
</main>
) : (
<div class="grid grid-cols-[var(--docs-sidebar-width)_minmax(0,1fr)_var(--toc-width)] w-[var(--content-width)] mx-auto pt-6 pb-16 bg-[var(--surface-raised)] [@media(900px<width<=1320px)]:grid-cols-[240px_minmax(0,1fr)] [@media(width<=900px)]:block [@media(width<=900px)]:w-[min(100%_-_36px,820px)]">
<div class:list={[
'grid grid-cols-[var(--docs-sidebar-width)_minmax(0,1fr)] w-[var(--content-width)] mx-auto pt-6 pb-16 bg-[var(--surface-raised)] [@media(900px<width<=1320px)]:grid-cols-[240px_minmax(0,1fr)] [@media(width<=900px)]:block [@media(width<=900px)]:w-[min(100%_-_36px,820px)]',
hasToc && '[@media(width>1320px)]:grid-cols-[var(--docs-sidebar-width)_minmax(0,1fr)_var(--toc-width)]',
]}>
<DocsSidebar items={documentItems} currentRoute={page.route} locale={locale} labels={config.content.sidebar} />
<main id="main-content" data-pagefind-body class="min-w-0">
<article class="markdown-body">
Expand Down
9 changes: 9 additions & 0 deletions src/theme/pages/robots.txt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { APIRoute } from 'astro';
import { withBase } from '../lib/paths';

export const GET: APIRoute = ({ site }) => {
const sitemap = new URL(withBase('/sitemap-index.xml'), site);
return new Response(`User-agent: *\nAllow: /\n\nSitemap: ${sitemap.href}\n`, {
headers: { 'Content-Type': 'text/plain; charset=utf-8' },
});
};
Loading