Coder Social home page Coder Social logo

posthtml / posthtml-highlight Goto Github PK

View Code? Open in Web Editor NEW
9.0 4.0 1.0 1.3 MB

:art: Syntax Highlighting for PostHTML

License: Do What The F*ck You Want To Public License

TypeScript 75.24% HTML 22.34% Shell 2.41%
posthtml posthtml-plugin highlight highlightjs syntax-highlighting syntax-highlighter

posthtml-highlight's Introduction

PostHTML Highlight Plugin

Version License TypeScript Build Status Coverage Downloads Chat

Compile-time syntax highlighting for code blocks via highlight.js

Before:

<pre><code>
  const foo = 'foo'
  console.log(foo)
</code></pre>

After:

<pre><code class="hljs">
  <span class="hljs-keyword">const</span> foo = <span class="hljs-string">'foo'</span>
  console.<span class="hljs-built_in">log</span>(foo)
</code></pre>

Install

$ yarn add -D posthtml posthtml-highlight

or

$ npm i posthtml posthtml-highlight

If using TypeScript, additionally install @types/highlight.js

Usage

const fs = require('fs')
const posthtml = require('posthtml')
const highlight = require('posthtml-highlight')

const source = fs.readFileSync('./before.html')

posthtml([
  highlight(
    /* optional */ {
      /**
       * By default, only code tags wrapped in pre tags are highlighted (i.e. <pre><code><code/><pre/>)
       *
       * Set `inline: true` to highlight all code tags
       */
      inline: true,

      /**
       * You may also pass any highlight.js options (http://highlightjs.readthedocs.io/en/latest/api.html#configure-options)
       */
      useBR: true,
    }
  ),
])
  .process(source)
  .then((result) => fs.writeFileSync('./after.html', result.html))

Styling

You will also need to include a highlight.js stylesheet

View the available color schemes here, then
a) include via a CDN
b) install via npm (yarn add -D highlight.js, ./node_modules/highlight.js/styles/*)
c) download via the highlight.js repo

Specifying a language

Specifying a language as per highlight.js's usage docs is supported, with the caveat that you must use the lang-* or language-* prefix

Skip highlighting on a node

Add the nohighlight class as per highlight.js's usage docs

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.