Skip to content

Commit

Permalink
add math
Browse files Browse the repository at this point in the history
  • Loading branch information
gjtorikian committed Apr 30, 2024
1 parent 4ef4951 commit b64998d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 14 deletions.
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,20 +179,21 @@ Commonmarker.to_html('"Hi *there*"', options: {

### Extension options

| Name | Description | Default |
| ------------------------ | ------------------------------------------------------------------------------------------------------------------- | ------- |
| `strikethrough` | Enables the [strikethrough extension](https://github.github.com/gfm/#strikethrough-extension-) from the GFM spec. | `true` |
| `tagfilter` | Enables the [tagfilter extension](https://github.github.com/gfm/#disallowed-raw-html-extension-) from the GFM spec. | `true` |
| `table` | Enables the [table extension](https://github.github.com/gfm/#tables-extension-) from the GFM spec. | `true` |
| `autolink` | Enables the [autolink extension](https://github.github.com/gfm/#autolinks-extension-) from the GFM spec. | `true` |
| `tasklist` | Enables the [task list extension](https://github.github.com/gfm/#task-list-items-extension-) from the GFM spec. | `true` |
| `superscript` | Enables the superscript Comrak extension. | `false` |
| `header_ids` | Enables the header IDs Comrak extension. from the GFM spec. | `""` |
| `footnotes` | Enables the footnotes extension per `cmark-gfm`. | `false` |
| `description_lists` | Enables the description lists extension. | `false` |
| `front_matter_delimiter` | Enables the front matter extension. | `""` |
| `shortcodes` | Enables the shortcodes extension. | `true` |
| `multiline_block_quotes` | Enables the multiline block quotes extension. | `false` |
| Name | Description | Default |
| --------------------------- | ------------------------------------------------------------------------------------------------------------------- | ------- |
| `strikethrough` | Enables the [strikethrough extension](https://github.github.com/gfm/#strikethrough-extension-) from the GFM spec. | `true` |
| `tagfilter` | Enables the [tagfilter extension](https://github.github.com/gfm/#disallowed-raw-html-extension-) from the GFM spec. | `true` |
| `table` | Enables the [table extension](https://github.github.com/gfm/#tables-extension-) from the GFM spec. | `true` |
| `autolink` | Enables the [autolink extension](https://github.github.com/gfm/#autolinks-extension-) from the GFM spec. | `true` |
| `tasklist` | Enables the [task list extension](https://github.github.com/gfm/#task-list-items-extension-) from the GFM spec. | `true` |
| `superscript` | Enables the superscript Comrak extension. | `false` |
| `header_ids` | Enables the header IDs Comrak extension. from the GFM spec. | `""` |
| `footnotes` | Enables the footnotes extension per `cmark-gfm`. | `false` |
| `description_lists` | Enables the description lists extension. | `false` |
| `front_matter_delimiter` | Enables the front matter extension. | `""` |
| `shortcodes` | Enables the shortcodes extension. | `true` |
| `multiline_block_quotes` | Enables the multiline block quotes extension. | `false` |
| `math_dollars`, `math_code` | Enables the math extension. | `false` |

For more information on these options, see [the comrak documentation](https://github.com/kivikakk/comrak#usage).

Expand Down
8 changes: 8 additions & 0 deletions ext/commonmarker/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ const EXTENSION_DESCRIPTION_LISTS: &str = "description_lists";
const EXTENSION_FRONT_MATTER_DELIMITER: &str = "front_matter_delimiter";
const EXTENSION_SHORTCODES: &str = "shortcodes";
const EXTENSION_MULTILINE_BLOCK_QUOTES: &str = "multiline_block_quotes";
const EXTENSION_MATH_DOLLARS: &str = "math_dollars";
const EXTENSION_MATH_CODE: &str = "math_code";

fn iterate_extension_options(comrak_options: &mut ComrakOptions, options_hash: RHash) {
options_hash
Expand Down Expand Up @@ -126,6 +128,12 @@ fn iterate_extension_options(comrak_options: &mut ComrakOptions, options_hash: R
comrak_options.extension.multiline_block_quotes =
TryConvert::try_convert(value)?;
}
Ok(Cow::Borrowed(EXTENSION_MATH_DOLLARS)) => {
comrak_options.extension.math_dollars = TryConvert::try_convert(value)?;
}
Ok(Cow::Borrowed(EXTENSION_MATH_CODE)) => {
comrak_options.extension.math_code = TryConvert::try_convert(value)?;
}
_ => {}
}
Ok(ForEach::Continue)
Expand Down
2 changes: 2 additions & 0 deletions lib/commonmarker/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ module Config
front_matter_delimiter: "",
shortcodes: true,
multiline_block_quotes: false,
math_dollars: false,
math_code: false,
},
format: [:html].freeze,
}.freeze
Expand Down
27 changes: 27 additions & 0 deletions test/math_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require "test_helper"

class MathTest < Minitest::Test
def test_math_dollars_to_html
md = <<~MARKDOWN
$1 + 2$ and $$x = y$$
MARKDOWN
expected = <<~HTML
<p><span data-math-style="inline">1 + 2</span> and <span data-math-style="display">x = y</span></p>
HTML

assert_equal(expected, Commonmarker.to_html(md, options: { extension: { math_dollars: true } }))
end

def test_math_code_to_html
md = <<~MARKDOWN
$`1 + 2`$
MARKDOWN
expected = <<~HTML
<p><code data-math-style="inline">1 + 2</code></p>
HTML

assert_equal(expected, Commonmarker.to_html(md, options: { extension: { math_code: true } }))
end
end

0 comments on commit b64998d

Please sign in to comment.