This commit is contained in:
Adam Matthiesen 2024-03-22 03:50:02 -07:00 committed by GitHub
parent 0be102e2d0
commit ec38182f14
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 77 additions and 33 deletions

View File

@ -0,0 +1,5 @@
---
"@matthiesenxyz/astro-gists": patch
---
improve internal functions and error handling

View File

@ -30,7 +30,10 @@
}, },
"sideEffects": false, "sideEffects": false,
"exports": { "exports": {
".": "./src/index.ts", ".": {
"types": "./src/index.d.ts",
"import": "./src/index.ts"
},
"./components": "./src/components/index.ts", "./components": "./src/components/index.ts",
"./GetGist": "./src/components/GetGist.astro", "./GetGist": "./src/components/GetGist.astro",
"./GetGistGroup": "./src/components/GetGistGroup.astro" "./GetGistGroup": "./src/components/GetGistGroup.astro"

View File

@ -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<BundledShikiTheme>().optional(),
/**
* Optional: Allows the user to enable verbose logging.
*/
verbose: z.boolean().optional().default(false),
}).optional().default({ verbose: false });
export type astroGistsUserConfig = z.infer<typeof optionsSchema>

View File

@ -1,6 +1,6 @@
import { defineIntegration, createResolver } from "astro-integration-kit" import { defineIntegration, createResolver } from "astro-integration-kit"
import { corePlugins } from "astro-integration-kit/plugins" import { corePlugins } from "astro-integration-kit/plugins"
import type { astroGistsUserConfig } from "./index" import type { astroGistsUserConfig } from "./UserConfigSchema"
import { readFileSync } from "node:fs"; import { readFileSync } from "node:fs";
import type { AstroIntegrationLogger } from "astro"; import type { AstroIntegrationLogger } from "astro";
import { loadEnv } from "vite"; import { loadEnv } from "vite";

View File

@ -25,14 +25,19 @@ const WRAP = wrap ? wrap : wrap === undefined ? true : false;
const SLN = showLineNumbers ? showLineNumbers : showLineNumbers === undefined ? true : false; const SLN = showLineNumbers ? showLineNumbers : showLineNumbers === undefined ? true : false;
// Fetching the Gist // Fetching the Gist
const Gist = await getGistFile( gistId, filename); const Gist = await getGistFile( gistId, filename );
--- ---
{ Gist && { Gist ? (
<Code <Code
wrap={WRAP} showLineNumbers={SLN} wrap={WRAP} showLineNumbers={SLN}
title={Gist.filename} raw_url={Gist.raw_url} title={Gist.filename} raw_url={Gist.raw_url}
code={ Gist.content ? Gist.content : "" } code={ Gist.content ? Gist.content : "" }
lang={Gist.language ? Gist.language.toLowerCase() : undefined } lang={Gist.language ? Gist.language.toLowerCase() : undefined }
/> /> ) : (
<div>
<h1>Sorry, the Gist with ID: {gistId} was not found</h1>
</div>
)
} }

View File

@ -25,13 +25,41 @@ const SLN = showLineNumbers ? showLineNumbers : showLineNumbers === undefined ?
// get the Gist // get the Gist
const Gist = await getGistGroup(gistId); const Gist = await getGistGroup(gistId);
// extract the files // Define the GistResponse type
const files = Gist.files; 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 && (
<div>
<h1>Sorry, the Gist with ID: {gistId} was not found</h1>
</div>
) }
{ !noGist && (
<div> <div>
{Object.keys(files).map((file) => { {Object.keys(files).map((file) => {
const { content, filename, language, raw_url } = files[file]; const { content, filename, language, raw_url } = files[file] as GistFile;
return ( return (
<Code <Code
title={filename} wrap={WRAP} title={filename} wrap={WRAP}

4
package/src/index.d.ts vendored Normal file
View File

@ -0,0 +1,4 @@
import type { AstroIntegration } from "astro";
import type { astroGistsUserConfig} from "./UserConfigSchema";
export default function astroGists(options?: astroGistsUserConfig): AstroIntegration;

View File

@ -1,23 +1,3 @@
// Export Integration // Export Integration
import astroGist from "./integration"; import astroGists from "./astro-gists";
export default astroGist; export default astroGists;
// 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<BundledShikiTheme>().optional(),
/**
* Optional: Allows the user to enable verbose logging.
*/
verbose: z.boolean().optional().default(false),
}).optional().default({ verbose: false });
export type astroGistsUserConfig = z.infer<typeof optionsSchema>

View File

@ -1,9 +1,9 @@
import { defineConfig } from "astro/config"; import { defineConfig } from "astro/config";
import astroGist from "@matthiesenxyz/astro-gists"; import astroGists from "@matthiesenxyz/astro-gists";
import mdx from "@astrojs/mdx" import mdx from "@astrojs/mdx"
// https://astro.build/config // https://astro.build/config
export default defineConfig({ export default defineConfig({
integrations: [astroGist(), mdx()] integrations: [astroGists(), mdx()]
}); });