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

eleventy-gist

v1.2.2

Published

Render your Github gists in markdown

Downloads

14

Readme

Eleventy Gist

An Eleventy plugin to display your Github gists in markdown.

This plugin came from the process of converting a Jekyll site to one generated with Eleventy. In the old site, I made extensive use of the jekyll-gist gem for rendering code snippets from Github Gist. After copying markdown files from the old to the new _posts directories, I had to figure out how to avoid manually replacing a bunch of {% gist %} shortcodes. Eleventy Gist helps replace the Jekyll Gist gem in Javascript-based static site generators.

Installation and setup

  1. You will need a Github personal access token. These instructions should help. If the token is scoped to just Github Gist, that should be okay.

  2. In the root of your static site project, run:

$ npm install eleventy-gist --save-dev
  1. If you are using this in Eleventy, add the following to your eleventy.config.js file:
const gist = require('eleventy-gist');

module.exports = function(eleventyConfig) {
    eleventyConfig.addPlugin(gist, {
		authToken: '<MY ACCESS TOKEN FROM STEP ONE, USED FOR AN Authorization HEADER>',
		userAgent: '<NAME TO PASS TO A User-Agent HEADER>'
	});
}

Configuration

The following configuration options are available:

authToken

  • type: string
  • required: yes
  • default?: none

This is your Github Gist API bearer token (see Installation and setup).

userAgent

  • type: string
  • required: yes
  • default?: none

This value gets passed to a User-Agent header when calling the Github Gist API.

useCache

  • type: boolean
  • required: no
  • default?: false

If set to true, caches the rendered Gist content after making a first call to the Github Gist API. It is recommended to set it to true while using an eleventy project in development to reduce build times and API traffic.

debug

  • type: boolean
  • required: no
  • default?: false

If set to true, if there are any errors returned while rendering your Gist, this will render the error in your Eleventy page. Not recommended for production. If set to false, eleventy-gist will just render an empty string.

When debug is true, any error messages get output to the page like below:

<p class="gist-error">gist error: my-file.rb not found in this gist</p>

The error messages can be selected with the .gist-error class.

addHiddenField

(v 1.2.2)

  • type: boolean
  • required: no
  • default?: false

If set to true, adds a hidden field next to the code block with the raw contents of the gist file. This can be useful for accessing the text of the code block without the extra elements that a syntax highlighter might add. (In my use case, it as for creating copy/paste feature).

After the markdown engine renders the content, the resulting html will look something like this:

<pre>
	<code class="language-javascript">
		... a whole mess of special divs and classes to render the syntax highlighting
	</code>
</pre>
<pre class="eleventy-gist-raw-content" style="display:none">
	<code>
        ... your raw content, hidden but available in the DOM ....
	</code>
</div>

Example config using environment variables:

const config = {
	authToken: process.env.github_access_token,
	userAgent: process.env.github_user_agent,
	debug: process.env.NODE_ENV === 'development', 
	useCache: process.env.NODE_ENV === 'development',
	addHiddenField: true
};

module.exports = function(eleventyConfig) {
    eleventyConfig.addPlugin(gist, config);
}

Usage

  1. Use the gist shortcode as follows in Liquid or Nunjucks templates:

When you have some code in a Github Gist, get the ID of the Gist and the name of the file with the code, then add those as strings following the gist shortcode. For example, at this url https://gist.github.com/jsheridanwells/1fee874ca9e0addefd0241419dcc561e the Gist ID is 1fee874ca9e0addefd0241419dcc561e and it has a file called ng-example.ts. In your template, fetch the code like this:

{% gist '1fee874ca9e0addefd0241419dcc561e' 'ng-example.ts' %}
  1. If you want to access the main function to fetch the code in a Gist file and output it to markdown:
const { gist } = require('eleventy-gist/gist');
const authToken = 'MY ACCESS TOKEN';
const userAgent = 'my-user-agent';
const myCodeInMarkdown = gist('1fee874ca9e0addefd0241419dcc561e', 'ng-example.ts', { authToken, userAgent });

Note: If an error is thrown while generating your site with the plugin, the error message will print in the console. The plugin will return an empty string so as to not to completely blow up the process.