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 sourcepos option #265

Merged
merged 2 commits into from
Dec 27, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Note that there is a distinction in comrak for "parse" options and "render" opti
| `width` | The wrap column when outputting CommonMark. | `80` |
| `unsafe` | Allow rendering of raw HTML and potentially dangerous links. | `false` |
| `escape` | Escape raw HTML instead of clobbering it. | `false` |
| `sourcepos` | Include source position attribute in HTML and XML output. | `false` |

As well, there are several extensions which you can toggle in the same manner:

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 @@ -33,6 +33,7 @@ const RENDER_GITHUB_PRE_LANG: &str = "github_pre_lang";
const RENDER_WIDTH: &str = "width";
const RENDER_UNSAFE: &str = "unsafe";
const RENDER_ESCAPE: &str = "escape";
const RENDER_SOURCEPOS: &str = "sourcepos";

fn iterate_render_options(comrak_options: &mut ComrakOptions, options_hash: RHash) {
options_hash
Expand All @@ -53,6 +54,9 @@ fn iterate_render_options(comrak_options: &mut ComrakOptions, options_hash: RHas
Ok(Cow::Borrowed(RENDER_ESCAPE)) => {
comrak_options.render.escape = TryConvert::try_convert(value)?;
}
Ok(Cow::Borrowed(RENDER_SOURCEPOS)) => {
comrak_options.render.sourcepos = TryConvert::try_convert(value)?;
}
_ => {}
}
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 @@ -15,6 +15,7 @@ module Config
width: 80,
unsafe: false,
escape: false,
sourcepos: false,
}.freeze,
extension: {
strikethrough: true,
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"
VERSION = "1.0.1"
end
23 changes: 23 additions & 0 deletions test/sourcepos_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require "test_helper"

class TestSourcepos < Minitest::Test
def test_to_html
md = <<~MARKDOWN
# heading
paragraph

- list1
MARKDOWN
expected = <<~HTML
<h1 data-sourcepos="1:1-1:9"><a href="#heading" aria-hidden="true" class="anchor" id="heading"></a>heading</h1>
<p data-sourcepos="2:1-2:9">paragraph</p>
<ul data-sourcepos="4:1-4:7">
<li data-sourcepos="4:1-4:7">list1</li>
</ul>
HTML

assert_equal(expected, Commonmarker.to_html(md, options: { render: { sourcepos: true } }))
end
end
Loading