commonmarker 2.10.0-x86_64-linux → 3.0.0-x86_64-linux

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: 9ce6b951b17bb332437fd707359b206756948e4c71a1446aef9b2e1466622e43
4
- data.tar.gz: 129ae692bbaa24696a7ef0900dad2cae399631e9d26f3f038eb900792b5ffce9
3
+ metadata.gz: 2e1eebee91b8d60c1b3c489b963bbf938f17a95e0b74ec0e1d9333a96adc88ee
4
+ data.tar.gz: b3073a580a9e2d1d2a61c073a20912710784a8e1155024acb2e022752314d5b1
5
5
  SHA512:
6
- metadata.gz: 71fde760c2ac2d8035b0090e7b0270e1f0147bc66d5a05b157a54248b630107ac0e299453fed9fa2269d6c1be97ade2788f40790e15c0b77cf8d837b70795ffa
7
- data.tar.gz: 0b3c60142ac4e0b9440c4a08f3ed7fde610f27615438b638eb98349142df89dbaf51b62a4faae051cf176f54b93b7bce28893ee94ed12a00fc04eaca827434dd
6
+ metadata.gz: 9a9e3ee95694815513b74427df333ccee4f2e094badd9a045df581a68535516b244a21be0516bd2a3f4c9bd75237e35ae03250f6c95e1e7f44bf5f07b76906b0
7
+ data.tar.gz: 3b3a543ea4f1be4005801e31311ee9715930ba977ed45b637435d392ee1e99fac0a6efccd7736624322df13872a6515504db04651abf0d1524b8f77b0255fa93
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`, `tagfilter`, `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` |
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` |
187
256
 
188
257
  ### Render options
189
258
 
