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

Expose code and code blocks' literals via string_content/=. #290

Merged
merged 3 commits into from
May 4, 2024
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
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