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

codedown

v3.2.1

Published

Extract and run code blocks from Markdown files

Downloads

707

Readme

Build Status Coverage Status npm version

Codedown

Codedown is a little utility to extract code blocks from Markdown files. Inspired by literate Haskell, Codedown can be used to:

  • Validate the correctness of code embedded in Markdown
  • Run code embedded in Markdown
  • Ship code and Markdown together in harmony

Quicker start

To skip installing Codedown locally, try it online.

Quick start

Install Codedown:

$ npm install -g codedown

Run Codedown:

Usage: codedown <lang> [...]

Options:
--separator <separator line>
--section <section number>

Example:
cat README.md | codedown haskell --separator=----- --section 1.3

Codedown reads Markdown from stin, extracts the code blocks designated as language <lang>, and outputs them to stdout. The example above extracts the Haskell code from section 1.3 of this file, and outputs it with five dashes in between each block:

x :: Int
x = 42
-----
main :: IO ()
main = putStrLn $ show x

We can pipe the output of Codedown to a language interpreter:

$ cat README.md | codedown haskell | runhaskell
42
$ cat README.md | codedown javascript | node
42
$ cat README.md | codedown scala | xargs -0 scala -e
42

Examples

This readme is a Markdown file, so we can use Codedown to extract code from it.

Variables in different languages

In the following code blocks, let's set x to 42 in different languages:

Haskell:

x :: Int
x = 42

JavaScript:

var x = 42;

Scala:

val x = 42

Console output in different languages

Now let's print x it to stdout in different languages. This time, the code blocks are nested within an unordered list:

  • Haskell:

    main :: IO ()
    main = putStrLn $ show x
  • JavaScript:

    console.log(x);
  • Scala:

    println(x)

Sections and subsections

The section above is 1.3, counting by headings. It has two subsections (1.3.1 and 1.3.2). We can specify a section number to extract the content from just that section:

$ cat README.md | codedown haskell --section 1.3
x :: Int
x = 42

main :: IO ()
main = putStrLn $ show x
$ cat README.md | codedown haskell --section 1.3.1
x :: Int
x = 42
$ cat README.md | codedown haskell --section 1.3.2
main :: IO ()
main = putStrLn $ show x

We can also specify a section by heading:

cat README.md | ./codedown.js haskell --section '### Variables in different languages'
x :: Int
x = 42

Wildcard matching

Codedown can use wildcards to match file paths, which are used by some markdown implementations:

lib/codedown.js:

var x = 42;
$ cat README.md | codedown '**/*.js'
var x = 42

Additionally, you can use a special * character in place of the language option to extract any/all code blocks agnostic of language:

$ cat README.md | codedown '*'

Separator

If there are multiple code blocks in the same file, we can specify a separator to insert in between them:

$ cat README.md | codedown haskell --separator=-----
x :: Int
x = 42
-----
main :: IO ()
main = putStrLn $ show x