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

code-blocks

v1.1.0

Published

Parse fenced code blocks from markdown with useful metadata

Downloads

396

Readme

code-blocks

npm build status

Parse fenced code blocks from Markdown with useful metadata.

npm install [--save | --save-dev] code-blocks

Usage

// ES5/CommonJS
const codeBlocks = require('code-blocks')
// ES2015/ES6/Babel, etc.
import codeBlocks from 'code-blocks'

codeBlocks.fromFile('README.md')
  .then(blocks => {
    // do stuff with blocks here
  })

See the API documentation for more examples.

How it works

This library uses remark to parse Markdown into a unist tree, then finds all of the fenced code blocks. Those with a language identifier after the opening ``` or ~~~ get some additional properties.

Code block info

According to the CommonMark Spec:

The first word of the info string is typically used to specify the language of the code sample, and rendered in the class attribute of the code tag. However, this spec does not mandate any particular treatment of the info string.

In other words, CommonMark-compliant parsers should safely ignore everything after the language identifier. That's where we can attach additional key/value pairs, which are parsed as each code block's info, as in:

```html title="A dumb example" foo=bar "x.y.z"="1 2 3"
<h1>Hello, world!</h1>
```

When parsed with code-blocks, this would yield an array with one object:

[{
  type: 'code',
  lang: 'html',
  value: '<h1>Hello, world!</h1>',
  info: {
    title: 'A dumb example',
    foo: 'bar',
    'x.y.z': '1 2 3'
  },
  title: 'A dumb example',
  source: {
    file: 'README.md',
    line: 1
  },
  position: {
    // see https://github.com/syntax-tree/unist#position
  }
}]

Node properties

The unist node objects returned by all of the block parsing functions are "enhanced" with the following properties:

  • lang contains only the first "word" of the info string
  • info is an object of key/value pairs parsed from the remainder of the info string
  • title is the title of the code block, as determined by this algorithm
  • source is an object with two keys:
    • file is the path provided as the first argument to fromFile() and fromFileSync(), or as the last argument to fromString() or fromAST(). (If no file is provided, this value will be buffer.)
    • line is the starting line of the code block in markdown input.

See the mdast documentation for more info about the Code nodes generated by remark, and the unist documentation for more on the underlying structures.

Code block titles

Because code blocks are often meaningless without at least some context, every block parsed gets a title according to the following algorithm:

  1. If a title key is found in the block's info object, use that.

  2. Otherwise, find the previous heading in the markdown and use its text.

    1. If two or more code blocks share the same heading, add a numeric suffix: (2) for the second, (3) for the third, and so on.
  3. If no previous heading is found, provide a title that describes where it comes from, in the form:

    Code block {n} from {filename}:{line}

    Where {n} is the 1-based index of the code block in the parsed file, {filename} is the parsed file (or buffer), and {line} is the line at which the code block starts.