commonmarker 2.10.1 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4d5e959f519ad6befde6dac13c3d66d8df0eb8a5607553466d49544d75fb53dd
4
- data.tar.gz: c4b1fa07305b5781a5a4ddb94090e474221df13ca946724068407b925d7c7e57
3
+ metadata.gz: 2ef88a951e4a005de5220544f5187b5c6cbfdfef756163aa1ff8a32575fab8a5
4
+ data.tar.gz: c1250df6112b1ca7831a6018e5195d9a5210d5bf888e1f748ad1336b775a95ef
5
5
  SHA512:
6
- metadata.gz: 0e91a96c44c65c948f9e4631ca57f4e0a735f6e23ca5f8eb76e8d5a1ae7f5909b102712501a3a01abc25109e18830333f7557ed676a87bd1b458bdc6f0ed61a3
7
- data.tar.gz: 858617c6e1444fafaa3fa8b70ee128ed4ad4fe9e5802ff1155adb2102a209cc53d4721ae85c78392fc2f26a7b8c386bcdd6a84cc7e0e38411ddae389bb06e87e
6
+ metadata.gz: 3918c8058c944a65d0be5ef1e2f316dac4892a8bce3a7f85ce95e0b48dbc3a478708d351a805642ecb7bff55e9c78680513e6dbc459efa321f95d2c72ea01957
7
+ data.tar.gz: 239b5654ea577f53c4c72492d0312357278c0276e94fb63713b7589c26e37e57283e8d9bcf21c66fd799a56caf94ecb63658149c1901db1e92204c903ebd11c6
data/Cargo.lock CHANGED
@@ -130,9 +130,9 @@ dependencies = [
130
130
 
131
131
  [[package]]
132
132
  name = "comrak"
133
- version = "0.56.0"
133
+ version = "1.2.0"
134
134
  source = "registry+https://github.com/rust-lang/crates.io-index"
135
- checksum = "2544c4d6b6da7d54e95ac2833b464bf3dffa0666237868f2bf4ec5e11c9ba8cc"
135
+ checksum = "a253d04a12dff46b9ff641890e76bad5f6475afa891faac93d736a04fed5804e"
136
136
  dependencies = [
137
137
  "caseless",
138
138
  "emojis",
@@ -318,9 +318,9 @@ checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f"
318
318
 
319
319
  [[package]]
320
320
  name = "magnus"
321
- version = "0.9.3"
321
+ version = "0.9.4"
322
322
  source = "registry+https://github.com/rust-lang/crates.io-index"
323
- checksum = "40ef424404d166cbb71c8b8d19c9c589c748c4547c1afa1928c5504299f6c713"
323
+ checksum = "3c5f2dd2017d172e2fde9969a35f10f1374a4dd79b37a17f1847f83201883de7"
324
324
  dependencies = [
325
325
  "magnus-macros",
326
326
  "rb-sys",
data/README.md CHANGED
@@ -2,10 +2,17 @@
2
2
 
3
3
  Ruby wrapper for Rust's [comrak](https://github.com/kivikakk/comrak) crate.
4
4
 
5
- It passes all of the CommonMark test suite, and is therefore spec-complete. It also includes extensions to the CommonMark spec as documented in the [GitHub Flavored Markdown spec](http://github.github.com/gfm/), such as support for tables, strikethroughs, and autolinking.
5
+ It uses comrak's defaults out of the box, which comrak documents as compliant with [CommonMark](https://spec.commonmark.org). It also offers comrak's extensions to the spec, including the ones in the [GitHub Flavored Markdown spec](http://github.github.com/gfm/), such as tables, strikethrough, and autolinking.
6
6
 
7
7
  > [!NOTE]
8
- > By default, the following extensions are enabled for end user convenience: `strikethrough`, `table`, `autolink`, `tasklist` (all from the [GFM spec](http://github.github.com/gfm/)), and `shortcodes`. The `syntax_highlighter` plugin is also enabled by default, using the `"base16-ocean.dark"` theme.
8
+ > Commonmarker's defaults are comrak's: plain CommonMark, with no extensions and no plugins enabled. For GitHub-style output, turn on the GFM extensions and rendering quirks:
9
+ >
10
+ > ```ruby
11
+ > Commonmarker.to_html(text, options: {
12
+ > extension: { strikethrough: true, table: true, autolink: true, tasklist: true },
13
+ > render: { github_pre_lang: true, gfm_quirks: true },
14
+ > })
15
+ > ```
9
16
  >
10
17
  > For more information on the available options and extensions, see [the documentation below](#options-and-plugins).
11
18
 
@@ -41,6 +48,43 @@ Commonmarker.to_html('"Hi *there*"', options: {
41
48
 
42
49
  (The second argument is optional--[see below](#options-and-plugins) for more information.)
43
50
 
51
+ ### Reusing a configuration
52
+
53
+ `Commonmarker.to_html` and `Commonmarker.parse` process their `options` and `plugins` on every call. When you convert many documents with the same settings, build a `Commonmarker` instance once and call `to_html` or `parse` on it instead:
54
+
55
+ ```ruby
56
+ require 'commonmarker'
57
+
58
+ markdown = Commonmarker.new(options: { parse: { smart: true } }, plugins: { syntax_highlighter: { theme: "InspiredGitHub" } })
59
+
60
+ markdown.to_html('"Hi *there*"')
61
+ # => <p>“Hi <em>there</em>”</p>\n
62
+
63
+ doc = markdown.parse("*Hello* world")
64
+ doc.to_html
65
+ # => <p><em>Hello</em> world</p>\n
66
+ ```
67
+
68
+ The options and plugins are validated and processed when the instance is created, so invalid values raise from `Commonmarker.new` rather than from the first conversion, and changing the hashes afterwards has no effect on the instance. Instances hold no mutable state, so one can live in a constant and be shared across threads. `Commonmarker.to_html(text, options: o, plugins: p)` is the same as `Commonmarker.new(options: o, plugins: p).to_html(text)`.
69
+
70
+ `to_html` and `to_commonmark` also accept a `Commonmarker::Node`, so a document you parsed and then modified renders with the same settings without passing them again:
71
+
72
+ ```ruby
73
+ doc = markdown.parse("*Hello* [GitHub](https://www.github.com)")
74
+
75
+ # Transform links to regular text
76
+ doc.walk do |node|
77
+ node.replace(node.first_child) if node.type == :link
78
+ end
79
+
80
+ markdown.to_html(doc)
81
+ # => <p><em>Hello</em> GitHub</p>\n
82
+ markdown.to_commonmark(doc)
83
+ # => *Hello* GitHub\n
84
+ ```
85
+
86
+ `Node#to_html` and `Node#to_commonmark` keep their own `options:` and `plugins:` arguments for one-off renders.
87
+
44
88
  ### Generating a document
45
89
 
46
90
  You can also parse a string to receive a `:document` node. You can then print that node to HTML, iterate over the children, and do other fun node stuff. For example:
@@ -115,7 +159,7 @@ doc.walk do |node|
115
159
  node.delete
116
160
  end
117
161
  end
118
- # => <h1><a href=\"#the-site\"></a>The site</h1>\n<p>GitHub</p>\n
162
+ # => <h1>The site</h1>\n<p>GitHub</p>\n
119
163
  ```
120
164
 
121
165
  #### Example: Converting a document back into raw CommonMark
@@ -158,6 +202,28 @@ end
158
202
 
159
203
  Note that a `:frontmatter` node's literal includes its delimiters and trailing newlines, so anything you assign must include them too.
160
204
 
205
+ ### Node attributes
206
+
207
+ With any of the `*_attributes` extensions enabled, comrak parses Pandoc-style attribute blocks such as `{#intro .big data-x=1}` off the end of headings, fenced code blocks, inline code, links and images. `attributes` reads them as a Hash of `id`, `classes` and `pairs`, or `nil` when a node has none; `attributes=` replaces them, and accepts `nil` to clear them:
208
+
209
+ ```ruby
210
+ doc = Commonmarker.parse("## Intro {#intro .big data-x=1}", options: { extension: { header_attributes: true } })
211
+ doc.first_child.attributes
212
+ # => {id: "intro", classes: ["big"], pairs: {"data-x" => "1"}}
213
+
214
+ doc.first_child.attributes = { classes: ["lede"], pairs: { "data-x" => "2" } }
215
+ ```
216
+
217
+ Attributes only reach the HTML when the `attributes` render option is on **and** `unsafe` is on, because an attribute block can smuggle in event handlers and the like. If you render untrusted Markdown, leave `attributes` off and apply an allowlist of the parsed values yourself:
218
+
219
+ ```ruby
220
+ Commonmarker.to_html("## Intro {#intro .big}", options: {
221
+ extension: { header_attributes: true, header_ids: nil },
222
+ render: { attributes: true, unsafe: true },
223
+ })
224
+ # => <h2 id="intro" class="big">Intro</h2>
225
+ ```
226
+
161
227
  ## Options and plugins
162
228
 
163
229
  ### Options
@@ -165,86 +231,101 @@ Note that a `:frontmatter` node's literal includes its delimiters and trailing n
165
231
  Commonmarker accepts the same parse, render, and extensions options that comrak does, as a hash dictionary with symbol keys:
166
232
 
167
233
  ```ruby
168
- Commonmarker.to_html('"Hi *there*"', options:{
234
+ Commonmarker.to_html('"Hi *there*"', options: {
169
235
  parse: { smart: true },
170
- render: { hardbreaks: false}
236
+ render: { hardbreaks: true }
171
237
  })
172
238
  ```
173
239
 
174
- Note that there is a distinction in comrak for "parse" options and "render" options, which are represented in the tables below. As well, if you wish to disable any-non boolean option, pass in `nil`.
240
+ Note that there is a distinction in comrak for "parse" options and "render" options, which are represented in the tables below. The defaults are comrak's. Passing `nil` for any option means "use the default", which for the string-valued options (`default_info_string`, `header_ids`, and `front_matter_delimiter`) means leaving that feature off.
175
241
 
176
242
  ### Parse options
177
243
 
178
- | Name | Description | Default |
179
- | ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
180
- | `smart` | Punctuation (quotes, full-stops and hyphens) are converted into 'smart' punctuation. | `false` |
181
- | `default_info_string` | The default info string for fenced code blocks. | `""` |
182
- | `relaxed_tasklist_matching` | Enables relaxing of the tasklist extension matching, allowing any non-space to be used for the "checked" state instead of only `x` and `X`. | `false` |
183
- | `relaxed_autolinks` | Enable relaxing of the autolink extension parsing, allowing links to be recognized when in brackets, as well as permitting any url scheme. | `false` |
184
- | `leave_footnote_definitions` | Allow footnote definitions to remain in their original positions instead of being moved to the document's end (only affects AST) | `false` |
185
- | `ignore_setext` | Ignores setext-style headings. | `false` |
186
- | `sourcepos_chars` | Use character-based column tracking in source positions instead of byte-based. Relevant for multi-byte UTF-8 documents with `sourcepos`. | `false` |
187
- | `max_nesting_depth` | The deepest a node may nest below the document. Deeper documents raise `Commonmarker::NestingDepthExceededError` instead of being parsed. | `1000` |
244
+ | Name | Description | Default |
245
+ | ---------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
246
+ | `smart` | Punctuation (quotes, full-stops and hyphens) are converted into 'smart' punctuation. | `false` |
247
+ | `default_info_string` | The default info string for fenced code blocks. | `nil` |
248
+ | `relaxed_tasklist_matching` | Enables relaxing of the tasklist extension matching, allowing any non-space to be used for the "checked" state instead of only `x` and `X`. | `false` |
249
+ | `relaxed_autolinks` | Enable relaxing of the autolink extension parsing, allowing links to be recognized when in brackets, as well as permitting any url scheme. | `false` |
250
+ | `leave_footnote_definitions` | Allow footnote definitions to remain in their original positions instead of being moved to the document's end (only affects AST) | `false` |
251
+ | `ignore_setext` | Ignores setext-style headings. | `false` |
252
+ | `sourcepos_chars` | Use character-based column tracking in source positions instead of byte-based. Relevant for multi-byte UTF-8 documents with `sourcepos`. | `false` |
253
+ | `max_nesting_depth` | The deepest a node may nest below the document. Deeper documents raise `Commonmarker::NestingDepthExceededError` instead of being parsed. | `1000` |
254
+ | `tasklist_in_table` | Allows a task list item to be the sole content of a table cell. Needs the `table` and `tasklist` extensions. | `false` |
255
+ | `escaped_char_spans` | Keeps [escaped characters](https://spec.commonmark.org/0.31.2/#backslash-escapes) as `escaped` nodes in the AST. Implied by the render option of the same name. | `false` |
188
256
 
189
257
  ### Render options
190
258
 
191
- | Name | Description | Default |
192
- | -------------------- | ------------------------------------------------------------------------------------------------------------------------ | ------------ |
193
- | `hardbreaks` | [Soft line breaks](http://spec.commonmark.org/0.27/#soft-line-breaks) translate into hard line breaks. | `true` |
194
- | `github_pre_lang` | GitHub-style `<pre lang="xyz">` is used for fenced code blocks with info tags. | `true` |
195
- | `full_info_string` | Gives info string data after a space in a `data-meta` attribute on code blocks. | `false` |
196
- | `width` | The wrap column when outputting CommonMark. | `80` |
197
- | `unsafe` | Allow rendering of raw HTML and potentially dangerous links. | `false` |
198
- | `escape` | Escape raw HTML instead of clobbering it. | `false` |
199
- | `sourcepos` | Include source position attribute in HTML and XML output. | `false` |
200
- | `escaped_char_spans` | Wrap escaped characters in span tags. | `true` |
201
- | `ignore_empty_links` | Ignores empty links, leaving the Markdown text in place. | `false` |
202
- | `gfm_quirks` | Outputs HTML with GFM-style quirks; namely, not nesting `<strong>` inlines. | `false` |
203
- | `prefer_fenced` | Always output fenced code blocks, even where an indented one could be used. | `false` |
204
- | `tasklist_classes` | Add CSS classes to the HTML output of the tasklist extension | `false` |
205
- | `compact_html` | Suppress newlines in pretty-printed HTML output. | `false` |
206
- | `alert_style` | The style of alert output: `"specific"` (`<div class="markdown-alert">`) or `"semantic"` (`<aside class="admonition">`). | `"specific"` |
259
+ | Name | Description | Default |
260
+ | ---------------------------------- | --------------------------------------------------------------------------------------------------------------------------- | ----------- |
261
+ | `hardbreaks` | [Soft line breaks](http://spec.commonmark.org/0.27/#soft-line-breaks) translate into hard line breaks. | `false` |
262
+ | `github_pre_lang` | GitHub-style `<pre lang="xyz">` is used for fenced code blocks with info tags. | `false` |
263
+ | `full_info_string` | Gives info string data after a space in a `data-meta` attribute on code blocks. | `false` |
264
+ | `width` | The wrap column when outputting CommonMark. `0` means no wrapping. | `0` |
265
+ | `unsafe` | Allow rendering of raw HTML and potentially dangerous links. | `false` |
266
+ | `escape` | Escape raw HTML instead of clobbering it. | `false` |
267
+ | `sourcepos` | Include source position attribute in HTML and XML output. | `false` |
268
+ | `escaped_char_spans` | Wrap escaped characters in span tags. | `false` |
269
+ | `ignore_empty_links` | Ignores empty links, leaving the Markdown text in place. | `false` |
270
+ | `gfm_quirks` | Outputs HTML with GFM-style quirks; namely, not nesting `<strong>` inlines. | `false` |
271
+ | `prefer_fenced` | Always output fenced code blocks, even where an indented one could be used. | `false` |
272
+ | `tasklist_classes` | Add CSS classes to the HTML output of the tasklist extension | `false` |
273
+ | `compact_html` | Suppress newlines in pretty-printed HTML output. | `false` |
274
+ | `alert_style` | The style of alert output: `:specific` (`<div class="markdown-alert">`) or `:semantic` (`<aside class="admonition">`). | `:specific` |
275
+ | `list_style` | The bullet list marker used when outputting CommonMark: `:dash` (`-`), `:plus` (`+`) or `:star` (`*`). | `:dash` |
276
+ | `figure_with_caption` | Renders an image that has a title as a `<figure>`, with the title as its `<figcaption>`. | `false` |
277
+ | `ol_width` | The minimum ordered list marker width when outputting CommonMark. Values below 3 have no effect. | `0` |
278
+ | `experimental_minimize_commonmark` | Drops escapes from CommonMark output when the document still roundtrips without them. Expensive. | `false` |
279
+ | `attributes` | Writes parsed or assigned node attributes into the HTML. Does nothing without `unsafe`, since attributes can carry scripts. | `false` |
207
280
 
208
281
  As well, there are several extensions which you can toggle in the same manner:
209
282
 
210
283
  ```ruby
211
284
  Commonmarker.to_html('"Hi *there*"', options: {
212
- extension: { footnotes: true, description_lists: true },
213
- render: { hardbreaks: false }
285
+ extension: { footnotes: true, description_lists: true }
214
286
  })
215
287
  ```
216
288
 
217
289
  ### Extension options
218
290
 
219
- | Name | Description | Default |
220
- | ----------------------------- | ------------------------------------------------------------------------------------------------------------------- | ------- |
221
- | `strikethrough` | Enables the [strikethrough extension](https://github.github.com/gfm/#strikethrough-extension-) from the GFM spec. | `true` |
222
- | `table` | Enables the [table extension](https://github.github.com/gfm/#tables-extension-) from the GFM spec. | `true` |
223
- | `autolink` | Enables the [autolink extension](https://github.github.com/gfm/#autolinks-extension-) from the GFM spec. | `true` |
224
- | `tasklist` | Enables the [task list extension](https://github.github.com/gfm/#task-list-items-extension-) from the GFM spec. | `true` |
225
- | `superscript` | Enables the superscript Comrak extension. | `false` |
226
- | `header_ids` | Enables the header IDs Comrak extension. from the GFM spec. | `""` |
227
- | `header_id_prefix_in_href` | Also add the prefix to generated `href` attributes pointing to headers. | `false` |
228
- | `footnotes` | Enables the footnotes extension per `cmark-gfm`. | `false` |
229
- | `inline_footnotes` | Enables the inline footnotes extension. | `false` |
230
- | `description_lists` | Enables the description lists extension. | `false` |
231
- | `front_matter_delimiter` | Enables the front matter extension. | `""` |
232
- | `multiline_block_quotes` | Enables the multiline block quotes extension. | `false` |
233
- | `math_dollars`, `math_code` | Enables the math extension. | `false` |
234
- | `math_latex` | Enables the math extension with LaTeX-style delimiters (`\(inline\)`, `\[display\]`). | `false` |
235
- | `shortcodes` | Enables the shortcodes extension. | `true` |
236
- | `wikilinks_title_before_pipe` | Enables the wikilinks extension, placing the title before the dividing pipe. | `false` |
237
- | `wikilinks_title_after_pipe` | Enables the wikilinks extension, placing the title after the dividing pipe. | `false` |
238
- | `underline` | Enables the underline extension. | `false` |
239
- | `spoiler` | Enables the spoiler extension. | `false` |
240
- | `greentext` | Enables the greentext extension. | `false` |
241
- | `subtext` | Enables the subtext extension. | `false` |
242
- | `subscript` | Enables the subscript extension. | `false` |
243
- | `alerts` | Enables the alerts extension. | `false` |
244
- | `cjk_friendly_emphasis` | Enables the [CJK friendly emphasis](https://github.com/tats-u/markdown-cjk-friendly) extension. | `false` |
245
- | `highlight` | Enables highlighting via `==` | `false` |
246
- | `insert` | Enables the insert extension, rendering `++text++` as `<ins>text</ins>`. | `false` |
247
- | `block_directive` | Enables the block directive extension. | `false` |
291
+ | Name | Description | Default |
292
+ | ----------------------------- | ----------------------------------------------------------------------------------------------------------------- | ------- |
293
+ | `strikethrough` | Enables the [strikethrough extension](https://github.github.com/gfm/#strikethrough-extension-) from the GFM spec. | `false` |
294
+ | `table` | Enables the [table extension](https://github.github.com/gfm/#tables-extension-) from the GFM spec. | `false` |
295
+ | `autolink` | Enables the [autolink extension](https://github.github.com/gfm/#autolinks-extension-) from the GFM spec. | `false` |
296
+ | `tasklist` | Enables the [task list extension](https://github.github.com/gfm/#task-list-items-extension-) from the GFM spec. | `false` |
297
+ | `superscript` | Enables the superscript Comrak extension. | `false` |
298
+ | `header_ids` | Enables the header IDs Comrak extension; the value is prefixed to every generated ID. Pass `""` for no prefix. | `nil` |
299
+ | `header_id_prefix_in_href` | Also add the prefix to generated `href` attributes pointing to headers. | `false` |
300
+ | `footnotes` | Enables the footnotes extension per `cmark-gfm`. | `false` |
301
+ | `inline_footnotes` | Enables the inline footnotes extension. | `false` |
302
+ | `description_lists` | Enables the description lists extension. | `false` |
303
+ | `front_matter_delimiter` | Enables the front matter extension; the value is the delimiter line, such as `"---"`. | `nil` |
304
+ | `multiline_block_quotes` | Enables the multiline block quotes extension. | `false` |
305
+ | `math_dollars`, `math_code` | Enables the math extension. | `false` |
306
+ | `math_latex` | Enables the math extension with LaTeX-style delimiters (`\(inline\)`, `\[display\]`). | `false` |
307
+ | `shortcodes` | Enables the shortcodes extension, which turns `:smile:` and friends into emoji. | `false` |
308
+ | `wikilinks_title_before_pipe` | Enables the wikilinks extension, placing the title before the dividing pipe. | `false` |
309
+ | `wikilinks_title_after_pipe` | Enables the wikilinks extension, placing the title after the dividing pipe. | `false` |
310
+ | `underline` | Enables the underline extension. | `false` |
311
+ | `spoiler` | Enables the spoiler extension. | `false` |
312
+ | `greentext` | Enables the greentext extension. | `false` |
313
+ | `subtext` | Enables the subtext extension. | `false` |
314
+ | `subscript` | Enables the subscript extension. | `false` |
315
+ | `alerts` | Enables the alerts extension. | `false` |
316
+ | `cjk_friendly_emphasis` | Enables the [CJK friendly emphasis](https://github.com/tats-u/markdown-cjk-friendly) extension. | `false` |
317
+ | `highlight` | Enables highlighting via `==` | `false` |
318
+ | `insert` | Enables the insert extension, rendering `++text++` as `<ins>text</ins>`. | `false` |
319
+ | `block_directive` | Enables the block directive extension. | `false` |
320
+ | `math_dollars_block` | Enables multiline display math blocks delimited by `$$` lines. Requires `math_dollars`. | `false` |
321
+ | `wikilinks_ignore_embeds` | Ignores wikilink-style embeds (`![[url]]`) when a wikilinks extension is enabled. | `false` |
322
+ | `ofm_callouts` | Enables [Obsidian-style callouts](https://help.obsidian.md/callouts), such as `> [!tip]`. | `false` |
323
+ | `ofm_comments` | Enables Obsidian-style `%%comment%%` comments, which are dropped from the output. | `false` |
324
+ | `ofm_block_ids` | Enables Obsidian-style block identifiers (`^abc123` at the end of a block). | `false` |
325
+ | `header_attributes` | Parses `{#id .class key=value}` attribute blocks at the end of ATX and setext headings. | `false` |
326
+ | `fenced_code_attributes` | Parses attribute blocks in the info string of fenced code blocks. | `false` |
327
+ | `inline_code_attributes` | Parses attribute blocks directly after inline code spans. | `false` |
328
+ | `link_attributes` | Parses attribute blocks directly after links and images. | `false` |
248
329
 
249
330
  For more information on these options, see [the comrak documentation](https://github.com/kivikakk/comrak#usage).
250
331
 
@@ -255,17 +336,7 @@ providing further niceties.
255
336
 
256
337
  #### Syntax Highlighter Plugin
257
338
 
258
- The syntax highlighter plugin is **enabled by default**, using the `"base16-ocean.dark"` theme. It applies syntax highlighting to fenced code blocks that specify a language.
259
-
260
- 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:
261
-
262
- - `"base16-ocean.dark"`
263
- - `"base16-eighties.dark"`
264
- - `"base16-mocha.dark"`
265
- - `"base16-ocean.light"`
266
- - `"InspiredGitHub"`
267
- - `"Solarized (dark)"`
268
- - `"Solarized (light)"`
339
+ The syntax highlighter plugin applies syntax highlighting to fenced code blocks that specify a language. Like every plugin, it is **disabled by default**. Pass an empty Hash to enable it with the default `"base16-ocean.dark"` theme, or name a theme:
269
340
 
270
341
  ````ruby
271
342
  code = <<~CODE
@@ -276,52 +347,49 @@ code = <<~CODE
276
347
  ```
277
348
  CODE
278
349
 
279
- # pass in a theme name from a pre-existing set
350
+ # the default theme
351
+ puts Commonmarker.to_html(code, plugins: { syntax_highlighter: {} })
352
+
353
+ # a theme name from the pre-existing set
280
354
  puts Commonmarker.to_html(code, plugins: { syntax_highlighter: { theme: "InspiredGitHub" } })
281
355
 
282
- # <pre style="background-color:#ffffff;" lang="ruby"><code>
283
- # <span style="font-weight:bold;color:#a71d5d;">def </span><span style="font-weight:bold;color:#795da3;">hello
284
- # </span><span style="color:#62a35c;">puts </span><span style="color:#183691;">&quot;hello&quot;
356
+ # <pre style="background-color:#ffffff;"><code class="language-ruby"><span style="font-weight:bold;color:#a71d5d;">def </span><span style="font-weight:bold;color:#795da3;">hello
357
+ # </span><span style="color:#323232;"> </span><span style="color:#62a35c;">puts </span><span style="color:#183691;">&quot;hello&quot;
285
358
  # </span><span style="font-weight:bold;color:#a71d5d;">end
286
- # </span>
287
- # </code></pre>
359
+ # </span></code></pre>
288
360
  ````
289
361
 
290
- To disable this plugin, set the value to `nil`:
362
+ 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:
291
363
 
292
- ````ruby
293
- code = <<~CODE
294
- ```ruby
295
- def hello
296
- puts "hello"
297
- end
298
- ```
299
- CODE
364
+ - `"base16-ocean.dark"`
365
+ - `"base16-eighties.dark"`
366
+ - `"base16-mocha.dark"`
367
+ - `"base16-ocean.light"`
368
+ - `"InspiredGitHub"`
369
+ - `"Solarized (dark)"`
370
+ - `"Solarized (light)"`
371
+
372
+ Without the plugin, a fenced code block renders as plain CommonMark HTML. Passing `syntax_highlighter: nil` has the same effect, which is handy when the plugins Hash is built elsewhere:
300
373
 
374
+ ```ruby
375
+ Commonmarker.to_html(code)
301
376
  Commonmarker.to_html(code, plugins: { syntax_highlighter: nil })
302
377
 
303
- # <pre lang="ruby"><code>def hello
378
+ # <pre><code class="language-ruby">def hello
304
379
  # puts &quot;hello&quot;
305
380
  # end
306
381
  # </code></pre>
307
- ````
382
+ ```
308
383
 
309
384
  To output CSS classes instead of `style` attributes, set the `theme` key to `""`:
310
385
 
311
- ````ruby
312
- code = <<~CODE
313
- ```ruby
314
- def hello
315
- puts "hello"
316
- end
317
- CODE
318
-
386
+ ```ruby
319
387
  Commonmarker.to_html(code, plugins: { syntax_highlighter: { theme: "" } })
320
388
 
321
- # <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>
389
+ # <pre class="syntax-highlighting"><code class="language-ruby"><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>
322
390
  # <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>
323
391
  # <span class="keyword control ruby">end</span>\n</span></code></pre>
324
- ````
392
+ ```
325
393
 
326
394
  To use a custom theme, you can provide a `path` to a directory containing `.tmtheme` files to load:
327
395
 
@@ -329,6 +397,8 @@ To use a custom theme, you can provide a `path` to a directory containing `.tmth
329
397
  Commonmarker.to_html(code, plugins: { syntax_highlighter: { theme: "Monokai", path: "./themes" } })
330
398
  ```
331
399
 
400
+ Syntax highlighters are expensive to build, so Commonmarker builds one per `theme`/`path` combination and keeps it for the life of the process. Theme files in a custom `path` are read the first time that path is used; restart the process to pick up changes to them.
401
+
332
402
  ## Output formats
333
403
 
334
404
  Commonmarker can currently only generate output in one format: HTML.
@@ -356,30 +426,108 @@ If there were no errors, you're done! Otherwise, make sure to follow the comrak
356
426
 
357
427
  ```
358
428
  ❯ bundle exec rake benchmark
429
+ input size = 3789 bytes
430
+
431
+ ruby 3.4.0dev (2024-12-25 master f450108330) +PRISM [arm64-darwin24]
432
+ Warming up --------------------------------------
433
+ Markly.render_html 11.057k i/100ms
434
+ Markly::Node#to_html 10.873k i/100ms
435
+ Commonmarker.to_html 24.813k i/100ms
436
+ Commonmarker.to_html (no syntax highlighting)
437
+ 24.243k i/100ms
438
+ Commonmarker#to_html 27.063k i/100ms
439
+ Commonmarker::Node.to_html
440
+ 14.214k i/100ms
441
+ Kramdown::Document#to_html
442
+ 3.854k i/100ms
443
+ Calculating -------------------------------------
444
+ Markly.render_html 109.848k (± 2.8%) i/s (9.10 μs/i) - 552.850k in 5.036926s
445
+ Markly::Node#to_html 112.683k (± 3.0%) i/s (8.87 μs/i) - 565.396k in 5.022126s
446
+ Commonmarker.to_html 251.737k (± 3.6%) i/s (3.97 μs/i) - 1.265M in 5.033668s
447
+ Commonmarker.to_html (no syntax highlighting)
448
+ 240.071k (± 2.3%) i/s (4.17 μs/i) - 1.212M in 5.051940s
449
+ Commonmarker#to_html 273.099k (± 1.1%) i/s (3.66 μs/i) - 1.380M in 5.054553s
450
+ Commonmarker::Node.to_html
451
+ 142.769k (± 2.1%) i/s (7.00 μs/i) - 724.914k in 5.079875s
452
+ Kramdown::Document#to_html
453
+ 37.737k (± 1.4%) i/s (26.50 μs/i) - 188.846k in 5.005327s
454
+
455
+ Comparison:
456
+ Commonmarker#to_html: 273099.4 i/s
457
+ Commonmarker.to_html: 251736.9 i/s - 1.08x slower
458
+ Commonmarker.to_html (no syntax highlighting): 240070.7 i/s - 1.14x slower
459
+ Commonmarker::Node.to_html: 142769.5 i/s - 1.91x slower
460
+ Markly::Node#to_html: 112683.0 i/s - 2.42x slower
461
+ Markly.render_html: 109848.3 i/s - 2.49x slower
462
+ Kramdown::Document#to_html: 37736.6 i/s - 7.24x slower
463
+
464
+ input size = 3051 bytes
465
+
466
+ ruby 3.4.0dev (2024-12-25 master f450108330) +PRISM [arm64-darwin24]
467
+ Warming up --------------------------------------
468
+ Markly.render_html 3.117k i/100ms
469
+ Markly::Node#to_html 3.122k i/100ms
470
+ Commonmarker.to_html 3.461k i/100ms
471
+ Commonmarker.to_html (no syntax highlighting)
472
+ 3.453k i/100ms
473
+ Commonmarker#to_html 3.524k i/100ms
474
+ Commonmarker::Node.to_html
475
+ 1.865k i/100ms
476
+ Kramdown::Document#to_html
477
+ 518.000 i/100ms
478
+ Calculating -------------------------------------
479
+ Markly.render_html 31.758k (± 4.5%) i/s (31.49 μs/i) - 158.967k in 5.015343s
480
+ Markly::Node#to_html 31.760k (± 4.9%) i/s (31.49 μs/i) - 159.222k in 5.024779s
481
+ Commonmarker.to_html 35.033k (± 0.9%) i/s (28.54 μs/i) - 176.511k in 5.038807s
482
+ Commonmarker.to_html (no syntax highlighting)
483
+ 34.754k (± 1.0%) i/s (28.77 μs/i) - 176.103k in 5.067574s
484
+ Commonmarker#to_html 35.240k (± 1.5%) i/s (28.38 μs/i) - 176.200k in 5.001097s
485
+ Commonmarker::Node.to_html
486
+ 19.716k (± 2.3%) i/s (50.72 μs/i) - 98.845k in 5.016154s
487
+ Kramdown::Document#to_html
488
+ 5.314k (± 0.8%) i/s (188.19 μs/i) - 26.936k in 5.069493s
489
+
490
+ Comparison:
491
+ Commonmarker#to_html: 35240.0 i/s
492
+ Commonmarker.to_html: 35033.0 i/s - same-ish: difference falls within error
493
+ Commonmarker.to_html (no syntax highlighting): 34754.3 i/s - same-ish: difference falls within error
494
+ Markly::Node#to_html: 31759.8 i/s - 1.11x slower
495
+ Markly.render_html: 31757.6 i/s - 1.11x slower
496
+ Commonmarker::Node.to_html: 19716.0 i/s - 1.79x slower
497
+ Kramdown::Document#to_html: 5313.7 i/s - 6.63x slower
498
+
359
499
  input size = 11064832 bytes
360
500
 
361
- ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin23]
501
+ ruby 3.4.0dev (2024-12-25 master f450108330) +PRISM [arm64-darwin24]
362
502
  Warming up --------------------------------------
363
- Markly.render_html 1.000 i/100ms
364
- Markly::Node#to_html 1.000 i/100ms
365
- Commonmarker.to_html 1.000 i/100ms
503
+ Markly.render_html 2.000 i/100ms
504
+ Markly::Node#to_html 2.000 i/100ms
505
+ Commonmarker.to_html 3.000 i/100ms
506
+ Commonmarker.to_html (no syntax highlighting)
507
+ 3.000 i/100ms
508
+ Commonmarker#to_html 3.000 i/100ms
366
509
  Commonmarker::Node.to_html
367
- 1.000 i/100ms
510
+ 2.000 i/100ms
368
511
  Kramdown::Document#to_html
369
512
  1.000 i/100ms
370
513
  Calculating -------------------------------------
371
- Markly.render_html 15.606 (±25.6%) i/s - 71.000 in 5.047132s
372
- Markly::Node#to_html 15.692 (±25.5%) i/s - 72.000 in 5.095810s
373
- Commonmarker.to_html 4.482 (± 0.0%) i/s - 23.000 in 5.137680s
514
+ Markly.render_html 26.830 (± 7.5%) i/s (37.27 ms/i) - 134.000 in 5.035141s
515
+ Markly::Node#to_html 25.772 (±11.6%) i/s (38.80 ms/i) - 128.000 in 5.014892s
516
+ Commonmarker.to_html 34.650 (± 2.9%) i/s (28.86 ms/i) - 174.000 in 5.024235s
517
+ Commonmarker.to_html (no syntax highlighting)
518
+ 34.140 (± 2.9%) i/s (29.29 ms/i) - 171.000 in 5.013207s
519
+ Commonmarker#to_html 34.108 (± 2.9%) i/s (29.32 ms/i) - 171.000 in 5.016186s
374
520
  Commonmarker::Node.to_html
375
- 5.092 (±19.6%) i/s - 25.000 in 5.072220s
521
+ 18.842 (± 5.3%) i/s (53.07 ms/i) - 96.000 in 5.106005s
376
522
  Kramdown::Document#to_html
377
- 0.379 (± 0.0%) i/s - 2.000 in 5.277770s
523
+ 6.290 (± 0.0%) i/s (158.97 ms/i) - 32.000 in 5.112588s
378
524
 
379
525
  Comparison:
380
- Markly::Node#to_html: 15.7 i/s
381
- Markly.render_html: 15.6 i/s - same-ish: difference falls within error
382
- Commonmarker::Node.to_html: 5.1 i/s - 3.08x slower
383
- Commonmarker.to_html: 4.5 i/s - 3.50x slower
384
- Kramdown::Document#to_html: 0.4 i/s - 41.40x slower
526
+ Commonmarker.to_html: 34.6 i/s
527
+ Commonmarker.to_html (no syntax highlighting): 34.1 i/s - same-ish: difference falls within error
528
+ Commonmarker#to_html: 34.1 i/s - same-ish: difference falls within error
529
+ Markly.render_html: 26.8 i/s - 1.29x slower
530
+ Markly::Node#to_html: 25.8 i/s - 1.34x slower
531
+ Commonmarker::Node.to_html: 18.8 i/s - 1.84x slower
532
+ Kramdown::Document#to_html: 6.3 i/s - 5.51x slower
385
533
  ```