190
- | Name | Description | Default |
191
- | -------------------- | ------------------------------------------------------------------------------------------------------------------------ | ------------ |
192
- | `hardbreaks` | [Soft line breaks](http://spec.commonmark.org/0.27/#soft-line-breaks) translate into hard line breaks. | `true` |
193
- | `github_pre_lang` | GitHub-style `<pre lang="xyz">` is used for fenced code blocks with info tags. | `true` |
194
- | `full_info_string` | Gives info string data after a space in a `data-meta` attribute on code blocks. | `false` |
195
- | `width` | The wrap column when outputting CommonMark. | `80` |
196
- | `unsafe` | Allow rendering of raw HTML and potentially dangerous links. | `false` |
197
- | `escape` | Escape raw HTML instead of clobbering it. | `false` |
198
- | `sourcepos` | Include source position attribute in HTML and XML output. | `false` |
199
- | `escaped_char_spans` | Wrap escaped characters in span tags. | `true` |
200
- | `ignore_empty_links` | Ignores empty links, leaving the Markdown text in place. | `false` |
201
- | `gfm_quirks` | Outputs HTML with GFM-style quirks; namely, not nesting `<strong>` inlines. | `false` |
202
- | `prefer_fenced` | Always output fenced code blocks, even where an indented one could be used. | `false` |
203
- | `tasklist_classes` | Add CSS classes to the HTML output of the tasklist extension | `false` |
204
- | `compact_html` | Suppress newlines in pretty-printed HTML output. | `false` |
205
- | `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` |
206
280
 
207
281
  As well, there are several extensions which you can toggle in the same manner:
208
282
 
209
283
  ```ruby
210
284
  Commonmarker.to_html('"Hi *there*"', options: {
211
- extension: { footnotes: true, description_lists: true },
212
- render: { hardbreaks: false }
285
+ extension: { footnotes: true, description_lists: true }
213
286
  })
214
287
  ```
215
288
 
216
289
  ### Extension options
217
290
 
218
- | Name | Description | Default |
219
- | ----------------------------- | ------------------------------------------------------------------------------------------------------------------- | ------- |
220
- | `strikethrough` | Enables the [strikethrough extension](https://github.github.com/gfm/#strikethrough-extension-) from the GFM spec. | `true` |
221
- | `tagfilter` | Enables the [tagfilter extension](https://github.github.com/gfm/#disallowed-raw-html-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
  ```
data/UPGRADING.md ADDED
@@ -0,0 +1,102 @@
1
+ # Upgrading Commonmarker
2
+
3
+ ## Upgrading from 2.x
4
+
5
+ Commonmarker 3.0 changes what a bare `Commonmarker.to_html(text)` produces and turns `Commonmarker` into a class. Each change is described below along with what to do about it.
6
+
7
+ ### The defaults are now comrak's
8
+
9
+ Previously, Commonmarker enabled a handful of options and the syntax highlighter by default, so `Commonmarker.to_html(text)` with no options produced GitHub-comment-style HTML.
10
+
11
+ 3.0 uses comrak's own defaults instead: plain CommonMark, with no extensions and no plugins. Comrak documents these defaults as compliant with [CommonMark 0.31.2](https://spec.commonmark.org/0.31.2/). Everything in this table is now off unless you turn it on:
12
+
13
+ | Option | 2.x default | 3.0 default |
14
+ | ------------------------------------------------------------- | -------------------------------- | ----------------- |
15
+ | `extension: { strikethrough:, table:, autolink:, tasklist: }` | `true` | `false` |
16
+ | `extension: { shortcodes: }` | `true` | `false` |
17
+ | `extension: { header_ids: }` | `""` (on, with no prefix) | `nil` (off) |
18
+ | `render: { hardbreaks: }` | `true` | `false` |
19
+ | `render: { github_pre_lang: }` | `true` | `false` |
20
+ | `render: { escaped_char_spans: }` | `true` | `false` |
21
+ | `render: { width: }` | `80` | `0` (no wrapping) |
22
+ | `plugins: { syntax_highlighter: }` | `{ theme: "base16-ocean.dark" }` | `nil` (off) |
23
+
24
+ The visible effects are:
25
+
26
+ - Soft line breaks stay soft. A newline inside a paragraph no longer becomes `<br />`.
27
+ - Headings have no `id` attribute and no anchor link.
28
+ - Fenced code blocks render as `<pre><code class="language-ruby">` instead of `<pre lang="ruby">`, and are not syntax highlighted.
29
+ - `~~strikethrough~~`, tables, bare URLs, `- [ ]` task lists, and `:emoji:` shortcodes render as literal text.
30
+ - `to_commonmark` no longer wraps lines at 80 columns.
31
+
32
+ **To get the 2.x output back**, pass the old defaults explicitly. If you convert many documents, build one `Commonmarker` instance with them (see the next section) rather than passing them on every call:
33
+
34
+ ```ruby
35
+ OPTIONS = {
36
+ render: { hardbreaks: true, github_pre_lang: true, width: 80, escaped_char_spans: true },
37
+ extension: { strikethrough: true, table: true, autolink: true, tasklist: true, shortcodes: true, header_ids: "" },
38
+ }.freeze
39
+ PLUGINS = { syntax_highlighter: { theme: "base16-ocean.dark" } }.freeze
40
+
41
+ Commonmarker.to_html(text, options: OPTIONS, plugins: PLUGINS)
42
+ ```
43
+
44
+ **For GitHub Flavored Markdown**, enable what comrak's own `--gfm` flag enables:
45
+
46
+ ```ruby
47
+ Commonmarker.to_html(text, options: {
48
+ extension: { strikethrough: true, table: true, autolink: true, tasklist: true },
49
+ render: { github_pre_lang: true, gfm_quirks: true },
50
+ })
51
+ ```
52
+
53
+ Two smaller consequences of the new defaults:
54
+
55
+ - `nil` now always means "the default". In 2.x, `render: { hardbreaks: nil }` gave comrak's default (`false`) while leaving the key out gave Commonmarker's (`true`). Those are now the same thing, for every option.
56
+ - `Commonmarker::Config::OPTIONS` reports the new defaults, with `nil` for the string options that are off (`default_info_string`, `header_ids`, `front_matter_delimiter`), and `Commonmarker::Config::PLUGINS` is `{ syntax_highlighter: nil }`. Both are snapshots for introspection; passing them back in changes nothing.
57
+
58
+ ### `Commonmarker` is a class
59
+
60
+ `Commonmarker` was a module. It is now a class, so that `Commonmarker.new(options:, plugins:)` can validate a configuration once and reuse it across calls and threads:
61
+
62
+ ```ruby
63
+ MARKDOWN = Commonmarker.new(options: { parse: { smart: true } }, plugins: { syntax_highlighter: {} })
64
+
65
+ MARKDOWN.to_html(text)
66
+ MARKDOWN.to_commonmark(text)
67
+ MARKDOWN.parse(text)
68
+ ```
69
+
70
+ `Commonmarker.to_html`, `Commonmarker.parse`, and `Node#to_html` keep working as before. What breaks:
71
+
72
+ - `module Commonmarker ... end` raises `TypeError` when the file loads. Reopen it with `class Commonmarker` instead.
73
+ - `include Commonmarker` and `extend Commonmarker` no longer work. Call `Commonmarker.to_html` directly.
74
+
75
+ ### `alert_style` and `list_style` take Symbols
76
+
77
+ Both pick from a fixed set of names, and now take a Symbol like option keys, node types, and list types do everywhere else in the API:
78
+
79
+ ```ruby
80
+ Commonmarker.to_html(md, options: { render: { alert_style: :semantic } }) # was "semantic"
81
+ doc.to_commonmark(options: { render: { list_style: :plus } }) # was "plus"
82
+ ```
83
+
84
+ - A String raises `TypeError`: ``render option `:list_style` must be Symbol; got String``.
85
+ - An unknown name raises `ArgumentError` instead of silently falling back to the default: ``render option `:list_style` must be one of :dash, :plus, :star; got :bullet``.
86
+ - `Commonmarker::Config::OPTIONS` reports these defaults as `:specific` and `:dash`.
87
+
88
+ Free-form string options (`header_ids`, `front_matter_delimiter`, `default_info_string`, and the highlighter `theme`) still take Strings.
89
+
90
+ ### Options are processed in Rust
91
+
92
+ Option and plugin handling moved out of Ruby, which removed some Ruby-side internals:
93
+
94
+ - `Commonmarker::Config.process_options` and `Commonmarker::Config.process_plugins` are gone. Pass your Hash to `Commonmarker.new`, which validates it up front.
95
+ - `Commonmarker::Utils` and `Commonmarker::Constants` are gone.
96
+ - `Commonmarker::Config::OPTIONS` no longer has a `:format` key.
97
+
98
+ Validation still raises `TypeError` for a value of the wrong type. A value of the right type that is not allowed (an unknown theme, a negative width, an unknown `list_style`) raises `ArgumentError`.
99
+
100
+ ### comrak 1.x output changes
101
+
102
+ 3.0 is built on comrak 1.2, where 2.x used 0.56. Comrak 1.0 formally specified footnotes, math, superscript and subscript, description lists, header IDs, wikilinks, alerts, tables, autolinks, and task lists, and each spec changed output in edge cases. Among them: duplicate footnote definitions follow first-wins, empty headings get a `-1` id, and lists, footnote definitions, and fenced containers stop nesting at 100 levels. If you snapshot-test HTML output, expect some diffs. [Comrak's changelog](https://github.com/kivikakk/comrak/blob/main/CHANGELOG.md) has the full list.
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Commonmarker
4
+ # Raised when a document nests more deeply than the `max_nesting_depth`
5
+ # parse option allows.
6
+ class NestingDepthExceededError < StandardError; end
7
+ end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Commonmarker
3
+ class Commonmarker
4
4
  class Node
5
5
  class Ast
6
6
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  require "pp"
4
4
 
5
- module Commonmarker
5
+ class Commonmarker
6
6
  class Node
7
7
  module Inspect
8
8
  PP_INDENT_SIZE = 2
@@ -27,6 +27,7 @@ module Commonmarker
27
27
  :list_tight,
28
28
  :fence_info,
29
29
  :alert_type,
30
+ :callout_type,
30
31
  ].filter_map do |name|
31
32
  [name, __send__(name)] if respond_to?(name)
32
33
  end
@@ -36,18 +37,22 @@ module Commonmarker
36
37
  printer.pp(value)
37
38
  end
38
39
 
40
+ # Show each child's type rather than recursively pretty-printing
41
+ # the full subtree. See GHSA-c4fj-h48v-9fxp: the tree walk, copy, and
42
+ # teardown were all made iterative so deeply nested input cannot
43
+ # overflow the native stack, but `printer.pp` on an array of Node
44
+ # objects re-enters `pretty_print` once per nesting level and still
45
+ # could. Collecting types keeps the output shallow and safe.
39
46
  if first_child
40
47
  printer.breakable
41
- printer.group(PP_INDENT_SIZE) do
42
- children = []
43
- node = first_child
44
- while node
45
- children << node
46
- node = node.next_sibling
47
- end
48
- printer.text("children=")
49
- printer.pp(children)
48
+ children = []
49
+ node = first_child
50
+ while node
51
+ children << node.type
52
+ node = node.next_sibling
50
53
  end
54
+ printer.text("children=")
55
+ printer.pp(children)
51
56
  end
52
57
  end
53
58
  end
@@ -3,7 +3,7 @@
3
3
  require "commonmarker/node/ast"
4
4
  require "commonmarker/node/inspect"
5
5
 
6
- module Commonmarker
6
+ class Commonmarker
7
7
  class Node
8
8
  include Enumerable
9
9
  include Inspect
@@ -20,15 +20,28 @@ module Commonmarker
20
20
  super
21
21
  end
22
22
 
23
- # Public: An iterator that "walks the tree," descending into children recursively.
23
+ # Public: An iterator that "walks the tree," yielding each node and then
24
+ # its descendants, in document order.
24
25
  #
25
- # blk - A {Proc} representing the action to take for each child
26
+ # The walk is iterative rather than recursive, so arbitrarily deep trees
27
+ # can't exhaust the stack.
28
+ #
29
+ # blk - A {Proc} representing the action to take for each node
26
30
  def walk(&block)
27
31
  return enum_for(:walk) unless block
28
32
 
29
33
  yield self
30
- each do |child|
31
- child.walk(&block)
34
+
35
+ # Like #each, the next sibling is captured before a node is yielded, so
36
+ # the block may detach or replace the node it is given. `pending` holds
37
+ # the sibling to resume at once each ancestor's subtree is done.
38
+ pending = []
39
+ node = first_child
40
+ while node
41
+ next_node = node.next_sibling
42
+ yield node
43
+ pending << next_node if next_node
44
+ node = node.first_child || pending.pop
32
45
  end
33
46
  end
34
47
 
@@ -50,28 +63,22 @@ module Commonmarker
50
63
  # plugins - A {Hash} of additional plugins.
51
64
  #
52
65
  # Returns a {String} of HTML.
53
- def to_html(options: Commonmarker::Config::OPTIONS, plugins: Commonmarker::Config::PLUGINS)
66
+ def to_html(options: {}, plugins: {})
54
67
  raise TypeError, "options must be a Hash; got a #{options.class}!" unless options.is_a?(Hash)
55
68
 
56
- opts = Config.process_options(options)
57
- plugins = Config.process_plugins(plugins)
58
-
59
- node_to_html(render: opts[:render], parse: opts[:parse], extension: opts[:extension], plugins: plugins).force_encoding("utf-8")
69
+ node_to_html(options: options, plugins: plugins)
60
70
  end
61
71
 
62
72
  # Public: Convert the node to a CommonMark string.
63
73
  #
64
- # options - A {Symbol} or {Array of Symbol}s indicating the render options
74
+ # options - A {Hash} of render, parse, and extension options.
65
75
  # plugins - A {Hash} of additional plugins.
66
76
  #
67
77
  # Returns a {String}.
68
- def to_commonmark(options: Commonmarker::Config::OPTIONS, plugins: Commonmarker::Config::PLUGINS)
78
+ def to_commonmark(options: {}, plugins: {})
69
79
  raise TypeError, "options must be a Hash; got a #{options.class}!" unless options.is_a?(Hash)
70
80
 
71
- opts = Config.process_options(options)
72
- plugins = Config.process_plugins(plugins)
73
-
74
- node_to_commonmark(render: opts[:render], parse: opts[:parse], extension: opts[:extension], plugins: plugins).force_encoding("utf-8")
81
+ node_to_commonmark(options: options, plugins: plugins)
75
82
  end
76
83
  end
77
84
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Commonmarker
4
- VERSION = "2.10.0"
3
+ class Commonmarker
4
+ VERSION = "3.0.0"
5
5
  end
data/lib/commonmarker.rb CHANGED
@@ -2,28 +2,24 @@
2
2
 
3
3
  require_relative "commonmarker/extension"
4
4
 
5
- require "commonmarker/utils"
5
+ require "commonmarker/errors"
6
6
  require "commonmarker/node"
7
- require "commonmarker/config"
8
- require "commonmarker/renderer"
9
7
  require "commonmarker/version"
10
8
 
11
- module Commonmarker
9
+ class Commonmarker
12
10
  class << self
13
- # Public: Parses a CommonMark string into an HTML string.
11
+ # Public: Parses a CommonMark string into an AST.
14
12
  #
15
13
  # text - A {String} of text
16
14
  # options - A {Hash} of render, parse, and extension options to transform the text.
17
15
  #
18
- # Returns the `parser` node.
19
- def parse(text, options: Commonmarker::Config::OPTIONS)
20
- raise TypeError, "text must be a String; got a #{text.class}!" unless text.is_a?(String)
21
- raise TypeError, "text must be UTF-8 encoded; got #{text.encoding}!" unless text.encoding.name == "UTF-8"
22
- raise TypeError, "options must be a Hash; got a #{options.class}!" unless options.is_a?(Hash)
23
-
24
- opts = Config.process_options(options)
25
-
26
- commonmark_parse(text, parse: opts.fetch(:parse, {}), render: opts.fetch(:render, {}), extension: opts.fetch(:extension, {}))
16
+ # The options are processed on every call. To parse many documents with the
17
+ # same options, create an instance with `Commonmarker.new` and call `parse`
18
+ # on it instead.
19
+ #
20
+ # Returns the `document` node.
21
+ def parse(text, options: {})
22
+ new(options: options).parse(text)
27
23
  end
28
24
 
29
25
  # Public: Parses a CommonMark string into an HTML string.
@@ -32,16 +28,79 @@ module Commonmarker
32
28
  # options - A {Hash} of render, parse, and extension options to transform the text.
33
29
  # plugins - A {Hash} of additional plugins.
34
30
  #
31
+ # The options and plugins are processed on every call. To convert many
32
+ # documents with the same configuration, create an instance with
33
+ # `Commonmarker.new` and call `to_html` on it instead.
34
+ #
35
35
  # Returns a {String} of converted HTML.
36
- def to_html(text, options: Commonmarker::Config::OPTIONS, plugins: Commonmarker::Config::PLUGINS)
37
- raise TypeError, "text must be a String; got a #{text.class}!" unless text.is_a?(String)
38
- raise TypeError, "text must be UTF-8 encoded; got #{text.encoding}!" unless text.encoding.name == "UTF-8"
39
- raise TypeError, "options must be a Hash; got a #{options.class}!" unless options.is_a?(Hash)
36
+ def to_html(text, options: {}, plugins: {})
37
+ new(options: options, plugins: plugins).to_html(text)
38
+ end
39
+ end
40
+
41
+ # Public: Creates a converter with a fixed set of options and plugins.
42
+ #
43
+ # options - A {Hash} of render, parse, and extension options to transform the text.
44
+ # plugins - A {Hash} of additional plugins.
45
+ #
46
+ # The options and plugins are validated and processed once, here, so each
47
+ # later `parse`, `to_html`, or `to_commonmark` call skips that work. Changing
48
+ # the hashes after this point has no effect on the instance. Instances hold
49
+ # no mutable state and can be shared across threads.
50
+ def initialize(options: {}, plugins: {})
51
+ raise TypeError, "options must be a Hash; got a #{options.class}!" unless options.is_a?(Hash)
52
+
53
+ commonmark_init(options, plugins)
54
+ end
55
+
56
+ # Public: Parses a CommonMark string into an AST using this instance's options.
57
+ #
58
+ # text - A {String} of text
59
+ #
60
+ # Returns the `document` node.
61
+ def parse(text)
62
+ check_text(text)
40
63
 
41
- opts = Config.process_options(options)
42
- plugins = Config.process_plugins(plugins)
64
+ commonmark_parse(text)
65
+ end
66
+
67
+ # Public: Converts CommonMark into HTML using this instance's options and plugins.
68
+ #
69
+ # input - A {String} of CommonMark, or a {Commonmarker::Node} to render as-is
70
+ # (for example, a document from `parse` that you have since modified).
71
+ #
72
+ # Returns a {String} of converted HTML.
73
+ def to_html(input)
74
+ return commonmark_render_html(input) if input.is_a?(Node)
75
+
76
+ check_text(input, or_node: true)
77
+
78
+ commonmark_to_html(input)
79
+ end
80
+
81
+ # Public: Converts CommonMark into CommonMark using this instance's options
82
+ # and plugins. Given a {String}, this parses and re-emits it; given a
83
+ # {Commonmarker::Node}, it writes out that node.
84
+ #
85
+ # input - A {String} of CommonMark, or a {Commonmarker::Node}.
86
+ #
87
+ # Returns a {String} of CommonMark.
88
+ def to_commonmark(input)
89
+ return commonmark_render_commonmark(input) if input.is_a?(Node)
43
90
 
44
- commonmark_to_html(text, parse: opts.fetch(:parse, {}), render: opts.fetch(:render, {}), extension: opts.fetch(:extension, {}), plugins: plugins)
91
+ check_text(input, or_node: true)
92
+
93
+ commonmark_to_commonmark(input)
94
+ end
95
+
96
+ private
97
+
98
+ def check_text(text, or_node: false)
99
+ unless text.is_a?(String)
100
+ accepted = or_node ? "a String or a Commonmarker::Node" : "a String"
101
+ raise TypeError, "text must be #{accepted}; got a #{text.class}!"
45
102
  end
103
+
104
+ raise TypeError, "text must be UTF-8 encoded; got #{text.encoding}!" unless text.encoding == Encoding::UTF_8
46
105
  end
47
106
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: commonmarker
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.10.0
4
+ version: 3.0.0
5
5
  platform: x86_64-linux
6
6
  authors:
7
7
  - Garen Torikian
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2026-08-24 00:00:00.000000000 Z
12
+ date: 2026-11-29 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A fast, safe, extensible parser for CommonMark. This wraps the comrak
15
15
  Rust crate.
@@ -20,19 +20,17 @@ extra_rdoc_files: []
20
20
  files:
21
21
  - LICENSE.txt
22
22
  - README.md
23
+ - UPGRADING.md
23
24
  - lib/commonmarker.rb
24
25
  - lib/commonmarker/3.2/commonmarker.so
25
26
  - lib/commonmarker/3.3/commonmarker.so
26
27
  - lib/commonmarker/3.4/commonmarker.so
27
28
  - lib/commonmarker/4.0/commonmarker.so
28
- - lib/commonmarker/config.rb
29
- - lib/commonmarker/constants.rb
29
+ - lib/commonmarker/errors.rb
30
30
  - lib/commonmarker/extension.rb
31
31
  - lib/commonmarker/node.rb
32
32
  - lib/commonmarker/node/ast.rb
33
33
  - lib/commonmarker/node/inspect.rb
34
- - lib/commonmarker/renderer.rb
35
- - lib/commonmarker/utils.rb
36
34
  - lib/commonmarker/version.rb
37
35
  homepage: https://github.com/gjtorikian/commonmarker
38
36
  licenses:
@@ -1,119 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Commonmarker
4
- module Config
5
- # For details, see
6
- # https://github.com/kivikakk/comrak/blob/162ef9354deb2c9b4a4e05be495aa372ba5bb696/src/main.rs#L201
7
- OPTIONS = {
8
- parse: {
9
- smart: false,
10
- default_info_string: "",
11
- relaxed_tasklist_matching: false,
12
- relaxed_autolinks: false,
13
- leave_footnote_definitions: false,
14
- ignore_setext: false,
15
- sourcepos_chars: false,
16
- }.freeze,
17
- render: {
18
- hardbreaks: true,
19
- github_pre_lang: true,
20
- full_info_string: false,
21
- width: 80,
22
- unsafe: false,
23
- escape: false,
24
- sourcepos: false,
25
- escaped_char_spans: true,
26
- ignore_empty_links: false,
27
- gfm_quirks: false,
28
- prefer_fenced: false,
29
- tasklist_classes: false,
30
- compact_html: false,
31
- alert_style: "specific",
32
- }.freeze,
33
- extension: {
34
- strikethrough: true,
35
- tagfilter: true,
36
- table: true,
37
- autolink: true,
38
- tasklist: true,
39
- superscript: false,
40
- header_ids: "",
41
- header_id_prefix_in_href: false,
42
- footnotes: false,
43
- inline_footnotes: false,
44
- description_lists: false,
45
- front_matter_delimiter: "",
46
- multiline_block_quotes: false,
47
- math_dollars: false,
48
- math_code: false,
49
- math_latex: false,
50
- shortcodes: true,
51
- wikilinks_title_before_pipe: false,
52
- wikilinks_title_after_pipe: false,
53
- underline: false,
54
- spoiler: false,
55
- greentext: false,
56
- subscript: false,
57
- subtext: false,
58
- alerts: false,
59
- cjk_friendly_emphasis: false,
60
- highlight: false,
61
- insert: false,
62
- block_directive: false,
63
- }.freeze,
64
- format: [:html].freeze,
65
- }.freeze
66
-
67
- PLUGINS = {
68
- syntax_highlighter: {
69
- theme: "base16-ocean.dark",
70
- path: "",
71
- }.freeze,
72
- }.freeze
73
-
74
- class << self
75
- include Commonmarker::Utils
76
-
77
- def process_options(options)
78
- {
79
- parse: process_parse_options(options[:parse].dup),
80
- render: process_render_options(options[:render].dup),
81
- extension: process_extension_options(options[:extension].dup),
82
- }
83
- end
84
-
85
- def process_plugins(plugins)
86
- {
87
- syntax_highlighter: process_syntax_highlighter_plugin(plugins&.fetch(:syntax_highlighter, nil)),
88
- }
89
- end
90
- end
91
-
92
- [:parse, :render, :extension].each do |type|
93
- define_singleton_method :"process_#{type}_options" do |options|
94
- Commonmarker::Config::OPTIONS[type].each_with_object({}) do |(key, value), hash|
95
- if options.nil? || !options.key?(key) # option not provided, use the default
96
- hash[key] = value
97
- next
98
- end
99
-
100
- if options[key].nil? # # option explicitly not included, remove it
101
- options.delete(key)
102
- next
103
- end
104
-
105
- hash[key] = fetch_kv(options, key, value, type)
106
- end
107
- end
108
- end
109
-
110
- define_singleton_method :process_syntax_highlighter_plugin do |options|
111
- return if options.nil? # plugin explicitly nil, remove it
112
-
113
- raise TypeError, "Expected a Hash for syntax_highlighter plugin, got #{options.class}" unless options.is_a?(Hash)
114
- raise TypeError, "Expected a Hash for syntax_highlighter plugin, got nothing" if options.empty?
115
-
116
- Commonmarker::Config::PLUGINS[:syntax_highlighter].merge(options)
117
- end
118
- end
119
- end
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Commonmarker
4
- module Constants
5
- BOOLS = [true, false].freeze
6
- end
7
- end
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "set"
4
- require "stringio"
5
-
6
- module Commonmarker
7
- class Renderer
8
- end
9
- end
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "commonmarker/constants"
4
-
5
- module Commonmarker
6
- module Utils
7
- include Commonmarker::Constants
8
-
9
- def fetch_kv(options, key, value, type)
10
- value_klass = value.class
11
-
12
- if Constants::BOOLS.include?(value) && BOOLS.include?(options[key])
13
- options[key]
14
- elsif options[key].is_a?(value_klass)
15
- options[key]
16
- else
17
- expected_type = Constants::BOOLS.include?(value) ? "Boolean" : value_klass.to_s
18
- raise TypeError, "#{type} option `:#{key}` must be #{expected_type}; got #{options[key].class}"
19
- end
20
- end
21
- end
22
- end