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 = ; const html = highlight;console;
Outputs
var hello = " world " ;
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 updatesudo apt-get install git subversion ruby ruby-dev ruby-bundler curl nodejs npm cmake pkg-config libicu-devsudo ln -s "$(which nodejs)" /usr/bin/nodegit clone https://github.com/github/linguist.gitcd 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.