well shit... its working

This commit is contained in:
Adam Matthiesen 2024-02-24 01:47:57 -08:00
parent d2d14cfc28
commit 198898928f
19 changed files with 1431 additions and 1210 deletions

View File

@ -6,7 +6,7 @@
"node": ">=18.19.0"
},
"scripts": {
"dev": "pnpm --filter playground dev",
"dev": "pnpm --filter playground run dev",
"changeset": "changeset",
"lint": "biome check .",
"lint:fix": "biome check --apply ."

View File

@ -6,7 +6,9 @@ This is an [Astro integration](https://docs.astro.build/en/guides/integrations-g
### Prerequisites
TODO:
The Only Requirement to install this package is a **Github account with a Varified Email** to be able to create a Personal Access Token.
This Integration uses [`Octokit`](http://octokit.github.io/) by `GitHub` to Generate custom gists using [`ExpressiveCode`](https://expressive-code.com/) within your Astro project!
### Installation
@ -43,18 +45,41 @@ yarn add @matthiesenxyz/astro-gists
2. Add the integration to your astro config
```diff
+import integration from "@matthiesenxyz/astro-gists";
import { defineConfig } from "astro/config";
+import astroGist from "@matthiesenxyz/astro-gists";
// https://astro.build/config
export default defineConfig({
integrations: [
+ integration(),
],
+ integrations: [astroGist()]
});
```
### Configuration
TODO:configuration
Setup your `.env` file with the following secret from github:
Github Personal Access Token (Classic)
- No Special Permissions required
- Octokit recommends creating an empty token for this!
```
GITHUB_PERSONAL_TOKEN=ghp_YOURPERSONALTOKENHERE
```
### Usage
#### `<GetGist>`
This Utility is meant to display Gists as Codeblocks using ExpressiveCode for Astro instead of Scripted Elements using the default Gist method
---
import { GetGist } from "@matthiesenxyz/astro-gists/components"
---
<GetGist
gistId="your-gist-id-here"
filename="name-of-desired-file-to-be-displayed.md"
/>
## Licensing
@ -62,4 +87,5 @@ TODO:configuration
## Acknowledgements
TODO:
[`Octokit`](http://octokit.github.io) by GitHub
[`Expressive-Code`](https://expressive-code.com/) By Hippotasic

37
package/expressive-code.d.ts vendored Normal file
View File

@ -0,0 +1,37 @@
/**
* @file This file provides the types for Astro-Gists's `@matthiesenxyz/astro-gists/expressive-code` export.
*/
export * from 'astro-expressive-code';
import type { AstroGistsExpressiveCodeOptions } from './src/integrations/expressive-code';
export type { AstroGistsExpressiveCodeOptions };
/**
* A utility function that helps you define an Expressive Code configuration object. It is meant
* to be used inside the optional config file `ec.config.mjs` located in the root directory
* of your Starlight project, and its return value to be exported as the default export.
*
* Expressive Code will automatically detect this file and use the exported configuration object
* to override its own default settings.
*
* Using this function is recommended, but not required. It just passes through the given object,
* but it also provides type information for your editor's auto-completion and type checking.
*
* @example
* ```js
* // ec.config.mjs
* import { defineEcConfig } from '@matthiesenxyz/astro-gists/expressive-code'
*
* export default defineEcConfig({
* themes: ['github-dark', 'github-light'],
* styleOverrides: {
* borderRadius: '0.5rem',
* },
* })
* ```
*/
export function defineEcConfig(
config: AstroGistsExpressiveCodeOptions
): AstroGistsExpressiveCodeOptions;

View File

@ -0,0 +1,21 @@
/**
* @file This file is exported by astro-gists as `@matthiesenxyz/astro-gists/expressive-code`.
*
* It is required by the `<Code>` component to access the same configuration preprocessor
* function as the one used by the integration.
*
* It also provides access to all of the Expressive Code classes and functions without having
* to install `astro-expressive-code` as an additional dependency into a user's project
* (and thereby risiking version conflicts).
*
* Note: This file is intentionally not a TypeScript module to allow access to all exported
* functionality even if TypeScript is not available, e.g. from the `ec.config.mjs` file
* that does not get processed by Vite.
*/
export * from 'astro-expressive-code';
// @ts-ignore - Types are provided by the separate `expressive-code.d.ts` file
export function defineEcConfig(config) {
return config;
}

5
package/internal.ts Normal file
View File

@ -0,0 +1,5 @@
/**
* @file This file contains utility functions imported by the `<Code>` component.
*/
export { getGistsEcConfigPreprocessor } from './src/integrations/expressive-code.js';

View File

@ -23,14 +23,26 @@
},
"sideEffects": false,
"exports": {
".": "./src/index.ts"
".": "./src/index.ts",
"./expressive-code": {
"types": "./expressive-code.d.ts",
"default": "./expressive-code.mjs"
},
"./internal": "./internal.ts",
"./components": "./src/components/index.ts",
"./components/*": "./src/components/*"
},
"scripts": {},
"type": "module",
"peerDependencies": {
"astro": "^4.0.0"
"astro": "^4.4.0"
},
"dependencies": {
"astro-integration-kit": "^0.3.0"
"@octokit/types": "12.6.0",
"@expressive-code/plugin-line-numbers": "0.33.4",
"astro-integration-kit": "^0.3.0",
"astro-expressive-code": "^0.33.4",
"octokit": "3.1.2",
"vite": "5.1.4"
}
}

View File

@ -0,0 +1,50 @@
---
import { getGistFile } from "../utils"
import { Code } from 'astro-expressive-code/components'
export interface Props {
/** REQUIRED: Used to define the desired GitHub Gist ID */
gistId: string;
/** REQUIRED: Used to define the desired file from the Gist */
filename: string;
/** OPTIONAL: Allows the user to Enable and Disable LineNumbers
* @default true
*/
showLineNumbers?: boolean;
wrap?: boolean;
}
const { gistId, filename, 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 getGistFile( gistId, filename);
---
{ Gist &&
<div>
<a class="raw" title="GitHub Gist RAW" href={Gist.raw_url}>View <svg xmlns="http://www.w3.org/2000/svg" width="28" height="28" fill="currentColor" class="bi bi-filetype-raw" viewBox="0 0 16 16"> <path fill-rule="evenodd" d="M14 4.5V14a2 2 0 0 1-2 2v-1a1 1 0 0 0 1-1V4.5h-2A1.5 1.5 0 0 1 9.5 3V1H4a1 1 0 0 0-1 1v9H2V2a2 2 0 0 1 2-2h5.5zM1.597 11.85H0v3.999h.782v-1.491h.71l.7 1.491h1.651l.313-1.028h1.336l.314 1.028h.84L5.31 11.85h-.925l-1.329 3.96-.783-1.572A1.18 1.18 0 0 0 3 13.116q0-.384-.167-.668a1.1 1.1 0 0 0-.478-.44 1.7 1.7 0 0 0-.758-.158m-.815 1.913v-1.292h.7a.74.74 0 0 1 .507.17q.194.17.194.49 0 .315-.194.474-.19.158-.518.158zm4.063-1.148.489 1.617H4.32l.49-1.617zm4.006.445-.74 2.789h-.73L6.326 11.85h.855l.601 2.903h.038l.706-2.903h.683l.706 2.903h.04l.596-2.903h.858l-1.055 3.999h-.73l-.74-2.789H8.85Z"/> </svg></a>
<Code
showLineNumbers={SHOWLINENUMBERS}
wrap={WRAP}
title={Gist.filename}
code={ Gist.content ? Gist.content : "" }
lang={Gist.language ? Gist.language.toLowerCase() : undefined }
/>
</div>
}
<style>
.raw {
display: inline-flex;
position: absolute;
z-index: 99;
right: 0;
color: white;
padding-top: 2px;
padding-inline-end: 1rem;
text-decoration: none;
}
</style>

View File

@ -0,0 +1 @@
export { default as GetGist} from "./GetGist.astro"

View File

@ -1,3 +1,3 @@
import astroGist from "./integration";
import astroGist from "./integration.js";
export default astroGist;
export default astroGist;

View File

@ -1,5 +1,7 @@
import { defineIntegration, createResolver } from "astro-integration-kit"
import { corePlugins } from "astro-integration-kit/plugins"
import { astroGistsExpressiveCode } from "./integrations/expressive-code"
export default defineIntegration({
name: "@matthiesenxyz/astro-gists",
@ -7,8 +9,19 @@ export default defineIntegration({
setup() {
const { resolve } = createResolver(import.meta.url);
return {
"astro:config:setup": ({ watchIntegration }) => {
"astro:config:setup": ({ watchIntegration, config, updateConfig, logger }) => {
watchIntegration(resolve())
// IMPORT INTEGRATIONS & INTEGRATION ROUTES
const integrations = [...config.integrations];
if (!integrations.find(({ name }) => name === "astro-expressive-code")) {
logger.info("Adding astro-expressive-code integration")
updateConfig({
integrations: [...integrations, ...astroGistsExpressiveCode()]
})
}
}
}
}

View File

@ -0,0 +1,61 @@
import {
astroExpressiveCode,
type AstroExpressiveCodeOptions,
type CustomConfigPreprocessors
} from 'astro-expressive-code';
import type { AstroIntegration } from 'astro';
import { pluginLineNumbers } from '@expressive-code/plugin-line-numbers'
/**
* Create an Expressive Code configuration preprocessor based on Starlight config.
* Used internally to set up Expressive Code and by the `<Code>` component.
*/
export function getGistsEcConfigPreprocessor(): CustomConfigPreprocessors['preprocessAstroIntegrationConfig'] {
return (input): AstroExpressiveCodeOptions => {
const ecConfig = input.ecConfig as AstroExpressiveCodeOptions; // Replace {} with the appropriate value for ecConfig
const {
themes = ["github-dark", "github-light"] as const,
styleOverrides: { ...otherStyleOverrides } = {},
plugins = [pluginLineNumbers()],
...rest
} = ecConfig;
return {
themes,
styleOverrides: {
borderRadius: '0px',
borderWidth: '1px',
codePaddingBlock: '0.75rem',
codePaddingInline: '1rem',
codeFontFamily: 'var(--__sl-font-mono)',
codeFontSize: 'var(--sl-text-code)',
codeLineHeight: 'var(--sl-line-height)',
uiFontFamily: 'var(--__sl-font)',
textMarkers: {
lineDiffIndicatorMarginLeft: '0.25rem',
defaultChroma: '45',
backgroundOpacity: '60%',
},
...otherStyleOverrides,
},
plugins,
...rest,
};
};
}
export const astroGistsExpressiveCode = (): AstroIntegration[] => {
return [
astroExpressiveCode({
customConfigPreprocessors: {
preprocessAstroIntegrationConfig: getGistsEcConfigPreprocessor(),
preprocessComponentConfig: `
import { getGistsEcConfigPreprocessor } from '@matthiesenxyz/astro-gists/internal'
export default getGistsEcConfigPreprocessor()
`,
},
}),
];
};

24
package/src/utils.ts Normal file
View File

@ -0,0 +1,24 @@
import { Octokit } from "octokit";
import { loadEnv } from "vite";
const { GITHUB_PERSONAL_TOKEN } = loadEnv("all", process.cwd(), "GITHUB_");
export const octokit = new Octokit({ auth: GITHUB_PERSONAL_TOKEN });
export const getGist = async (gistId: string) => {
const { data } = await octokit.request('GET /gists/{gist_id}', { gist_id: gistId });
return data;
};
export const getGistFile = async (
gistId: string,
filename: string
) => {
const gist = await getGist(gistId);
if (gist?.files) {
const file = gist.files[filename];
return file ? file : null;
}
return null;
};

View File

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

View File

@ -11,10 +11,8 @@
"astro": "astro"
},
"dependencies": {
"@astrojs/tailwind": "^5.1.0",
"astro": "^4.4.4",
"@matthiesenxyz/astro-gists": "workspace:*",
"tailwindcss": "^3.4.1"
"@matthiesenxyz/astro-gists": "*",
"astro": "^4.4.4"
},
"devDependencies": {
"@astrojs/check": "^0.5.5",

View File

@ -1,123 +1,9 @@
---
import Layout from '../layouts/Layout.astro';
import Card from '../components/Card.astro';
import { GetGist } from "@matthiesenxyz/astro-gists/components"
---
<h1>Testing</h1>
<Layout title="Welcome to Astro.">
<main>
<svg
class="astro-a"
width="495"
height="623"
viewBox="0 0 495 623"
fill="none"
xmlns="http://www.w3.org/2000/svg"
aria-hidden="true"
>
<path
fill-rule="evenodd"
clip-rule="evenodd"
d="M167.19 364.254C83.4786 364.254 0 404.819 0 404.819C0 404.819 141.781 19.4876 142.087 18.7291C146.434 7.33701 153.027 0 162.289 0H332.441C341.703 0 348.574 7.33701 352.643 18.7291C352.92 19.5022 494.716 404.819 494.716 404.819C494.716 404.819 426.67 364.254 327.525 364.254L264.41 169.408C262.047 159.985 255.147 153.581 247.358 153.581C239.569 153.581 232.669 159.985 230.306 169.408L167.19 364.254ZM160.869 530.172C160.877 530.18 160.885 530.187 160.894 530.195L160.867 530.181C160.868 530.178 160.868 530.175 160.869 530.172ZM136.218 411.348C124.476 450.467 132.698 504.458 160.869 530.172C160.997 529.696 161.125 529.242 161.248 528.804C161.502 527.907 161.737 527.073 161.917 526.233C165.446 509.895 178.754 499.52 195.577 500.01C211.969 500.487 220.67 508.765 223.202 527.254C224.141 534.12 224.23 541.131 224.319 548.105C224.328 548.834 224.337 549.563 224.347 550.291C224.563 566.098 228.657 580.707 237.264 593.914C245.413 606.426 256.108 615.943 270.749 622.478C270.593 621.952 270.463 621.508 270.35 621.126C270.045 620.086 269.872 619.499 269.685 618.911C258.909 585.935 266.668 563.266 295.344 543.933C298.254 541.971 301.187 540.041 304.12 538.112C310.591 533.854 317.059 529.599 323.279 525.007C345.88 508.329 360.09 486.327 363.431 457.844C364.805 446.148 363.781 434.657 359.848 423.275C358.176 424.287 356.587 425.295 355.042 426.275C351.744 428.366 348.647 430.33 345.382 431.934C303.466 452.507 259.152 455.053 214.03 448.245C184.802 443.834 156.584 436.019 136.218 411.348Z"
fill="url(#paint0_linear_1805_24383)"></path>
<defs>
<linearGradient
id="paint0_linear_1805_24383"
x1="247.358"
y1="0"
x2="247.358"
y2="622.479"
gradientUnits="userSpaceOnUse"
>
<stop stop-opacity="0.9"></stop>
<stop offset="1" stop-opacity="0.2"></stop>
</linearGradient>
</defs>
</svg>
<h1>Welcome to <span class="text-gradient">Astro</span></h1>
<p class="instructions">
To get started, open the directory <code>src/pages</code> in your project.<br />
<strong>Code Challenge:</strong> Tweak the "Welcome to Astro" message above.
</p>
<ul role="list" class="link-card-grid">
<Card
href="https://docs.astro.build/"
title="Documentation"
body="Learn how Astro works and explore the official API docs."
/>
<Card
href="https://astro.build/integrations/"
title="Integrations"
body="Supercharge your project with new frameworks and libraries."
/>
<Card
href="https://astro.build/themes/"
title="Themes"
body="Explore a galaxy of community-built starter themes."
/>
<Card
href="https://astro.build/chat/"
title="Community"
body="Come say hi to our amazing Discord community. ❤️"
/>
</ul>
</main>
</Layout>
<style>
main {
margin: auto;
padding: 1rem;
width: 800px;
max-width: calc(100% - 2rem);
color: white;
font-size: 20px;
line-height: 1.6;
}
.astro-a {
position: absolute;
top: -32px;
left: 50%;
transform: translatex(-50%);
width: 220px;
height: auto;
z-index: -1;
}
h1 {
font-size: 4rem;
font-weight: 700;
line-height: 1;
text-align: center;
margin-bottom: 1em;
}
.text-gradient {
background-image: var(--accent-gradient);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-size: 400%;
background-position: 0%;
}
.instructions {
margin-bottom: 2rem;
border: 1px solid rgba(var(--accent-light), 25%);
background: linear-gradient(rgba(var(--accent-dark), 66%), rgba(var(--accent-dark), 33%));
padding: 1.5rem;
border-radius: 8px;
}
.instructions code {
font-size: 0.8em;
font-weight: bold;
background: rgba(var(--accent-light), 12%);
color: rgb(var(--accent-light));
border-radius: 4px;
padding: 0.3em 0.4em;
}
.instructions strong {
color: rgb(var(--accent-light));
}
.link-card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(24ch, 1fr));
gap: 2rem;
padding: 0;
}
</style>
<GetGist
gistId="ff6704ff48e79f45ce5f7c47932bffab"
filename="authentik.yaml"
/>

View File

@ -1,8 +0,0 @@
/** @type {import('tailwindcss').Config} */
export default {
content: ["./src/**/*.{astro,html,js,jsx,md,mdx,svelte,ts,tsx,vue}"],
theme: {
extend: {},
},
plugins: [],
};

View File

@ -1,7 +1,4 @@
{
"extends": "astro/tsconfigs/strict",
"compilerOptions": {
"jsx": "preserve"
},
"exclude": ["dist"]
}

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,3 @@
packages:
- package
- "packages/*"
- "playground"