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

remark-inline-local-code-snippets

v1.0.0

Published

Remark plugin to inline GitHub code snippets

Downloads

5

Readme

remark-inline-local-code-snippets

NPM version NPM downloads Build typescript License

This package is a unified (remark) plugin to replace links to a github code snippet with the code snippet itself.

Installation

npm install remark-inline-local-code-snippets

Usage

If you have a markdown file like this:

My blog post about code has snippets:

[inline](./index.ts#L8-L23)

This is the `.prettierrc.json` file:

[inline](./.prettierrc.json#L1-L7)

With a configuration like this

import * as path from "path";
import { read } from "to-vfile";
import remark from "remark";
import gfm from "remark-gfm";
import remarkRehype from "remark-rehype";
import rehypeStringify from "rehype-stringify";

import remarkInlinLocalCodeSnippet from "remark-inline-local-code-snippet";

main();

async function main() {
  const file = await remark()
    .use(gfm)
    .use(remarkInlinLocalCodeSnippet, {
      rootPath: path.resolve(__dirname, "src")),
      inlineMarker: "inline",
      originComment: "Source of this code snippet: <url>"
    })
    .use(remarkRehype)
    .use(rehypeStringify)
    .process(await read("example.md"));
}

We get an output equivalent to this Markdown:

My blog post about code has snippets:

\`\`\`typescript
export type Options = {
  // If this string is detected in a link text, the link will be replaced with a code snippet
  // Default: "inline"
  inlineMarker?: string;
  // The comment placed on top of the linked code snippet, <url> will be replaced with the URL
  // of the link. If undefined, no comment will be added.
  // Default: undefined
  originComment?: string;
  // Function that gets called with an error if a fetch fails. If undefined, the error will be
  // ignored.
  // Default: undefined
  logError?: (error: Error) => void;
  // The root path of the project, used to resolve relative paths. Ideally use an absolute path.
  // Default: process.cwd()
  rootPath?: string;
};
\`\`\`

This is the `.prettierrc.json` file:

\`\`\`json
{
  "bracketSpacing": true,
  "trailingComma": "all",
  "tabWidth": 2,
  "printWidth": 96
}
\`\`\`

Please note that you need the starting and ending line numbers in the URL to get the correct code snippet.

Options

rootPath

The base path that is used to resolve relative paths. Default: process.cwd().

inlineMarker

The string that is used to identify links that should be replaced with code snippets. Default: inline.

originComment

A comment that is placed on top of the linked code snippet. The string <url> will be replaced with the URL of the link. Default: undefined.

Supported languages

  • JavaScript
  • TypeScript
  • Python
  • Shell
  • JSON
  • YAML
  • Terraform
  • HCL
  • Go

Adding a new language is easy, feel free to open a PR if your favorite language is missing.

License

MIT License © DanielMSchmidt