Skip to content

Commit

Permalink
Merge pull request #265 from ppworks/sourcepos
Browse files Browse the repository at this point in the history
Add sourcepos option
  • Loading branch information
gjtorikian committed Dec 27, 2023
2 parents 1302f42 + 6ab7551 commit d426e18
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
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

0 comments on commit d426e18

Please sign in to comment.