@writedocs/generator 0.10.20 → 0.11.1
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/astro.config.mjs +32 -8
- package/package.json +3 -1
- package/src/cli/astro-output.js +44 -11
- package/src/cli/base-path-layout.js +44 -0
- package/src/cli/build.js +18 -9
- package/src/cli/check-built-styles.js +41 -33
- package/src/cli/dev.js +21 -4
- package/src/cli/run-pagefind.js +154 -148
- package/src/cli/write-mcp-files.js +103 -96
- package/src/cli/write-redirects-file.js +25 -8
- package/src/components/AppIcon.astro +51 -50
- package/src/components/Card.astro +193 -192
- package/src/components/CopyPageMenu.astro +374 -370
- package/src/components/Hint.astro +188 -187
- package/src/components/Icon.astro +72 -71
- package/src/components/Image.astro +3 -2
- package/src/components/Tile.astro +67 -66
- package/src/components/Video.astro +137 -136
- package/src/layout/BaseLayout.astro +21 -12
- package/src/layout/components/AnalyticsScripts.astro +6 -2
- package/src/layout/components/Breadcrumbs.astro +98 -97
- package/src/layout/components/ConsentBanner.astro +119 -118
- package/src/layout/components/MobileMenu.astro +229 -228
- package/src/layout/components/NavTree.astro +393 -392
- package/src/layout/components/Sidebar.astro +232 -231
- package/src/layout/components/SiteFooter.astro +87 -86
- package/src/layout/components/TopBar.astro +386 -385
- package/src/lib/agent-markdown.js +164 -162
- package/src/lib/asset-dimensions.js +89 -89
- package/src/lib/base-path.js +90 -0
- package/src/lib/config-schema.js +10 -0
- package/src/lib/config-schema.ts +17 -0
- package/src/lib/config.ts +1314 -1289
- package/src/lib/consent.js +108 -102
- package/src/lib/json-schema-descriptions.js +1 -0
- package/src/lib/link-check.js +7 -2
- package/src/lib/llms-index.ts +90 -88
- package/src/lib/mcp-dev-integration.js +65 -60
- package/src/lib/mcp-index.ts +102 -101
- package/src/lib/project-files-integration.js +2 -2
- package/src/lib/rehype-base-path.js +62 -0
- package/src/lib/robots-txt.js +21 -16
- package/src/lib/seo-tags.js +155 -140
- package/src/lib/site-base.ts +27 -0
- package/src/lib/styles-asset-integration.js +23 -10
- package/src/lib/wdignore-cases.json +195 -195
- package/src/lib/wdignore.js +177 -177
- package/src/pages/404.astro +63 -62
- package/src/pages/[...slug].astro +1604 -1597
- package/src/pages/[...slug].md.ts +93 -93
- package/src/pages/llms-full.txt.ts +88 -86
- package/src/scripts/search.ts +245 -237
- package/writedocs.schema.json +6 -0
package/src/lib/seo-tags.js
CHANGED
|
@@ -1,140 +1,155 @@
|
|
|
1
|
-
// What a page's <head> says to search engines and link previews: the
|
|
2
|
-
// <title>, the canonical link, and every <meta> tag - built here from the
|
|
3
|
-
// page's title and description, the site's and the page's `seo`
|
|
4
|
-
// (seoFieldsSchema, lib/config-schema.ts), and the site's address.
|
|
5
|
-
// BaseLayout.astro renders the result as it is.
|
|
6
|
-
//
|
|
7
|
-
// Plain JavaScript with no Astro or config.ts import, so scripts/
|
|
8
|
-
// seo-tags.test.js can run it directly, and astro.config.mjs can use the
|
|
9
|
-
// same rules for the sitemap before any page is rendered.
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
if (
|
|
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
|
-
set
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
set(
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
1
|
+
// What a page's <head> says to search engines and link previews: the
|
|
2
|
+
// <title>, the canonical link, and every <meta> tag - built here from the
|
|
3
|
+
// page's title and description, the site's and the page's `seo`
|
|
4
|
+
// (seoFieldsSchema, lib/config-schema.ts), and the site's address.
|
|
5
|
+
// BaseLayout.astro renders the result as it is.
|
|
6
|
+
//
|
|
7
|
+
// Plain JavaScript with no Astro or config.ts import, so scripts/
|
|
8
|
+
// seo-tags.test.js can run it directly, and astro.config.mjs can use the
|
|
9
|
+
// same rules for the sitemap before any page is rendered.
|
|
10
|
+
//
|
|
11
|
+
// `siteUrl` is the site's URL - {domain}{basePath}, see resolveSiteUrl() in
|
|
12
|
+
// lib/config.ts - and a page's `path` is its address in the site, without
|
|
13
|
+
// the base ("/guides/a/").
|
|
14
|
+
import { withBase } from './base-path.js';
|
|
15
|
+
|
|
16
|
+
/** Meta tags written as `<meta property>` (Open Graph and its relatives);
|
|
17
|
+
* every other one is `<meta name>`. */
|
|
18
|
+
const PROPERTY_PREFIXES = ['og:', 'article:', 'book:', 'profile:', 'music:', 'video:', 'fb:'];
|
|
19
|
+
|
|
20
|
+
/** Whether a `robots` value, or the older `noindex` flag, keeps a page out
|
|
21
|
+
* of search engines. `robots` wins when both are set. */
|
|
22
|
+
export function isNoindex(seo) {
|
|
23
|
+
if (!seo) return false;
|
|
24
|
+
if (typeof seo.robots === 'string') return /\bnoindex\b|\bnone\b/i.test(seo.robots);
|
|
25
|
+
return seo.noindex === true;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** A page's frontmatter as gray-matter read it - before the schema folds
|
|
29
|
+
* Mintlify's top-level `noindex`/`robots` into `seo`, and marks a `hidden`
|
|
30
|
+
* page noindex. Used where pages aren't loaded yet (astro.config.mjs). */
|
|
31
|
+
export function frontmatterNoindex(data) {
|
|
32
|
+
const robots = data?.seo?.robots ?? data?.robots;
|
|
33
|
+
if (typeof robots === 'string') return isNoindex({ robots });
|
|
34
|
+
return (data?.seo?.noindex ?? data?.noindex ?? data?.hidden === true) === true;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** `value` - a full URL or a site address - as a full URL; without a
|
|
38
|
+
* siteUrl, a site address with the site's `base` in front. */
|
|
39
|
+
function absoluteUrl(siteUrl, value, base = '') {
|
|
40
|
+
if (/^https?:\/\//i.test(value)) return value;
|
|
41
|
+
if (!siteUrl) return withBase(base, value);
|
|
42
|
+
return `${siteUrl}${value.startsWith('/') ? '' : '/'}${value}`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** A page's canonical URL: its own address on the site's domain, or
|
|
46
|
+
* `canonical` - a full URL, or a path on the same domain. Null when there's
|
|
47
|
+
* no full URL to give (no `domain`, and `canonical` is a path). */
|
|
48
|
+
export function canonicalUrl(siteUrl, pagePath, canonical) {
|
|
49
|
+
if (canonical) {
|
|
50
|
+
if (/^https?:\/\//i.test(canonical)) return canonical;
|
|
51
|
+
return siteUrl ? absoluteUrl(siteUrl, canonical) : null;
|
|
52
|
+
}
|
|
53
|
+
return siteUrl ? `${siteUrl}${pagePath}` : null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const samePath = (a, b) => a.replace(/^\/+|\/+$/g, '').toLowerCase() === b.replace(/^\/+|\/+$/g, '').toLowerCase();
|
|
57
|
+
|
|
58
|
+
/** Whether `canonical` names another address than the page's own - such a
|
|
59
|
+
* page is left out of sitemap.xml, which lists canonical addresses only. */
|
|
60
|
+
export function canonicalIsElsewhere(siteUrl, pagePath, canonical) {
|
|
61
|
+
if (!canonical) return false;
|
|
62
|
+
if (/^https?:\/\//i.test(canonical)) {
|
|
63
|
+
if (!siteUrl) return true;
|
|
64
|
+
let url;
|
|
65
|
+
try {
|
|
66
|
+
url = new URL(canonical);
|
|
67
|
+
} catch {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
const site = new URL(siteUrl);
|
|
71
|
+
if (url.origin.toLowerCase() !== site.origin.toLowerCase()) return true;
|
|
72
|
+
// The canonical's path, from the site's root - outside the basePath, it's
|
|
73
|
+
// another site's address.
|
|
74
|
+
const root = site.pathname.replace(/\/+$/, '').toLowerCase();
|
|
75
|
+
const pathname = url.pathname.toLowerCase();
|
|
76
|
+
if (root && pathname !== root && !pathname.startsWith(`${root}/`)) return true;
|
|
77
|
+
return !samePath(pathname.slice(root.length), pagePath);
|
|
78
|
+
}
|
|
79
|
+
return !samePath(canonical, pagePath);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The `<link rel="alternate" hreflang>` set for a page: one per language
|
|
83
|
+
* it exists in (its own included - each page of the set has to list all
|
|
84
|
+
* of them, itself too), then `x-default`, the first language, for a
|
|
85
|
+
* reader whose language is none of them. `alternates` is
|
|
86
|
+
* `[{ lang, href }]` with site paths. Empty without a `domain` (the
|
|
87
|
+
* links must be full URLs) or with fewer than two languages. */
|
|
88
|
+
export function hreflangLinks(siteUrl, alternates = []) {
|
|
89
|
+
if (!siteUrl || alternates.length < 2) return [];
|
|
90
|
+
const links = alternates.map(({ lang, href }) => ({ hreflang: lang, href: absoluteUrl(siteUrl, href) }));
|
|
91
|
+
return [...links, { hreflang: 'x-default', href: links[0].href }];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** The <title>: `seo.title` exactly as written, or "Page title · Site name"
|
|
95
|
+
* - the site name once when the page's title is the site's name. */
|
|
96
|
+
export function documentTitle(title, siteName, seoTitle) {
|
|
97
|
+
if (seoTitle && seoTitle.trim()) return seoTitle.trim();
|
|
98
|
+
if (!siteName || title.trim().toLowerCase() === siteName.trim().toLowerCase()) return title;
|
|
99
|
+
return `${title} · ${siteName}`;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Everything BaseLayout.astro writes for search engines and link previews.
|
|
103
|
+
* `seo` is the site's `seo` with the page's own merged over it (mergeSeo()).
|
|
104
|
+
* Returns `{ title, canonical, metas }`, each meta as
|
|
105
|
+
* `{ attr: 'name' | 'property', key, content }`, in the order they're
|
|
106
|
+
* written. An entry of `seo.metatags` replaces the tag of the same name,
|
|
107
|
+
* or is added after the others. `base` is the site's basePath ("/docs"),
|
|
108
|
+
* for a picture's address when there's no siteUrl to make it full. */
|
|
109
|
+
export function seoHead({ title, siteName, description, siteDescription, seo = {}, siteUrl = null, path = '/', base = '' }) {
|
|
110
|
+
const shareTitle = seo.title?.trim() || title;
|
|
111
|
+
const metaDescription = seo.description?.trim() || description || siteDescription;
|
|
112
|
+
const canonical = canonicalUrl(siteUrl, path, seo.canonical);
|
|
113
|
+
const custom = seo.metatags ?? {};
|
|
114
|
+
// The picture in a link preview: the page's or the site's `ogImage` -
|
|
115
|
+
// or, set by [...slug].astro when there is neither, the card generated
|
|
116
|
+
// for the page (lib/og-image.js).
|
|
117
|
+
const image = seo.ogImage ? absoluteUrl(siteUrl, seo.ogImage, base) : undefined;
|
|
118
|
+
// The large card needs a picture - that one, or a tag set by hand.
|
|
119
|
+
const hasImage = Boolean(image || custom['og:image'] || custom['twitter:image']);
|
|
120
|
+
const robots = typeof seo.robots === 'string' && seo.robots.trim() ? seo.robots.trim() : seo.noindex ? 'noindex, nofollow' : undefined;
|
|
121
|
+
|
|
122
|
+
const tags = new Map();
|
|
123
|
+
const set = (key, content) => {
|
|
124
|
+
if (content === undefined || content === null || content === '') return;
|
|
125
|
+
tags.set(key, String(content));
|
|
126
|
+
};
|
|
127
|
+
set('description', metaDescription);
|
|
128
|
+
if (seo.keywords?.length) set('keywords', seo.keywords.join(', '));
|
|
129
|
+
set('robots', robots);
|
|
130
|
+
set('og:title', shareTitle);
|
|
131
|
+
set('og:description', metaDescription);
|
|
132
|
+
set('og:type', seo.ogType ?? 'website');
|
|
133
|
+
set('og:site_name', siteName);
|
|
134
|
+
set('og:url', canonical);
|
|
135
|
+
set('og:image', image);
|
|
136
|
+
set('twitter:card', seo.twitterCard ?? (hasImage ? 'summary_large_image' : 'summary'));
|
|
137
|
+
set('twitter:title', shareTitle);
|
|
138
|
+
set('twitter:description', metaDescription);
|
|
139
|
+
set('twitter:image', image);
|
|
140
|
+
for (const [key, value] of Object.entries(custom)) {
|
|
141
|
+
if (typeof value !== 'string' || !value.trim()) continue;
|
|
142
|
+
// A picture's address has to be a full URL for the sites that read it.
|
|
143
|
+
set(key, /(^|:)image$/.test(key) ? absoluteUrl(siteUrl, value, base) : value);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return {
|
|
147
|
+
title: documentTitle(title, siteName, seo.title),
|
|
148
|
+
canonical,
|
|
149
|
+
metas: [...tags].map(([key, content]) => ({
|
|
150
|
+
attr: PROPERTY_PREFIXES.some((prefix) => key.startsWith(prefix)) ? 'property' : 'name',
|
|
151
|
+
key,
|
|
152
|
+
content,
|
|
153
|
+
})),
|
|
154
|
+
};
|
|
155
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// The site's base (writedocs.json `basePath`) for components and page
|
|
2
|
+
// scripts - lib/base-path.js explains the base and which addresses take it.
|
|
3
|
+
// __WD_BASE__ is "" or "/docs", set by astro.config.mjs (vite.define), so
|
|
4
|
+
// the same import works in a component's frontmatter and in a <script>; with
|
|
5
|
+
// no base, siteHref() returns its argument and the built pages don't change.
|
|
6
|
+
//
|
|
7
|
+
// The rule: logic keeps site addresses as they are ("/guides/a/"); the base
|
|
8
|
+
// goes in where the address is written - an href, a src, a url() - with
|
|
9
|
+
// siteHref(). Doing it once there is what keeps an address from getting it
|
|
10
|
+
// twice.
|
|
11
|
+
import { withBase, srcsetWithBase } from './base-path.js';
|
|
12
|
+
|
|
13
|
+
declare const __WD_BASE__: string;
|
|
14
|
+
|
|
15
|
+
/** "" or "/docs". */
|
|
16
|
+
export const BASE: string = __WD_BASE__;
|
|
17
|
+
|
|
18
|
+
/** A site address with the base in front ("/a/" -> "/docs/a/"); anything
|
|
19
|
+
* else - a full URL, "#x", a relative path - as it is. */
|
|
20
|
+
export function siteHref<T extends string | undefined | null>(url: T): T {
|
|
21
|
+
return withBase(BASE, url) as T;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** A `srcset` with the base in front of each site address. */
|
|
25
|
+
export function siteSrcset<T extends string | undefined | null>(srcset: T): T {
|
|
26
|
+
return srcsetWithBase(BASE, srcset) as T;
|
|
27
|
+
}
|
|
@@ -63,6 +63,7 @@
|
|
|
63
63
|
import fs from 'node:fs';
|
|
64
64
|
import path from 'node:path';
|
|
65
65
|
import { fileURLToPath } from 'node:url';
|
|
66
|
+
import { withoutBase } from './base-path.js';
|
|
66
67
|
import { loadDocsConfig, collectConfiguredAssetPaths } from './config.ts';
|
|
67
68
|
import { resolveOutsidePublic } from './asset-path.js';
|
|
68
69
|
import { PUBLISHED_TYPES, isPublishedFile, readSpecPaths } from './project-files.js';
|
|
@@ -173,7 +174,7 @@ function listFilesWithExtension(dir, extension) {
|
|
|
173
174
|
* writedocs.json field, there's no fixed set of field names to read a
|
|
174
175
|
* content image's path from ahead of time, so instead this reads
|
|
175
176
|
* whatever `src` a page's own rendered markup ends up containing. */
|
|
176
|
-
function copyReferencedContentAssets(outDir, contentDir) {
|
|
177
|
+
function copyReferencedContentAssets(outDir, contentDir, basePath) {
|
|
177
178
|
const attrPattern = /\b(?:src|srcset)="([^"]+)"/g;
|
|
178
179
|
for (const htmlFile of listHtmlFiles(outDir)) {
|
|
179
180
|
const html = fs.readFileSync(htmlFile, 'utf8');
|
|
@@ -182,7 +183,7 @@ function copyReferencedContentAssets(outDir, contentDir) {
|
|
|
182
183
|
// srcset can hold a comma-separated list of "url descriptor" pairs -
|
|
183
184
|
// split it out; a plain src is already just the one URL.
|
|
184
185
|
const candidates = match[1].split(',').map((part) => part.trim().split(/\s+/)[0]).filter(Boolean);
|
|
185
|
-
for (const urlPath of candidates) copyAssetIfMissing(urlPath, outDir, contentDir);
|
|
186
|
+
for (const urlPath of candidates) copyAssetIfMissing(urlPath, outDir, contentDir, basePath);
|
|
186
187
|
}
|
|
187
188
|
// Referências em CSS dentro do próprio HTML: um bloco <style> escrito
|
|
188
189
|
// na página (o `<style>` de uma página `mode: custom`, por exemplo) ou
|
|
@@ -190,13 +191,13 @@ function copyReferencedContentAssets(outDir, contentDir) {
|
|
|
190
191
|
// background-image nunca era copiada para o build e dava 404 em
|
|
191
192
|
// produção, embora funcionasse no dev (onde o middleware resolve
|
|
192
193
|
// qualquer caminho direto do disco).
|
|
193
|
-
for (const urlPath of cssUrlReferences(html)) copyAssetIfMissing(urlPath, outDir, contentDir);
|
|
194
|
+
for (const urlPath of cssUrlReferences(html)) copyAssetIfMissing(urlPath, outDir, contentDir, basePath);
|
|
194
195
|
}
|
|
195
196
|
// O mesmo para as folhas de estilo que o build gera (_astro/*.css): é
|
|
196
197
|
// para onde vai o .css solto na raiz do projeto, carregado automaticamente.
|
|
197
198
|
for (const cssFile of listFilesWithExtension(outDir, '.css')) {
|
|
198
199
|
for (const urlPath of cssUrlReferences(fs.readFileSync(cssFile, 'utf8'))) {
|
|
199
|
-
copyAssetIfMissing(urlPath, outDir, contentDir);
|
|
200
|
+
copyAssetIfMissing(urlPath, outDir, contentDir, basePath);
|
|
200
201
|
}
|
|
201
202
|
}
|
|
202
203
|
}
|
|
@@ -213,9 +214,14 @@ function cssUrlReferences(text) {
|
|
|
213
214
|
}
|
|
214
215
|
|
|
215
216
|
/** Copies one root-relative asset reference into the build output from
|
|
216
|
-
* wherever it lives in the project, unless it's already there.
|
|
217
|
-
|
|
218
|
-
|
|
217
|
+
* wherever it lives in the project, unless it's already there. A built
|
|
218
|
+
* page's addresses carry the site's `basePath` ("/docs/images/a.png" -
|
|
219
|
+
* lib/base-path.js); the file is the one at the address under it, and an
|
|
220
|
+
* address outside the base isn't the site's. */
|
|
221
|
+
function copyAssetIfMissing(address, outDir, contentDir, basePath = '') {
|
|
222
|
+
if (!address.startsWith('/') || address.startsWith('//')) return; // only root-relative - not external, not protocol-relative
|
|
223
|
+
const urlPath = withoutBase(basePath, address);
|
|
224
|
+
if (urlPath === null) return;
|
|
219
225
|
if (!looksLikeAssetPath(urlPath)) return;
|
|
220
226
|
const segments = urlPath.replace(/^\/+/, '').split('/');
|
|
221
227
|
const outPath = path.join(outDir, ...segments);
|
|
@@ -227,14 +233,21 @@ function copyAssetIfMissing(urlPath, outDir, contentDir) {
|
|
|
227
233
|
fs.copyFileSync(resolved, outPath);
|
|
228
234
|
}
|
|
229
235
|
|
|
230
|
-
|
|
236
|
+
// `basePath`: writedocs.json's, "" or "/docs" (lib/base-path.js). A request
|
|
237
|
+
// outside the base isn't the site's. Astro's dev server takes the base off
|
|
238
|
+
// req.url before this middleware runs, so /docs/images/a.png and a bare
|
|
239
|
+
// /images/a.png would look the same there - the path asked for is
|
|
240
|
+
// req.originalUrl.
|
|
241
|
+
export function stylesAssetFallback(contentDir, { basePath = '' } = {}) {
|
|
231
242
|
return {
|
|
232
243
|
name: 'writedocs-styles-asset-fallback',
|
|
233
244
|
hooks: {
|
|
234
245
|
'astro:server:setup': ({ server }) => {
|
|
235
246
|
server.middlewares.use((req, res, next) => {
|
|
236
247
|
if (!req.url) return next();
|
|
237
|
-
const [
|
|
248
|
+
const [requestPath, query = ''] = (req.originalUrl ?? req.url).split('?');
|
|
249
|
+
const urlPath = withoutBase(basePath, requestPath);
|
|
250
|
+
if (urlPath === null) return next();
|
|
238
251
|
if (!query && isIgnoredPublicRequest(urlPath, contentDir)) {
|
|
239
252
|
res.statusCode = 404;
|
|
240
253
|
res.end('Not found');
|
|
@@ -271,7 +284,7 @@ export function stylesAssetFallback(contentDir) {
|
|
|
271
284
|
// Pass 2: arbitrary content-body assets - anything a page's own
|
|
272
285
|
// rendered HTML references by src/srcset that pass 1 didn't
|
|
273
286
|
// already account for.
|
|
274
|
-
copyReferencedContentAssets(outDir, contentDir);
|
|
287
|
+
copyReferencedContentAssets(outDir, contentDir, basePath);
|
|
275
288
|
},
|
|
276
289
|
},
|
|
277
290
|
};
|