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

rehype-code-titles

v1.2.0

Published

Rehype plugin for parsing code blocks and adding titles to code blocks

Downloads

9,809

Readme

rehype-code-titles

npm

All Contributors

Rehype plugin for parsing code blocks and adding titles to code blocks

Why?

I moved my blog over to using mdx-bundler which uses xdm under the hood to parse the markdown and MDX files. I was using remark-code-titles prior to this move and unfortunately it no longer worked. I believe this was because of the order plugins were being applied internally for xdm. I'd never really worked with remark or rehype directly before and didn't have a lot of experience with ASTs so this was a fun little project that I initially built directly into my blog before pulling it out at a plugin to ship to other developers.

Many thanks to @mottox2, @mapbox, & @wooorm for their prior work in this ecosystem it was of great help when creating this plugin.

Installation

This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required.

npm install rehype-code-titles

yarn add rehype-code-titles

pnpm add rehype-code-titles

API

This package exports no identifiers. The default export is rehypeCodeTitles

rehype().use(rehypeCodeTitles[, options])

Add support for stripping out code titles from input.

options

options.customClassName

Specify your own custom css class name to apply. Defaults to rehype-code-title. Note: you will have to write the CSS implementation yourself.

For example

// some global css file
.rehype-code-title {
  margin-bottom: -0.6rem;
  padding: 0.5em 1em;
  font-family: Consolas, 'Andale Mono WT', 'Andale Mono', 'Lucida Console',
    'Lucida Sans Typewriter', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono',
    'Liberation Mono', 'Nimbus Mono L', Monaco, 'Courier New', Courier,
    monospace;

  background-color: black;
  color: white;
  z-index: 0;

  border-top-left-radius: 0.3em;
  border-top-right-radius: 0.3em;
}
options.titleSeparator

Specify the title separator for rehype-code-title. Defaults to: :.

// default behavior will be
'language-typescript:lib/mdx.ts' // the title will be lib/mdx.ts
'language-typescript:title=lib/mdx.ts' // title will be title=lib/mdx.ts

// titleSeparator set to :title=
'language-typescript:lib/mdx.ts' // Wont work! 😱. Does not match the separator
'language-typescript:title=lib/mdx.ts' // title will be lib/mdx.ts

Input

## Code Example

```typescript:lib/mdx.ts
// code here
```

Output

<div class="rehype-code-title">lib/mdx.ts</div>
<pre>
  <code class="language-typescript">
  <!-- HTML parse code here -->
  </code>
</pre>

Usage

Use this package as a rehype plugin.

const rehype = require('rehype')
const rehypeCodeTitles = require('rehype-code-titles')
const rehypePrism = require('@mapbox/rehype-prism')

rehype()
  .use(rehypeCodeTitles) // should always be before rehypePrism.
  .use(rehypePrism)
  .process(/* some html */)
const unified = require('unified')
const rehypeParse = require('rehype-parse')
const rehypeCodeTitles = require('rehype-code-titles')
const rehypePrism = require('@mapbox/rehype-prism')

unified()
  .use(rehypeParse)
  .use(rehypeCodeTitles)
  .use(rehypePrism)
  .processSync(/* some html */)

Development

This repository makes use of @arkweid/lefthook and will run eslint, jest, and prettier against all staged files.

git clone https://github.com/rockchalkwushock/rehype-code-titles.git
cd rehype-code-titles
pnpm i
# Do cool stuff with code
git add .
git commit -m "feat(src): a cool new feature"
# pre-commit hooks run: eslint, jest, and prettier
git push

Contributing

Please visit CONTRIBUTING.md

License

MIT © Cody Brunner

Contributors ✨

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!