From 22a790a4c63d73d556cf339350332ebfb98f91ab Mon Sep 17 00:00:00 2001 From: Adam Matthiesen Date: Fri, 22 Mar 2024 03:47:41 -0700 Subject: [PATCH] Update package.json and component files --- package/package.json | 5 ++- package/src/UserConfigSchema.ts | 19 ++++++++++ .../src/{integration.ts => astro-gists.ts} | 2 +- package/src/components/GetGist.astro | 11 ++++-- package/src/components/GetGistGroup.astro | 36 ++++++++++++++++--- package/src/index.d.ts | 4 +++ package/src/index.ts | 24 ++----------- playground/astro.config.mjs | 4 +-- 8 files changed, 72 insertions(+), 33 deletions(-) create mode 100644 package/src/UserConfigSchema.ts rename package/src/{integration.ts => astro-gists.ts} (97%) create mode 100644 package/src/index.d.ts diff --git a/package/package.json b/package/package.json index 585ab54..cbc25a5 100644 --- a/package/package.json +++ b/package/package.json @@ -30,7 +30,10 @@ }, "sideEffects": false, "exports": { - ".": "./src/index.ts", + ".": { + "types": "./src/index.d.ts", + "import": "./src/index.ts" + }, "./components": "./src/components/index.ts", "./GetGist": "./src/components/GetGist.astro", "./GetGistGroup": "./src/components/GetGistGroup.astro" diff --git a/package/src/UserConfigSchema.ts b/package/src/UserConfigSchema.ts new file mode 100644 index 0000000..113099e --- /dev/null +++ b/package/src/UserConfigSchema.ts @@ -0,0 +1,19 @@ +// Export the user config schema +import { z } from "astro/zod"; +import type { BundledShikiTheme } from "expressive-code"; + +export const optionsSchema = z.object({ + /** + * Optional: Allows the user to change the default theme for the code blocks. + * @example ['github-dark'] + * + * All available themes are listed in the [Shiki documentation](https://shiki.matsu.io/docs/themes). + */ + theme: z.custom().optional(), + /** + * Optional: Allows the user to enable verbose logging. + */ + verbose: z.boolean().optional().default(false), + }).optional().default({ verbose: false }); + +export type astroGistsUserConfig = z.infer \ No newline at end of file diff --git a/package/src/integration.ts b/package/src/astro-gists.ts similarity index 97% rename from package/src/integration.ts rename to package/src/astro-gists.ts index 0b1638e..f6f2a69 100644 --- a/package/src/integration.ts +++ b/package/src/astro-gists.ts @@ -1,6 +1,6 @@ import { defineIntegration, createResolver } from "astro-integration-kit" import { corePlugins } from "astro-integration-kit/plugins" -import type { astroGistsUserConfig } from "./index" +import type { astroGistsUserConfig } from "./UserConfigSchema" import { readFileSync } from "node:fs"; import type { AstroIntegrationLogger } from "astro"; import { loadEnv } from "vite"; diff --git a/package/src/components/GetGist.astro b/package/src/components/GetGist.astro index aeb6f9f..ce73876 100644 --- a/package/src/components/GetGist.astro +++ b/package/src/components/GetGist.astro @@ -25,14 +25,19 @@ const WRAP = wrap ? wrap : wrap === undefined ? true : false; const SLN = showLineNumbers ? showLineNumbers : showLineNumbers === undefined ? true : false; // Fetching the Gist -const Gist = await getGistFile( gistId, filename); +const Gist = await getGistFile( gistId, filename ); --- -{ Gist && +{ Gist ? ( + /> ) : ( +
+

Sorry, the Gist with ID: {gistId} was not found

+
+ ) + } diff --git a/package/src/components/GetGistGroup.astro b/package/src/components/GetGistGroup.astro index c36e6b4..f743e95 100644 --- a/package/src/components/GetGistGroup.astro +++ b/package/src/components/GetGistGroup.astro @@ -25,13 +25,41 @@ const SLN = showLineNumbers ? showLineNumbers : showLineNumbers === undefined ? // get the Gist const Gist = await getGistGroup(gistId); -// extract the files -const files = Gist.files; +// Define the GistResponse type +type GistFile = { + filename: string; + type: string; + language: string; + raw_url: string; + size: number; + content: string; +}; + +type GistFiles = { + [key: string]: GistFile +}; + +let files = {} as GistFiles; + +let noGist: boolean + +if (Gist) { + noGist = false; + files = Gist.files as GistFiles; +} else { + noGist = true; +} + --- -{ Gist && ( +{ noGist && ( +
+

Sorry, the Gist with ID: {gistId} was not found

+
+) } +{ !noGist && (
{Object.keys(files).map((file) => { - const { content, filename, language, raw_url } = files[file]; + const { content, filename, language, raw_url } = files[file] as GistFile; return ( ().optional(), - /** - * Optional: Allows the user to enable verbose logging. - */ - verbose: z.boolean().optional().default(false), - }).optional().default({ verbose: false }); - -export type astroGistsUserConfig = z.infer \ No newline at end of file +import astroGists from "./astro-gists"; +export default astroGists; \ No newline at end of file diff --git a/playground/astro.config.mjs b/playground/astro.config.mjs index aa66aa3..f1c34d0 100644 --- a/playground/astro.config.mjs +++ b/playground/astro.config.mjs @@ -1,9 +1,9 @@ import { defineConfig } from "astro/config"; -import astroGist from "@matthiesenxyz/astro-gists"; +import astroGists from "@matthiesenxyz/astro-gists"; import mdx from "@astrojs/mdx" // https://astro.build/config export default defineConfig({ - integrations: [astroGist(), mdx()] + integrations: [astroGists(), mdx()] });