diff --git a/README.md b/README.md index 881edf60..1ad27525 100644 --- a/README.md +++ b/README.md @@ -39,9 +39,11 @@ Commonmarker.to_html('"Hi *there*"', options: { The second argument is optional--[see below](#options) for more information. -## Parse and Render Options +## Options and plugins -Commonmarker accepts the same options that comrak does, as a hash dictionary with symbol keys: +### Options + +Commonmarker accepts the same parse, render, and extensions options that comrak does, as a hash dictionary with symbol keys: ```ruby Commonmarker.to_html('"Hi *there*"', options:{ @@ -95,6 +97,36 @@ Commonmarker.to_html('"Hi *there*"', options: { For more information on these options, see [the comrak documentation](https://github.com/kivikakk/comrak#usage). +### Plugins + +In addition to the possibilities provided by generic CommonMark rendering, Commonmarker also supports plugins as a means of +providing further niceties. For example: + + code = <<~CODE + ```ruby + def hello + puts "hello" + end + + CODE + + Commonmarker.to_html(code, plugins: { syntax_highlighter: { theme: "Inspired GitHub" } }) + + #
+ # def hello
+ # puts "hello"
+ # end
+ #
+ #
+
+You can disable plugins just the same as with options, by passing `nil`:
+
+```ruby
+Commonmarker.to_html(code, plugins: { syntax_highlighter: nil })
+# or
+Commonmarker.to_html(code, plugins: { syntax_highlighter: { theme: nil } })
+```
+
## Output formats
Commonmarker can currently only generate output in one format: HTML.
@@ -102,8 +134,7 @@ Commonmarker can currently only generate output in one format: HTML.
### HTML
```ruby
-html = CommonMarker.to_html('*Hello* world!', :DEFAULT)
-puts(html)
+puts Commonmarker.to_html('*Hello* world!')
# Hello world!
``` diff --git a/ext/commonmarker/src/lib.rs b/ext/commonmarker/src/lib.rs index 1abd859d..6d80ab83 100644 --- a/ext/commonmarker/src/lib.rs +++ b/ext/commonmarker/src/lib.rs @@ -1,29 +1,85 @@ extern crate core; -use comrak::{markdown_to_html, ComrakOptions}; -use magnus::{define_module, function, r_hash::ForEach, Error, RHash, Symbol}; +use comrak::{ + adapters::SyntaxHighlighterAdapter, markdown_to_html, markdown_to_html_with_plugins, + plugins::syntect::SyntectAdapter, ComrakOptions, ComrakPlugins, +}; +use magnus::{define_module, function, r_hash::ForEach, scan_args, Error, RHash, Symbol, Value}; -mod comrak_options; -use comrak_options::iterate_options_hash; +mod options; +use options::iterate_options_hash; + +mod plugins; +use plugins::{ + syntax_highlighting::{ + fetch_syntax_highlighter_theme, SYNTAX_HIGHLIGHTER_PLUGIN_DEFAULT_THEME, + }, + SYNTAX_HIGHLIGHTER_PLUGIN, +}; + +mod utils; + +pub const EMPTY_STR: &str = ""; + +fn commonmark_to_html<'a>(args: &[Value]) -> ResultHello world -- how are you today? I'm fine, yourself?
+ # and other times
+ assert_match(lang, html)
+ assert_match(background, html)
+ end
+
+ def test_can_disable_highlighting
+ code = <<~CODE
+ ```ruby
+ def hello
+ puts "hello"
+ end
+ ```
+ CODE
+
+ html = Commonmarker.to_html(code, plugins: { syntax_highlighter: nil })
+
+ result = <<~CODE
+ def hello
+ puts "hello"
+ end
+
+ CODE
+
+ assert_equal(result, html)
+ end
+
+ def test_lack_of_theme_has_no_highlighting
+ code = <<~CODE
+ ```ruby
+ def hello
+ puts "hello"
+ end
+ ```
+ CODE
+
+ html = Commonmarker.to_html(code, plugins: { syntax_highlighter: {} })
+
+ result = <<~CODE
+ def hello
+ puts "hello"
+ end
+
+ CODE
+
+ assert_match(result, html)
+ end
+
+ def test_nil_theme_removes_highlighting
+ code = <<~CODE
+ ```ruby
+ def hello
+ puts "hello"
+ end
+ ```
+ CODE
+
+ html = Commonmarker.to_html(code, plugins: { syntax_highlighter: { theme: nil } })
+
+ result = <<~CODE
+ def hello
+ puts "hello"
+ end
+
+ CODE
+
+ assert_equal(result, html)
+ end
+
+ def test_empty_theme_is_no_highlighting
+ code = <<~CODE
+ ```ruby
+ def hello
+ puts "hello"
+ end
+ ```
+ CODE
+
+ html = Commonmarker.to_html(code, plugins: { syntax_highlighter: { theme: "" } })
+
+ result = <<~CODE
+ def hello
+ puts "hello"
+ end
+
+ CODE
+
+ assert_equal(result, html)
+ end
+
+ def test_can_change_highlighting_theme
+ code = <<~CODE
+ ```ruby
+ def hello
+ puts "hello"
+ end
+ ```
+ CODE
+
+ html = Commonmarker.to_html(code, plugins: { syntax_highlighter: { theme: "InspiredGitHub" } })
+ result = <<~HTML
+ def hello
+ puts "hello"
+ end
+
+
+ HTML
+
+ lang = %(lang="ruby")
+ background = %(style="background-color:#ffffff;")
+
+ assert_match(result, html)
+ # doing this because sometimes comrak returns
+ # and other times
+ assert_match(lang, html)
+ assert_match(background, html)
+ end
+end