npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@kotosha/astro-gist

v1.0.0

Published

Dead simple gist rendering buddy for your Astro blog

Downloads

14

Readme

Astro Gist

Dead simple yet powerful gist rendering buddy for your Astro blog.

Features

✅ Uses the standard gist embed script and wraps it in an iframe to avoid blocking the main thread

✅ Lazy loads the gist using IntersectionObserver to improve performance

✅ Allows you to style the gist using your own stylesheet

✅ Fails gracefully when the gist fails to load, showing a link to the gist

✅ Supports rendering a specific file from the gist

✅ Supports adding a caption to the gist

✅ Zero dependencies

✅ Less than 1.5 kB of client JavaScript

Installation

# pnpm
pnpm add @kotosha/astro-gist

# npm
npm install @kotosha/astro-gist

# yarn
yarn add @kotosha/astro-gist

Usage

You can use it in your Astro blog both in .astro and .mdx files. Just import the component and use it with the id prop:

// YourComponent.astro
---
import Gist from '@kotosha/astro-gist'
---

<div>
    <Gist id="your-gist-id" />
</div>
// YourPost.mdx
import Gist from '@kotosha/astro-gist'

Awesome gist here:

<Gist id="your-gist-id" />

You can also pass additional props to customize the component behavior:

| Name | Type | Default | Description | Required | | ------------------- | ------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | | id | string | - | Gist ID | ✔️ | | caption | string | - | Caption to display below the gist. If not provided, no caption will be displayed. Caption will only be displayed if the gist is loaded successfully | | className | string | - | Additional class to add to the figure. Useful for styling | | file | string | - | File to display from the gist. If not provided, the whole gist will be displayed | | gistStylesUrl | string | - | URL to the stylesheet to use for styling the gist | | lazy | boolean | true | Whether to lazy load the gist. Use IntersectionObserver to load the gist when it comes into view. If IntersectionObserver is not supported, the gist will be loaded immediately | | observerRootMargin | number | 150 | Margin around the root of the IntersectionObserver. The more the margin, the earlier the iframe will be loaded | | showGistLinkOnError | boolean | true | Whether to show the link to the gist when the gist fails to load |

Customization

Gist is rendered as a figure with an optional figcaption. You can style it using the className prop:

<Gist className="cls" id="your-gist-id" />

<style>
    .cls {
        /* Basic styles here */
    }

    .cls figcaption {
        /* Figcaption styles here */
    }
</style>

You can also apply styles based on the loading status using the data-loading-status attribute:

.cls[data-loading-status='success'] {
    /* Success styles here */
}

.cls[data-loading-status='error'] {
    /* Error styles here */
}

To style the gist itself, pass the URL to the stylesheet you want to use as the gistStylesUrl prop:

<Gist id="your-gist-id" gistStylesUrl="/gist-theme.css" />

It might be convenient to use a wrapper component to pass the gistStylesUrl prop to all gists in your blog:

// GistWrapper.astro
---
import Gist from '@kotosha/astro-gist'

const props = Astro.props;
---

<Gist gistStylesUrl="/gist-theme.css" {...props} />

Astro Gist uses the srcdoc iframe attribute: when the gist is ready to be rendered, it composes the appropriate srcdoc and passes it to the iframe. Any HTML attributes added to that iframe's html tag are preserved, so it's really easy to style your gists according to your app's theme:

// gist.css
html[data-theme="light"] ... {
    /* Light theme styles here */
}
html[data-theme="dark"] ... {
    /* Dark theme styles here */
}

// Your page
<Gist gistStylesUrl="/gist.css" {...props} />

// Your theme provider
onThemeChange = (theme) => {
    document.querySelectorAll('[data-astro-gist-iframe]').forEach(iframe => {
        const iframeDocument =  iframe.contentWindow?.document || iframe.contentDocument;

        // Could be classes. Just anything you can work with from your CSS
        iframeDocument.documentElement.dataset.theme = theme
    })
}

License

MIT