Skip to content

Commit

Permalink
Merge pull request #290 from gjtorikian/code-string-content
Browse files Browse the repository at this point in the history
Expose code and code blocks' literals via string_content/=.
  • Loading branch information
gjtorikian committed May 4, 2024
2 parents 1ba709d + 6c0de93 commit 7a1a157
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
20 changes: 20 additions & 0 deletions ext/commonmarker/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,14 @@ impl CommonmarkerNode {
fn get_string_content(&self) -> Result<String, magnus::Error> {
let node = self.inner.borrow();

match node.data.value {
ComrakNodeValue::Code(ref code) => return Ok(code.literal.to_string()),
ComrakNodeValue::CodeBlock(ref code_block) => {
return Ok(code_block.literal.to_string())
}
_ => {}
}

match node.data.value.text() {
Some(s) => Ok(s.to_string()),
None => Err(magnus::Error::new(
Expand All @@ -681,6 +689,18 @@ impl CommonmarkerNode {
fn set_string_content(&self, new_content: String) -> Result<bool, magnus::Error> {
let mut node = self.inner.borrow_mut();

match node.data.value {
ComrakNodeValue::Code(ref mut code) => {
code.literal = new_content;
return Ok(true);
}
ComrakNodeValue::CodeBlock(ref mut code_block) => {
code_block.literal = new_content;
return Ok(true);
}
_ => {}
}

match node.data.value.text_mut() {
Some(s) => {
*s = new_content;
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.1.1"
VERSION = "1.1.2"
end
13 changes: 12 additions & 1 deletion test/node_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@ def test_delete

class StringContentTest < Minitest::Test
def setup
@document = Commonmarker.parse("**HELLO!** \n***\n This has nodes!")
@document = Commonmarker.parse("**HELLO!** \n***\n This has `nodes`!")
@paragraph = @document.first_child
@emph = @paragraph.first_child
@code_inline = @document.last_child.last_child.previous_sibling
end

def test_node_can_get_string_content
Expand All @@ -128,6 +129,16 @@ def test_node_can_protect_against_nodes_without_string_content

assert_match(%r{<strong>HELLO!</strong>}, @document.to_html)
end

def test_code_inline_can_get_string_content
assert_equal("nodes", @code_inline.string_content)
end

def test_code_inline_can_set_string_content
@code_inline.string_content = "string content"

assert_match(%r{<code>string content</code>}, @document.to_html)
end
end

class UrlTest < Minitest::Test
Expand Down

0 comments on commit 7a1a157

Please sign in to comment.