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

remark-tree-sitter

v1.0.3

Published

Highlight code in Markdown files using tree-sitter and remark

Downloads

58

Readme

remark-tree-sitter

Build Status Total alerts Language grade: JavaScript

Highlight code in Markdown files using tree-sitter and remark. Powered by tree-sitter-hast.

Installation

npm install remark-tree-sitter

or

yarn add remark-tree-sitter

Usage

This plugin uses the same mechanism and data as Atom for syntax highlighting, So to highlight a particular language, you need to either:

For more information on how this mechanism works, check out the documentation for tree-sitter-hast.

Any code blocks that are encountered for which there is not a matching language will be ignored.

Example

The following example is also in the examples directory and can be run directly from there. It uses @atom-languages/language-typescript to provide the TypeScript grammar and

npm install to-vfile vfile-reporter remark remark-tree-sitter remark-html @atom-languages/language-typescript

examples/example.js

const vfile = require('to-vfile')
const report = require('vfile-reporter')
const remark = require('remark')
const treeSitter = require('remark-tree-sitter')
const html = require('remark-html')

remark()
  .use(treeSitter, {
    grammarPackages: ['@atom-languages/language-typescript']
  })
  .use(html)
  .process(vfile.readSync('example.md'), (err, file) => {
    console.error(report(err || file))
    console.log(String(file))
  })

Output:

example.md: no issues found
<pre><code class="tree-sitter language-typescript"><span class="source ts"><span class="storage type function">function</span> <span class="entity name function">foo</span><span class="punctuation definition parameters begin bracket round">(</span><span class="punctuation definition parameters end bracket round">)</span> <span class="punctuation definition function body begin bracket curly">{</span>
  <span class="keyword control">return</span> <span class="constant numeric">1</span><span class="punctuation terminator statement semicolon">;</span>
<span class="punctuation definition function body end bracket curly">}</span></span></code></pre>

Atom language packages

To use an Atom language package, like any package you first need to install it using npm install or yarn add. Unfortunately most APM packages are not made available on NPM, so I've started to make some of them available under the NPM organization @atom-languages. Here's a list of packages with which languages they provide highlighting for.

API

remark.use(treeSitter, options)

Note that options is required, and either grammarPackages or grammars needs to be provided. (Both can be provided, and grammars specified in grammars will overide those loaded in grammarPackages).

options.grammarPackages

An array of all Atom language packages that should be loaded.

Example:

remark().use(treeSitter, {
    grammarPackages: ['@atom-languages/language-typescript']
  })

The language names that code blocks must then use to refer to a language is based on the filenames in the atom package. For example the above package has the files: tree-sitter-flow.cson, tree-sitter-tsx.cson, tree-sitter-typescript.cson... so this will make the languages flow, tsx and typescript available for use within code blocks.

If you want to make loaded languages available to use via different names, you can use options.languageAliases.

options.grammars

An object mapping language keys objects containing grammar and scopeMappings.

Anything specified here will overwrite the languages loaded by options.grammarPackages.

For more information on scopeMappings, check out the documentation for tree-sitter-hast.

Example:

See a working example at examples/example-grammars.js.

remark().use(treeSitter, {
    grammars: {
      typescript: {
        grammar: typescriptGrammar,
        scopeMappings: typescriptScopeMappings
      },
      'custom-language': {
        grammar: customLanguageGrammar,
        scopeMappings: customLanguageScopeMappings
      }
    }
  })

You can then use both the typescript and custom-language languages in code blocks:

```custom-language
some code
```

```typescript
let foo = 'bar';
```

If you want to make loaded languages available to use via different names, you can use options.languageAliases.

options.classWhitelist

Sometimes including the full list of classes applied by the scope mappings can be too much, and you'd like to only include those that you have stylesheets for.

To do this, you can pass in a whitelist of classes that you actually care about.

Example: The following configuration...

remark().use(treeSitter, {
    grammarPackages: ['@atom-languages/language-typescript'],
    classWhitelist: ['storage', 'numeric']
  })

...will convert the following markdown...

```typescript
function foo() {
  return 1;
}
```

...to this:

<pre><code class="tree-sitter language-typescript"><span><span class="storage">function</span> foo() {
  return <span class="numeric">1</span>;
}</span></code></pre>

options.languageAliases

TODO: options.languageAliases is not implemented yet

TODO:

  • Add unit tests for grammars option