Skip to content

Commit

Permalink
Update README for syntax highlighting rules
Browse files Browse the repository at this point in the history
  • Loading branch information
gjtorikian committed Dec 2, 2023
1 parent fef7d87 commit df7c24e
Showing 1 changed file with 53 additions and 17 deletions.
70 changes: 53 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,25 @@ providing further niceties.

#### Syntax Highlighter Plugin

The library comes with [a set of pre-existing themes](https://docs.rs/syntect/5.0.0/syntect/highlighting/struct.ThemeSet.html#implementations) for highlighting code:

- `"base16-ocean.dark"`
- `"base16-eighties.dark"`
- `"base16-mocha.dark"`
- `"base16-ocean.light"`
- `"InspiredGitHub"`
- `"Solarized (dark)"`
- `"Solarized (light)"`

````ruby
code = <<~CODE
```ruby
def hello
puts "hello"
puts "hello"
end
CODE

# pass in a theme name from a pre-existing set
puts Commonmarker.to_html(code, plugins: { syntax_highlighter: { theme: "InspiredGitHub" } })

# <pre style="background-color:#ffffff;" lang="ruby"><code>
Expand All @@ -123,32 +134,42 @@ puts Commonmarker.to_html(code, plugins: { syntax_highlighter: { theme: "Inspire
# </code></pre>
````

To disable this plugin, pass `nil`:
By default, the plugin uses the `"base16-ocean.dark"` theme to syntax highlight code.

To disable this plugin, set the value to `nil`:

```ruby
Commonmarker.to_html(code, plugins: { syntax_highlighter: nil })
# or
Commonmarker.to_html(code, plugins: { syntax_highlighter: { theme: nil } })

# <pre lang="ruby"><code>def hello
# puts &quot;hello&quot;
# end
# </code></pre>
```

You can also provide a `path` to a directory containing `.tmtheme` files to load:
To output CSS classes instead of `style` attributes, set the `theme` key to `""`:

```ruby
````ruby
code = <<~CODE
```ruby
def hello
puts "hello"
end
CODE

Commonmarker.to_html(code, plugins: { syntax_highlighter: { theme: "Monokai", path: "./themes" } })
```
Commonmarker.to_html(code, plugins: { syntax_highlighter: { theme: "" } })

##### Available themes
# <pre class="syntax-highlighting"><code><span class="source ruby"><span class="meta function ruby"><span class="keyword control def ruby">def</span></span><span class="meta function ruby"> # <span class="entity name function ruby">hello</span></span>
# <span class="support function builtin ruby">puts</span> <span class="string quoted double ruby"><span class="punctuation definition string begin ruby">&quot;</span>hello<span class="punctuation definition string end ruby">&quot;</span></span>
# <span class="keyword control ruby">end</span>\n</span></code></pre>
````

Here's [a list of themes available by default](https://docs.rs/syntect/5.0.0/syntect/highlighting/struct.ThemeSet.html#implementations):
To use a custom theme, you can provide a `path` to a directory containing `.tmtheme` files to load:

- `"base16-ocean.dark"`
- `"base16-eighties.dark"`
- `"base16-mocha.dark"`
- `"base16-ocean.light"`
- `"InspiredGitHub"`
- `"Solarized (dark)"`
- `"Solarized (light)"`
```ruby

Commonmarker.to_html(code, plugins: { syntax_highlighter: { theme: "Monokai", path: "./themes" } })
```

## Output formats

Expand Down Expand Up @@ -198,3 +219,18 @@ Comparison:
commonmarker with to_html: 5.8 i/s - 3.84x (± 0.00) slower
kramdown: 0.3 i/s - 68.30x (± 0.00) slower
```

```
```

````
```
```
```
```
````

0 comments on commit df7c24e

Please sign in to comment.