Giter VIP home page Giter VIP logo

gh-highlight's Introduction

gh-highlight

Highlight code like github.

This project is the synthesis of a couple of other projects:

  • github/linguist has information on language detection, has scripts for collecting all of the grammars used for highlighting.
  • atom/highlights is a syntax highlighter that can use TextMate's grammar format to highlight code.

Usage

The API was designed for highlighting fenced code blocks in markdown. For a list of names that are usable, check out aliases.json. The keys are the names you can use, the values are the scope name of the grammar that will be used to highlight.

Two functions are exposed; highlight and highlight.sync. They both take the code-to-be-highlighted as the first paramater and the language-infront-of-the-code-block as the second. For the top level version, the last parameter needs to be a callback.

If the language doesn't point to any grammar, no error will be reported.

const highlight = require('gh-highlight');

const html = highlight.sync('var hello = "world";', 'js');
console.log(html);

Outputs

<pre class="editor editor-colors">
  <div class="line">
    <span class="source js">
      <span class="storage modifier js"><span>var</span></span>
      <span>&nbsp;hello&nbsp;</span>
      <span class="keyword operator js"><span>=</span></span>
      <span>&nbsp;</span>
      <span class="string quoted double js">
        <span class="punctuation definition string begin js"><span>&quot;</span></span>
        <span>world</span>
        <span class="punctuation definition string end js"><span>&quot;</span></span>
      </span>
      <span class="punctuation terminator statement js"><span>;</span></span>
    </span>
  </div>
</pre>

Themes

For now I'm just going to defer to atom/highlights, as the process for creating a css file from an atom theme should be exactly the same. You can download these prebuilt css files if you don't want to bother generating them from scratch.

Development

Development is not very user friendly at this point, but this is the initial commit chill out. Here's roughly how to build linguist's grammars on a fresh xenial box.

sudo apt-get update
sudo apt-get install git subversion ruby ruby-dev ruby-bundler curl nodejs npm cmake pkg-config libicu-dev
sudo ln -s "$(which nodejs)" /usr/bin/node
git clone https://github.com/github/linguist.git
cd linguist
./script/bootstrap
./script/convert-grammars

Roadmap

  • Complete basic functionality.
  • Add basic tests.
  • Finish packaging for npm and publish.
  • Load grammars lazily.
  • Minify the grammars so that the package's download size is smaller. Should remove unnecessary keys, comments, disabled rules, unused rules, whitespace.
  • Compile code to support older node versions.
  • Make development process more user friendly, add more documentation.
  • Add pre-built common themes to this package.
  • Add playground and visual examples.
  • Continuous integration.
  • Consider including a tool for generating themes.
  • Consider convention-based extension method for adding grammars.

gh-highlight's People

Contributors

heyimalex avatar yomed avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

yomed

gh-highlight's Issues

update to latest linguist

Hi, I was happy to find your repo, as I couldn't find any other code highlighting packages that use linguist. Would you consider updating this to use the latest version of linguist? I'd like to use this with marko, which was added after your last update: github-linguist/linguist#3519

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.