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-syntax-highlight

v0.1.3

Published

Syntax highlight code blocks with a custom highlight function in remark

Downloads

25

Readme

remark-syntax-highlight

Build Status Code Coverage MIT License Version Bundle size (minified + gzip)

Syntax highlight code blocks with a custom highlight function in remark.

Why

Several markdown parsers accept a highlight function where you can highlight code in any way you want. It's especially useful if you're concerned about the bundle size and want to support a limited set of languages. However, with remark, you have to write a custom plugin to achieve the same behavior.

The official plugin for highlighting is remark-highlight.js, which uses highlight.js. However, it isn't possible to customize which languages are loaded. In addition, if you want to use something like prism.js instead of highlight.js, there's no similar plugin available.

This plugin is a tiny abstraction over remark's plugin API, allowing you to pass a custom highlight function.

Installation

npm install remark-syntax-highlight

or

yarn add remark-syntax-highlight

Usage

First, we need to wrap our worker:

import remark from 'remark';
import highlight from 'remark-syntax-highlight';

import html from 'remark-html';
import github from 'hast-util-sanitize/lib/github';
import merge from 'deepmerge';

// Import the highlighter, for example, say I want prism with javascript support
import { highlight, languages } from 'prismjs/components/prism-core';
import 'prismjs/components/prism-clike';
import 'prismjs/components/prism-javascript';

// Preserve className attributes when sanitizing the HTML
// This is necessary for syntax highlighting
const schema = merge(github, { attributes: { '*': ['className'] } });

remark()
  .use(html, { sanitize: schema })
  .use(highlight, {
    // Pass a highlight function to highlight the code
    highlight: (code, language) => {
      const grammar = languages[lang];

      if (grammar) {
        return highlight(code, grammar);
      }
    },
  })
  .process(markdown, function(err, file) {
    // do something
  });

You can return highlighted HTML code from the highlight, or null/undefined if no highlight was performed. The plugin will escape the HTML from the code if no highlight was performed.

You can also return a promise which resolves to the highlighted HTML. This can be useful if you want to load languages as needed.

Contributing

While developing, you can run the example app and open the console to see your changes:

yarn example

Make sure your code passes ESLint. Run the following to verify:

yarn lint

To fix formatting errors, run the following:

yarn lint -- --fix