Skip to content

Commit 50dd048

Browse files
DaniFoldiwebpro
andauthoredOct 17, 2023
Add astro plugin (#298)
* Add astro plugin * Add astro fixture and test * Narrow entry file pattern * Implement suggestions from review Co-Authored-By: Lars Kappert <lars@webpro.nl> * Remove unnecessary file references, fix astro:content Co-Authored-By: Lars Kappert <lars@webpro.nl> * Actually untrack unnecessary files * Ignore (only) the generated `.astro/types.d.ts` file * Add dummy astro package + binary * Wrap up astro plugin --------- Co-authored-by: Lars Kappert <lars@webpro.nl>
1 parent 4d6dea8 commit 50dd048

33 files changed

+1123
-1
lines changed
 

‎README.md

+2
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ This is especially useful over time when such configuration files change (and th
251251
Knip contains a growing list of plugins:
252252

253253
- [Angular][plugin-angular]
254+
- [astro][plugin-astro]
254255
- [Ava][plugin-ava]
255256
- [Babel][plugin-babel]
256257
- [Capacitor][plugin-capacitor]
@@ -898,6 +899,7 @@ Special thanks to the wonderful people who have contributed to this project:
898899
[72]: https://github.com/webpro/knip/graphs/contributors
899900
[73]: https://contrib.rocks/image?repo=webpro/knip
900901
[plugin-angular]: ./src/plugins/angular
902+
[plugin-astro]: ./src/plugins/astro
901903
[plugin-ava]: ./src/plugins/ava
902904
[plugin-babel]: ./src/plugins/babel
903905
[plugin-capacitor]: ./src/plugins/capacitor
+213
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
declare module 'astro:content' {
2+
interface Render {
3+
'.mdx': Promise<{
4+
Content: import('astro').MarkdownInstance<{}>['Content'];
5+
headings: import('astro').MarkdownHeading[];
6+
remarkPluginFrontmatter: Record<string, any>;
7+
}>;
8+
}
9+
}
10+
11+
declare module 'astro:content' {
12+
interface Render {
13+
'.md': Promise<{
14+
Content: import('astro').MarkdownInstance<{}>['Content'];
15+
headings: import('astro').MarkdownHeading[];
16+
remarkPluginFrontmatter: Record<string, any>;
17+
}>;
18+
}
19+
}
20+
21+
declare module 'astro:content' {
22+
export { z } from 'astro/zod';
23+
24+
type Flatten<T> = T extends { [K: string]: infer U } ? U : never;
25+
26+
export type CollectionKey = keyof AnyEntryMap;
27+
export type CollectionEntry<C extends CollectionKey> = Flatten<AnyEntryMap[C]>;
28+
29+
export type ContentCollectionKey = keyof ContentEntryMap;
30+
export type DataCollectionKey = keyof DataEntryMap;
31+
32+
// This needs to be in sync with ImageMetadata
33+
export type ImageFunction = () => import('astro/zod').ZodObject<{
34+
src: import('astro/zod').ZodString;
35+
width: import('astro/zod').ZodNumber;
36+
height: import('astro/zod').ZodNumber;
37+
format: import('astro/zod').ZodUnion<
38+
[
39+
import('astro/zod').ZodLiteral<'png'>,
40+
import('astro/zod').ZodLiteral<'jpg'>,
41+
import('astro/zod').ZodLiteral<'jpeg'>,
42+
import('astro/zod').ZodLiteral<'tiff'>,
43+
import('astro/zod').ZodLiteral<'webp'>,
44+
import('astro/zod').ZodLiteral<'gif'>,
45+
import('astro/zod').ZodLiteral<'svg'>,
46+
import('astro/zod').ZodLiteral<'avif'>,
47+
]
48+
>;
49+
}>;
50+
51+
type BaseSchemaWithoutEffects =
52+
| import('astro/zod').AnyZodObject
53+
| import('astro/zod').ZodUnion<[BaseSchemaWithoutEffects, ...BaseSchemaWithoutEffects[]]>
54+
| import('astro/zod').ZodDiscriminatedUnion<string, import('astro/zod').AnyZodObject[]>
55+
| import('astro/zod').ZodIntersection<BaseSchemaWithoutEffects, BaseSchemaWithoutEffects>;
56+
57+
type BaseSchema =
58+
| BaseSchemaWithoutEffects
59+
| import('astro/zod').ZodEffects<BaseSchemaWithoutEffects>;
60+
61+
export type SchemaContext = { image: ImageFunction };
62+
63+
type DataCollectionConfig<S extends BaseSchema> = {
64+
type: 'data';
65+
schema?: S | ((context: SchemaContext) => S);
66+
};
67+
68+
type ContentCollectionConfig<S extends BaseSchema> = {
69+
type?: 'content';
70+
schema?: S | ((context: SchemaContext) => S);
71+
};
72+
73+
type CollectionConfig<S> = ContentCollectionConfig<S> | DataCollectionConfig<S>;
74+
75+
export function defineCollection<S extends BaseSchema>(
76+
input: CollectionConfig<S>
77+
): CollectionConfig<S>;
78+
79+
type AllValuesOf<T> = T extends any ? T[keyof T] : never;
80+
type ValidContentEntrySlug<C extends keyof ContentEntryMap> = AllValuesOf<
81+
ContentEntryMap[C]
82+
>['slug'];
83+
84+
export function getEntryBySlug<
85+
C extends keyof ContentEntryMap,
86+
E extends ValidContentEntrySlug<C> | (string & {}),
87+
>(
88+
collection: C,
89+
// Note that this has to accept a regular string too, for SSR
90+
entrySlug: E
91+
): E extends ValidContentEntrySlug<C>
92+
? Promise<CollectionEntry<C>>
93+
: Promise<CollectionEntry<C> | undefined>;
94+
95+
export function getDataEntryById<C extends keyof DataEntryMap, E extends keyof DataEntryMap[C]>(
96+
collection: C,
97+
entryId: E
98+
): Promise<CollectionEntry<C>>;
99+
100+
export function getCollection<C extends keyof AnyEntryMap, E extends CollectionEntry<C>>(
101+
collection: C,
102+
filter?: (entry: CollectionEntry<C>) => entry is E
103+
): Promise<E[]>;
104+
export function getCollection<C extends keyof AnyEntryMap>(
105+
collection: C,
106+
filter?: (entry: CollectionEntry<C>) => unknown
107+
): Promise<CollectionEntry<C>[]>;
108+
109+
export function getEntry<
110+
C extends keyof ContentEntryMap,
111+
E extends ValidContentEntrySlug<C> | (string & {}),
112+
>(entry: {
113+
collection: C;
114+
slug: E;
115+
}): E extends ValidContentEntrySlug<C>
116+
? Promise<CollectionEntry<C>>
117+
: Promise<CollectionEntry<C> | undefined>;
118+
export function getEntry<
119+
C extends keyof DataEntryMap,
120+
E extends keyof DataEntryMap[C] | (string & {}),
121+
>(entry: {
122+
collection: C;
123+
id: E;
124+
}): E extends keyof DataEntryMap[C]
125+
? Promise<DataEntryMap[C][E]>
126+
: Promise<CollectionEntry<C> | undefined>;
127+
export function getEntry<
128+
C extends keyof ContentEntryMap,
129+
E extends ValidContentEntrySlug<C> | (string & {}),
130+
>(
131+
collection: C,
132+
slug: E
133+
): E extends ValidContentEntrySlug<C>
134+
? Promise<CollectionEntry<C>>
135+
: Promise<CollectionEntry<C> | undefined>;
136+
export function getEntry<
137+
C extends keyof DataEntryMap,
138+
E extends keyof DataEntryMap[C] | (string & {}),
139+
>(
140+
collection: C,
141+
id: E
142+
): E extends keyof DataEntryMap[C]
143+
? Promise<DataEntryMap[C][E]>
144+
: Promise<CollectionEntry<C> | undefined>;
145+
146+
/** Resolve an array of entry references from the same collection */
147+
export function getEntries<C extends keyof ContentEntryMap>(
148+
entries: {
149+
collection: C;
150+
slug: ValidContentEntrySlug<C>;
151+
}[]
152+
): Promise<CollectionEntry<C>[]>;
153+
export function getEntries<C extends keyof DataEntryMap>(
154+
entries: {
155+
collection: C;
156+
id: keyof DataEntryMap[C];
157+
}[]
158+
): Promise<CollectionEntry<C>[]>;
159+
160+
export function reference<C extends keyof AnyEntryMap>(
161+
collection: C
162+
): import('astro/zod').ZodEffects<
163+
import('astro/zod').ZodString,
164+
C extends keyof ContentEntryMap
165+
? {
166+
collection: C;
167+
slug: ValidContentEntrySlug<C>;
168+
}
169+
: {
170+
collection: C;
171+
id: keyof DataEntryMap[C];
172+
}
173+
>;
174+
// Allow generic `string` to avoid excessive type errors in the config
175+
// if `dev` is not running to update as you edit.
176+
// Invalid collection names will be caught at build time.
177+
export function reference<C extends string>(
178+
collection: C
179+
): import('astro/zod').ZodEffects<import('astro/zod').ZodString, never>;
180+
181+
type ReturnTypeOrOriginal<T> = T extends (...args: any[]) => infer R ? R : T;
182+
type InferEntrySchema<C extends keyof AnyEntryMap> = import('astro/zod').infer<
183+
ReturnTypeOrOriginal<Required<ContentConfig['collections'][C]>['schema']>
184+
>;
185+
186+
type ContentEntryMap = {
187+
"blog": {
188+
"first-post.md": {
189+
id: "first-post.md";
190+
slug: "first-post";
191+
body: string;
192+
collection: "blog";
193+
data: InferEntrySchema<"blog">
194+
} & { render(): Render[".md"] };
195+
"using-mdx.mdx": {
196+
id: "using-mdx.mdx";
197+
slug: "using-mdx";
198+
body: string;
199+
collection: "blog";
200+
data: InferEntrySchema<"blog">
201+
} & { render(): Render[".mdx"] };
202+
};
203+
204+
};
205+
206+
type DataEntryMap = {
207+
208+
};
209+
210+
type AnyEntryMap = ContentEntryMap & DataEntryMap;
211+
212+
type ContentConfig = typeof import("../src/content/config");
213+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { defineConfig } from 'astro/config';
2+
import mdx from '@astrojs/mdx';
3+
4+
import sitemap from '@astrojs/sitemap';
5+
6+
// https://astro.build/config
7+
export default defineConfig({
8+
site: 'https://example.com',
9+
integrations: [mdx(), sitemap()],
10+
});

‎fixtures/plugins/astro/knip.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default {
2+
ignore: '.astro/types.d.ts',
3+
compilers: {
4+
astro: (text: string) => [...text.matchAll(/import[^;]+/g)].join('\n'),
5+
css: (text: string) => [...text.matchAll(/(?<=@)import[^;]+/g)].join('\n'),
6+
mdx: (text: string) => [...text.matchAll(/import[^;]+/g)].join('\n'),
7+
},
8+
};

‎fixtures/plugins/astro/node_modules/astro/index.js

Whitespace-only changes.

‎fixtures/plugins/astro/node_modules/astro/package.json

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎fixtures/plugins/astro/package.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "@fixtures/astro",
3+
"type": "module",
4+
"version": "*",
5+
"scripts": {
6+
"dev": "astro dev",
7+
"start": "astro dev",
8+
"build": "astro build",
9+
"preview": "astro preview",
10+
"astro": "astro"
11+
},
12+
"dependencies": {
13+
"@astrojs/mdx": "*",
14+
"@astrojs/rss": "*",
15+
"@astrojs/sitemap": "*",
16+
"astro": "*"
17+
}
18+
}
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
// Import the global.css file here so that it is included on
3+
// all pages through the use of the <BaseHead /> component.
4+
import '../styles/global.css';
5+
6+
interface Props {
7+
title: string;
8+
description: string;
9+
image?: string;
10+
}
11+
12+
const canonicalURL = new URL(Astro.url.pathname, Astro.site);
13+
14+
const { title, description, image = '/favicon.svg' } = Astro.props;
15+
---
16+
17+
<!-- Global Metadata -->
18+
<meta charset="utf-8" />
19+
<meta name="viewport" content="width=device-width,initial-scale=1" />
20+
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
21+
<meta name="generator" content={Astro.generator} />
22+
23+
<!-- Canonical URL -->
24+
<link rel="canonical" href={canonicalURL} />
25+
26+
<!-- Primary Meta Tags -->
27+
<title>{title}</title>
28+
<meta name="title" content={title} />
29+
<meta name="description" content={description} />
30+
31+
<!-- Open Graph / Facebook -->
32+
<meta property="og:type" content="website" />
33+
<meta property="og:url" content={Astro.url} />
34+
<meta property="og:title" content={title} />
35+
<meta property="og:description" content={description} />
36+
<meta property="og:image" content={new URL(image, Astro.url)} />
37+
38+
<!-- Twitter -->
39+
<meta property="twitter:card" content="summary_large_image" />
40+
<meta property="twitter:url" content={Astro.url} />
41+
<meta property="twitter:title" content={title} />
42+
<meta property="twitter:description" content={description} />
43+
<meta property="twitter:image" content={new URL(image, Astro.url)} />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
const today = new Date();
3+
---
4+
5+
<footer>
6+
&copy; {today.getFullYear()} Your name here. All rights reserved.
7+
<div class="social-links">
8+
<a href="https://m.webtoo.ls/@astro" target="_blank">
9+
<span class="sr-only">Follow Astro on Mastodon</span>
10+
<svg
11+
viewBox="0 0 16 16"
12+
aria-hidden="true"
13+
width="32"
14+
height="32"
15+
astro-icon="social/mastodon"
16+
><path
17+
fill="currentColor"
18+
d="M11.19 12.195c2.016-.24 3.77-1.475 3.99-2.603.348-1.778.32-4.339.32-4.339 0-3.47-2.286-4.488-2.286-4.488C12.062.238 10.083.017 8.027 0h-.05C5.92.017 3.942.238 2.79.765c0 0-2.285 1.017-2.285 4.488l-.002.662c-.004.64-.007 1.35.011 2.091.083 3.394.626 6.74 3.78 7.57 1.454.383 2.703.463 3.709.408 1.823-.1 2.847-.647 2.847-.647l-.06-1.317s-1.303.41-2.767.36c-1.45-.05-2.98-.156-3.215-1.928a3.614 3.614 0 0 1-.033-.496s1.424.346 3.228.428c1.103.05 2.137-.064 3.188-.189zm1.613-2.47H11.13v-4.08c0-.859-.364-1.295-1.091-1.295-.804 0-1.207.517-1.207 1.541v2.233H7.168V5.89c0-1.024-.403-1.541-1.207-1.541-.727 0-1.091.436-1.091 1.296v4.079H3.197V5.522c0-.859.22-1.541.66-2.046.456-.505 1.052-.764 1.793-.764.856 0 1.504.328 1.933.983L8 4.39l.417-.695c.429-.655 1.077-.983 1.934-.983.74 0 1.336.259 1.791.764.442.505.661 1.187.661 2.046v4.203z"
19+
></path></svg
20+
>
21+
</a>
22+
<a href="https://twitter.com/astrodotbuild" target="_blank">
23+
<span class="sr-only">Follow Astro on Twitter</span>
24+
<svg viewBox="0 0 16 16" aria-hidden="true" width="32" height="32" astro-icon="social/twitter"
25+
><path
26+
fill="currentColor"
27+
d="M5.026 15c6.038 0 9.341-5.003 9.341-9.334 0-.14 0-.282-.006-.422A6.685 6.685 0 0 0 16 3.542a6.658 6.658 0 0 1-1.889.518 3.301 3.301 0 0 0 1.447-1.817 6.533 6.533 0 0 1-2.087.793A3.286 3.286 0 0 0 7.875 6.03a9.325 9.325 0 0 1-6.767-3.429 3.289 3.289 0 0 0 1.018 4.382A3.323 3.323 0 0 1 .64 6.575v.045a3.288 3.288 0 0 0 2.632 3.218 3.203 3.203 0 0 1-.865.115 3.23 3.23 0 0 1-.614-.057 3.283 3.283 0 0 0 3.067 2.277A6.588 6.588 0 0 1 .78 13.58a6.32 6.32 0 0 1-.78-.045A9.344 9.344 0 0 0 5.026 15z"
28+
></path></svg
29+
>
30+
</a>
31+
<a href="https://github.com/withastro/astro" target="_blank">
32+
<span class="sr-only">Go to Astro's GitHub repo</span>
33+
<svg viewBox="0 0 16 16" aria-hidden="true" width="32" height="32" astro-icon="social/github"
34+
><path
35+
fill="currentColor"
36+
d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z"
37+
></path></svg
38+
>
39+
</a>
40+
</div>
41+
</footer>
42+
<style>
43+
footer {
44+
padding: 2em 1em 6em 1em;
45+
background: linear-gradient(var(--gray-gradient)) no-repeat;
46+
color: rgb(var(--gray));
47+
text-align: center;
48+
}
49+
.social-links {
50+
display: flex;
51+
justify-content: center;
52+
gap: 1em;
53+
margin-top: 1em;
54+
}
55+
.social-links a {
56+
text-decoration: none;
57+
color: rgb(var(--gray));
58+
}
59+
.social-links a:hover {
60+
color: rgb(var(--gray-dark));
61+
}
62+
</style>

0 commit comments

Comments
 (0)
Please sign in to comment.