diff --git a/.changeset/lucky-candles-melt.md b/.changeset/lucky-candles-melt.md new file mode 100644 index 0000000..ef5f324 --- /dev/null +++ b/.changeset/lucky-candles-melt.md @@ -0,0 +1,5 @@ +--- +"@matthiesenxyz/astro-gists": patch +--- + +Added new Component to get entire collections of gists (See Readme for more info) diff --git a/package/README.md b/package/README.md index d58bb7b..7fd23d5 100644 --- a/package/README.md +++ b/package/README.md @@ -73,9 +73,9 @@ GITHUB_PERSONAL_TOKEN=ghp_YOURPERSONALTOKENHERE ### Usage -#### `` +#### `` Shows a SINGLE gist from a GistCollection -This Utility is meant to display Gists as Codeblocks using ExpressiveCode for Astro instead of Scripted Elements using the default Gist method +This Utility is meant to display a single Gist as Codeblocks using ExpressiveCode for Astro instead of Scripted Elements using the default Gist method by calling the ID and Filename ```astro --- @@ -87,6 +87,18 @@ import { GetGist } from "@matthiesenxyz/astro-gists/components" /> ``` +#### `` Shows all of the Gists from a GistCollection + +This Utility is meant to display an entire collection of Gists by ID + +```astro +import { GetGistGroup } from "@matthiesenxyz/astro-gists/components" + + +``` + ## Contributing This package is structured as a monorepo: diff --git a/package/src/components/GetGistGroup.astro b/package/src/components/GetGistGroup.astro new file mode 100644 index 0000000..1c9fe03 --- /dev/null +++ b/package/src/components/GetGistGroup.astro @@ -0,0 +1,52 @@ +--- +import { getGistGroup } from "../utils" +import { GetGist } from "./index" + +export interface Props { + /** REQUIRED: Used to define the desired GitHub Gist ID */ + gistId: string; + /** OPTIONAL: Allows the user to Enable and Disable LineNumbers + * @default true + */ + showLineNumbers?: boolean; + /** OPTIONAL: Allows the user to Enable and Disable LineWrapping + * @default true + */ + wrap?: boolean; +} + +const { gistId, showLineNumbers, wrap } = Astro.props as Props; + +const SHOWLINENUMBERS = showLineNumbers ? showLineNumbers : showLineNumbers == undefined ? true : false; +const WRAP = wrap ? wrap : wrap == undefined ? true : false; + +const Gist = await getGistGroup(gistId); +const files = Gist?.files; + +const filed = Object.keys(files as object); + + + +--- +{ Gist && ( +
+ { filed.map((file) => ( +
+ +
+ ))} +
+) } + + + \ No newline at end of file diff --git a/package/src/components/index.ts b/package/src/components/index.ts index 1e4414c..888b025 100644 --- a/package/src/components/index.ts +++ b/package/src/components/index.ts @@ -1 +1,2 @@ -export { default as GetGist} from "./GetGist.astro" \ No newline at end of file +export { default as GetGist} from "./GetGist.astro" +export { default as GetGistGroup} from "./GetGistGroup.astro" \ No newline at end of file diff --git a/package/src/integration.ts b/package/src/integration.ts index 4998389..71c3e4a 100644 --- a/package/src/integration.ts +++ b/package/src/integration.ts @@ -2,26 +2,50 @@ import { defineIntegration, createResolver } from "astro-integration-kit" import { corePlugins } from "astro-integration-kit/plugins" import { astroGistsExpressiveCode } from "./integrations/expressive-code" - +/** Astro-Gist - Astro Integration + * - There is currently no configuration for this integration. Just add it to your astro Integration list. + * @example + * import astroGist from "@matthiesenxyz/astro-gists"; + * export default defineConfig({ + * integrations: [astroGist()] + * }); +*/ export default defineIntegration({ name: "@matthiesenxyz/astro-gists", plugins: [...corePlugins], setup() { const { resolve } = createResolver(import.meta.url); - return { - "astro:config:setup": ({ watchIntegration, config, updateConfig, logger }) => { - watchIntegration(resolve()) + return { + "astro:config:setup": ({ + watchIntegration, + config, + updateConfig, + logger + }) => { + // WATCH INTEGRATION FOR CHANGES + watchIntegration(resolve()) // IMPORT INTEGRATIONS & INTEGRATION ROUTES const integrations = [...config.integrations]; + // ADD ASTRO-EXPRESSIVE-CODE INTEGRATION if (!integrations.find(({ name }) => name === "astro-expressive-code")) { logger.info("Adding astro-expressive-code integration") updateConfig({ integrations: [...integrations, ...astroGistsExpressiveCode()] }) } + + // UPDATE ASTRO-EXPRESSIVE-CODE INTEGRATION + try { + updateConfig({ + integrations: [...astroGistsExpressiveCode()] + }) + } catch (e) { + logger.error(e as string); + throw e; + } } } } diff --git a/package/src/utils.ts b/package/src/utils.ts index 0f6eab4..a9ebaa0 100644 --- a/package/src/utils.ts +++ b/package/src/utils.ts @@ -1,15 +1,19 @@ import { Octokit } from "octokit"; import { loadEnv } from "vite"; +// Load environment variables const { GITHUB_PERSONAL_TOKEN } = loadEnv("all", process.cwd(), "GITHUB_"); +// Create an Octokit instance export const octokit = new Octokit({ auth: GITHUB_PERSONAL_TOKEN }); +// Get a Gist by ID export const getGist = async (gistId: string) => { const { data } = await octokit.request('GET /gists/{gist_id}', { gist_id: gistId }); return data; }; +// Get a file from a Gist by ID and filename export const getGistFile = async ( gistId: string, filename: string @@ -22,3 +26,12 @@ export const getGistFile = async ( return null; }; +export const getGistGroup = async ( + gistId: string + ) => { +const gist = await getGist(gistId); +if (gist?.files) { + return gist ? gist : null; +} +return null; +}; \ No newline at end of file diff --git a/playground/astro.config.mjs b/playground/astro.config.mjs index ac8781b..e334110 100644 --- a/playground/astro.config.mjs +++ b/playground/astro.config.mjs @@ -1,7 +1,6 @@ import { defineConfig } from "astro/config"; import astroGist from "@matthiesenxyz/astro-gists"; - // https://astro.build/config export default defineConfig({ integrations: [astroGist()] diff --git a/playground/src/pages/index.astro b/playground/src/pages/index.astro index 1fedc46..945769e 100644 --- a/playground/src/pages/index.astro +++ b/playground/src/pages/index.astro @@ -1,9 +1,13 @@ --- -import { GetGist } from "@matthiesenxyz/astro-gists/components" +import { GetGist, GetGistGroup } from "@matthiesenxyz/astro-gists/components" ---

Dev: Playground

+ + \ No newline at end of file