Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shortcodes/emoji #210

Merged
merged 7 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ Commonmarker.to_html('"Hi *there*"', options: {
| `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` |

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

Expand Down Expand Up @@ -130,15 +131,17 @@ Commonmarker.to_html(code, plugins: { syntax_highlighter: nil })
Commonmarker.to_html(code, plugins: { syntax_highlighter: { theme: nil } })
```

Available themes ([source](https://docs.rs/syntect/5.0.0/syntect/highlighting/struct.ThemeSet.html#implementations)):
##### Available themes

- base16-ocean.dark
- base16-eighties.dark
- base16-mocha.dark
- base16-ocean.light
- InspiredGitHub
- Solarized (dark)
- Solarized (light)
Here's [a list of available themes](https://docs.rs/syntect/5.0.0/syntect/highlighting/struct.ThemeSet.html#implementations):

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

## Output formats

Expand Down
2 changes: 1 addition & 1 deletion ext/commonmarker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"

[dependencies]
magnus = "0.4"
comrak = "0.16"
comrak = { version = "0.16", features = ["shortcodes"] }

[lib]
name = "commonmarker"
Expand Down
4 changes: 4 additions & 0 deletions ext/commonmarker/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const EXTENSION_HEADER_IDS: &str = "header_ids";
const EXTENSION_FOOTNOTES: &str = "footnotes";
const EXTENSION_DESCRIPTION_LISTS: &str = "description_lists";
const EXTENSION_FRONT_MATTER_DELIMITER: &str = "front_matter_delimiter";
const EXTENSION_SHORTCODES: &str = "shortcodes";

fn iterate_extension_options(comrak_options: &mut ComrakOptions, options_hash: RHash) {
options_hash
Expand Down Expand Up @@ -103,6 +104,9 @@ fn iterate_extension_options(comrak_options: &mut ComrakOptions, options_hash: R
Ok(Cow::Borrowed(EXTENSION_FRONT_MATTER_DELIMITER)) => {
comrak_options.extension.front_matter_delimiter = try_convert_string(value);
}
Ok(Cow::Borrowed(EXTENSION_SHORTCODES)) => {
comrak_options.extension.shortcodes = value.try_convert::<bool>()?;
}
_ => {}
}
Ok(ForEach::Continue)
Expand Down
1 change: 1 addition & 0 deletions lib/commonmarker/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module Config
footnotes: false,
description_lists: false,
front_matter_delimiter: nil,
shortcodes: true,
},
format: [:html].freeze,
}.freeze
Expand Down
2 changes: 1 addition & 1 deletion lib/commonmarker/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Commonmarker
VERSION = "1.0.0.pre6"
VERSION = "1.0.0.pre7"
end
20 changes: 16 additions & 4 deletions test/test_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ def test_uses_specified_extensions
end
end

def test_bad_extension_specifications
assert_raises(TypeError) { Commonmarker.to_html(@markdown, options: "nope") }
end

def test_comments_are_kept_as_expected
options = { render: { unsafe: true }, extension: { tagfilter: true } }

Expand Down Expand Up @@ -64,4 +60,20 @@ def test_definition_lists
HTML
assert_equal(output, html)
end

def test_emoji_renders_by_default
assert_equal(
"<p>Happy Friday! 😄</p>\n",
Commonmarker.to_html("Happy Friday! :smile:"),
)
end

def test_can_disable_emoji_renders
options = { extension: { shortcodes: false } }

assert_equal(
"<p>Happy Friday! :smile:</p>\n",
Commonmarker.to_html("Happy Friday! :smile:", options: options),
)
end
end
4 changes: 4 additions & 0 deletions test/test_maliciousness.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def test_bad_options_value
assert_equal("parse option `:smart` must be Boolean; got Integer", err.message)
end

def test_bad_extension_type
assert_raises(TypeError) { Commonmarker.to_html(@markdown, options: { extensions: "nope" }) }
end

def test_non_utf8
err = assert_raises(TypeError) do
Commonmarker.to_html("foo \n baz".encode("US-ASCII"))
Expand Down