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

babel-plugin-transform-syntax-highlight

v0.1.0

Published

Perform syntax highlighting of string and template literals during Babel compilation, rather than at runtime

Downloads

6

Readme

babel-plugin-transform-syntax-highlight

Build Status

🚧 🚧 EXPERIMENTAL! 🚧 🚧

Performs syntax highlighting of string and template literals during Babel compilation, rather than at runtime.

Transforms a special function call into one of the following:

  • HTML mode: A function expression for a function that returns a template literal.
  • React mode: A stateless functional React component.

In both cases, you can provide delimited placeholders within the code string that can be interpreted at runtime, when you invoke the function or create a React element from the component.

Usage

require or import 'babel-plugin-transform-syntax-highlight/highlight', or whatever you've specified as packageName in your Babel options.

Now you have an object with react and html functions. Those functions accept two arguments: options and code. (options is optional.)

The react function returns a React component, whose props can be used for interpolation.

The html function returns a function that accepts a props argument, which can be used for interpolation.

React mode example:

// Input:
const highlight = require('babel-plugin-transform-syntax-highlight/highlight');
// Or import highlight from 'babel-plugin-transform-syntax-highlight/highlight';
const SomeCode = highlight.react({
  language: 'javascript',
  highlight: 'prism'
}, `
  const foo = "bar";
  const bar = "{# props.bar #}";
`);

// Output
const SomeCode = function SomeCode(props) {
  return (
    <pre>
      <code className="language-javascript">
        <span className="token keyword">const</span> foo{' '}
        <span className="token operator">=</span>{' '}
        <span className="token string">"bar"</span>
        <span className="token punctuation">;</span>{'\n'}
        <span className="token keyword">const</span> bar{' '}
        <span className="token operator">=</span>{' '}
        <span className="token string">"{props.bar}"</span>
        <span className="token punctuation">;</span>
      </code>
    </pre>
  );
}

// Usage
<SomeCode bar="something special" />;

HTML mode example:

// Input:
const highlight = require('babel-plugin-transform-syntax-highlight/highlight');
// Or import highlight from 'babel-plugin-transform-syntax-highlight/highlight';
const someCode = highlight.html(`
  const foo = 'bar';
  const bar = '{# props.bar #}';
`);

// Output
const someCode = function(props) {
  return `<pre><code class="hljs"><span class="hljs-keyword">const</span> foo = <span class="hljs-string">'bar'</span>;
<span class="hljs-keyword">const</span> bar = <span class="hljs-string">'${props.bar}'</span>;</code></pre>`;
}

// Usage
myDiv.innerHTML = someCode({ bar: 'something special' });

Plugin options

When you add the plugin to your Babel configuration, you can pass these options:

  • packageName string - Default: 'babel-plugin-transform-syntax-highlight/highlight'. The name of the package that you will require or import.
  • highlight 'highlightjs' | 'prism' - Default: 'highlight'. Choose the highlighter that you'd like to use, either highlight.js or Prism. Make sure you include CSS for the highlighter in your page.
  • delimiters [string, string] - Default: ['{#', '#}']. Delimiters for marking placeholders in the code that can later be replaced at runtime, either by props (in React mode) or function arguments (in HTML mode). If you don't use the default, make sure to choose delimiters that will not clash with the language of the code to be highlighted. And do not use < and >, which will be escaped by the syntax highlighter.

For example:

// .babelrc
{
  "plugins": [
    "transform-syntax-highlighting",
    {
      "packageName": "babel-highlighting",
      "highlight": "prism",
      "delimiters": ["$$", "$$"]
    }
  ]
}

Invocation options

When you invoke the html or react functions, the first argument can be an options object. That object can include the following:

  • language string - A language identifier that your highlighter of choice will understand. If no value is provided, highlight.js will try to guess the language. Prism will return an unhighlighted code.
  • highlight - Same as the Babel option, above. But overrides whatever Babel option has been set.
  • delimiters - Same as the Babel option, above. But overrides whatever Babel option has been set.

Notes

  • Interpolation happens at runtime, not before syntax highlighting. For this reason, your interpolations should only be strings or numbers. Strings and numbers should be correctly interpreted by syntax highlighters (I think).