GithubHelp home page GithubHelp logo

isabella232 / rehype-highlight-code-block Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mapbox/rehype-highlight-code-block

0.0 0.0 0.0 73 KB

rehype plugin that finds code blocks in HTML and lets you syntax-highlight them however you'd like to

License: MIT License

JavaScript 100.00%

rehype-highlight-code-block's Introduction

@mapbox/rehype-highlight-code-block

Build Status

rehype plugin that finds code blocks in HTML and lets you highlight them with a function.

Best suited for Node, because it includes an HTML parser, parse5, to parse the return value of your highlighting function. If you are doing your syntax highlighting in the browser, you might consider lowlight or refractor, both of which will work with rehype or other unified tools.

Installation

npm install @mapbox/rehype-highlight-code-block

API

Follow the directions for using rehype plugins.

rehype().use(rehypeHighlightCodeBlock, options)

options

highlight

Type: (code: string, lang?: string) => ?string. Required.

A function that accepts two arguments, code and lang, and returns a string of HTML representing code with syntax highlighting (or a falsy value if you don't want to highlight that block).

Every time a pre > code node is found, its contents are passed to options.highlight. The language passed to options.highlight with the code is determined by the first language- or lang- class found on the code node.

If options.highlight returns a falsy value, the code block will not be altered. If it returns an HTML string, that string will be parsed and inserted into the AST.

Usage

Use as a rehype plugin.

Some examples of how you might do that:

const rehype = require('rehype');
const rehypeHighlightCodeBlock = require('rehype-highlight-code-block');
const yourHighlightingFunction = require('../path/to/it');

rehype()
  .use(rehypeHighlightCodeBlock, {
    highlight: yourHighlightingFunction
  })
  .process(/* some html */);
const unified = require('unified');
const rehypeParse = require('rehype-parse');
const rehypeHighlightCodeBlock = require('rehype-highlight-code-block');
const yourHighlightingFunction = require('../path/to/it');

unified()
  .use(rehypeParse)
  .use(rehypeHighlightCodeBlock, {
    highlight: yourHighlightingFunction
  })
  .processSync(/* some html */);

If you'd like to get syntax highlighting in Markdown, parse the Markdown (with remark-parse), convert it to rehype, then use this plugin.

const unified = require('unified');
const remarkParse = require('remark-parse');
const remarkRehype = require('remark-rehype');
const rehypeHighlightCodeBlock = require('rehype-highlight-code-block');
const yourHighlightingFunction = require('../path/to/it');

unified()
  .use(remarkParse)
  .use(remarkRehype)
  .use(rehypeHighlightCodeBlock, {
    highlight: yourHighlightingFunction
  })
  .process(/* some markdown */);

rehype-highlight-code-block's People

Contributors

davidtheclark avatar lshig avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.