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

gatsby-md-fenced-block

v1.0.0

Published

A fenced code block generator for md/mdx files used for making gatsby pages.

Downloads

5

Readme

Why ?

In noraml md/mdx this ``` ``` generates a syntax highlighted code block but gatsby-plugin-mdx or any other mdx to html coverter, while converting these md/mdx pages to html pages does not do it for you. prism-react-renderer a "syntax highlighting library" gives you a REACT component but combining it with gatsby needs a bit of configuration and it doesn't work directly with markdown files.

gatsby-md-fenced-block is a glue layer between the two ( Prism and gatsby-plugin-mdx ). It is a very small but handly library to save some of your time.

Note: This is a part of gatsby-md-style which provides styles to every html files coverted from md by gatsby. Like the quotes,tables etc.

Installation

This module is distributed via npm which is bundled with node and should be installed as one of your project's dependencies:

# npm
npm install --save gatsby-md-fenced-block

# yarn
yarn add gatsby-md-fenced-block

The module needs the latest version of REACT to be installed, please make sure to install react.

Usage

The simple usage of the module.

/** The module gatsby-md-fenced-block*/
import fencedBlock from "gatsby-md-fenced-block";

/** Call fencedBlock function and pass the prism-react-renderer component. Here named Code*/
let component = fencedBlock(Code)

/** Finally pass the component to MDXProvider to use fence block in md/mdx files.*/
<MDXProvider components={component}>{children}</MDXProvider>

Example

Note: The example assumes that you have created a gatsby project, added gatsby-plugin-mdx and have installed prism-react-renderer

1. Create a component (here Code)

/** React import */
import React from 'react'

/** prism-react-renderer import */
import Highlight, { defaultProps } from "prism-react-renderer";
import nightOwl from "prism-react-renderer/themes/nightOwl";

/** Code component with language and code props */
const Code = ({language,code}) => {
    return (
        /** Code taken directly from prism-react-renderer docs. */
        <Highlight
          {...defaultProps}
          theme={nightOwl}
          code={code}
          language={language}
        >
          {({
            className,
            style,
            tokens,
            getLineProps,
            getTokenProps,
          }) => (
            <pre className={className} style={style}>
              {tokens.map((line, i) => (
                <div {...getLineProps({ line, key: i })}>
                  {line.map((token, key) => (
                    <span {...getTokenProps({ token, key })} />
                  ))}
                </div>
              ))}
            </pre>
          )}
        </Highlight>
    )
}

export default Code

The above code is taken from prism-react-renderer docs. The only thing to note here is that the language and the code shall be passed as props to this component.

2. In your mdx/md Layout File add this code. This code can be in a seperate layout file passed as default to gatsby-plugin-mdx options in gatsby-config file or can be added to gatsby-ssr and gatsby-browser file.

/** React import */
import React from "react";

/** MDX provider */
import { MDXProvider } from "@mdx-js/react";

/** Code component we created just now in step 1. */
import Code from "../components/Code";

/** The module gatsby-md-fenced-block*/
import fencedBlock from "gatsby-md-fenced-block";


/** Layout component **/
const Layout = ({ children }) => {
  return (
    /** call fencedBlock function and pass the code component */  
    <MDXProvider components={fencedBlock(Code)}>{children}</MDXProvider>
  );
};


export default Layout;

That's all now using this ``` ``` in the md/mdx file will show syntax highlighting

API Reference

Replaces pre component

  fenceBlock(react_component)

| Parameter | Type | Description | | :-------- | :------- | :------------------------- | | react_component | Function | Required |

The function returns a object which replaces the default "pre" with the "react_component" when passed to MDXProvider.

Style

To style the code block ( like spacing, padding, margins) select the pre tag.

pre{
  /** Style goes here **/
}

Attribute

The icon is taken from Freepik from www.flaticon.com

License

MIT