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-code-frontmatter

v1.0.0

Published

Extract frontmatter from markdown code blocks using remark, and do interesting things!

Downloads

631

Readme

remark-code-frontmatter

Build Status Total alerts Language grade: JavaScript

Extract frontmatter from markdown code blocks using remark and front-matter, and do interesting things!

For example:

  • Add properties that add indentation to your code
  • Add properties that indicate how the given code should wrap
  • Add properties that specify links that should be attached to the HTML output of your code
  • Add properties that specify which sort of syntx highlighting should be used
  • Add properties that specify other ways in which HTML output should be manipulated

... The possibilities are endless!

This plugin is compatible with most remark syntax highlighting plugins, including remark-midas, remark-tree-sitter and remark-highlight.js. Just make sure that you use this plugin before the highlighting plugins.

You can also use this plugin with remark-code-extra to use frontmatter data in additional HTML output for your code blocks.

Install

npm:

npm install remark-code-frontmatter

Use

An additional field frontmatter is added to all code MDAST nodes for later use.

Say we have the following Markdown file, example.md:

```c
---
wrap: c-main
---
printf("Hello, World!");
return 0;
```

```c
// Some other code
```

And our script, example.js, looks as follows:

const vfile = require('to-vfile')
const report = require('vfile-reporter')
const unified = require('unified')
const visit = require('unist-util-visit');
const markdown = require('remark-parse')
const html = require('remark-html')
const codeFrontmatter = require('remark-code-frontmatter');

// Wrap code in boilerplate where neccesary
const transformer = tree => {
  visit(tree, 'code', node => {
    if (node.frontmatter.wrap === 'c-main') {
      node.value = [
        '#include<stdio.h>',
        'int main()',
        '{',
        // indent
        ...node.value.split('\n').map(line => '  ' + line),
        `}`,
      ].join('\n');
    }
  });
  return tree;
};

unified()
  .use(markdown)
  .use(codeFrontmatter)
  .use(() => transformer)
  .use(html)
  .process(vfile.readSync('example.md'), (err, file) => {
    console.error(report(err || file))
    console.log(String(file))
  })

Now, running node example yields:

example.md: no issues found
<pre><code class="language-c">#include&#x3C;stdio.h>
int main()
{
  printf("Hello, World!");
  return 0;
}
</code></pre>
<pre><code class="language-c">// Some other code
</code></pre>

Use with code highlighters

This plugin is compatible with most remark syntax highlighting plugins, including remark-midas, remark-tree-sitter and remark-highlight.js. Just make sure that you use this plugin before the highlighting plugins.

Example:

unified()
  .use(markdown)
  .use(codeFrontmatter)
  .use(highlight) // comes AFTER codeFrontmatter, could be other highlighting plugins
  // Other plugins
  .use(html)

Use with remark-code-extra

You can access the markdown from within the transform function that you pass to the options for that plugin.

For example, if you had the following markdown:

```
---
before: Some header text
---
Code block with a header
```

```
---
after: Some footer text
---
Code block with a footer
```

```
---
before: Some header text
after: Some footer text
---
Code block with a header and footer
```

```
Code block with no header or footer
```

And the following unified processor:

// other imports
const codeFrontmatter = require('remark-code-frontmatter');
const codeExtra = require('remark-code-extra');

const processor = remark()
  .use(codeFrontmatter)
  .use(codeExtra, {
    transform: node => node.frontmatter.before || node.frontmatter.after ? {
      before: node.frontmatter.before && [{
        type: 'text',
        value: node.frontmatter.before
      }],
      after: node.frontmatter.after && [{
        type: 'text',
        value: node.frontmatter.after
      }]
    } : null
  })
  .use(html);

Then this would output the following HTML:

<div class="code-extra">Some header text<pre><code>Code block with a header</code></pre></div>
<div class="code-extra"><pre><code>Code block with a footer</code></pre>Some footer text</div>
<div class="code-extra">Some header text<pre><code>Code block with a header and footer</code></pre>Some footer text</div>
<pre><code>Code block with no header or footer
</code></pre>

API

remark().use(codeFrontmatter)

Extract frontmatter from markdown code blocks using front-matter.

Related