Skip to content

Commit 7a1a157

Browse files
authored
Merge pull request #290 from gjtorikian/code-string-content
Expose code and code blocks' literals via string_content/=.
2 parents 1ba709d + 6c0de93 commit 7a1a157

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

ext/commonmarker/src/node.rs

+20
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,14 @@ impl CommonmarkerNode {
669669
fn get_string_content(&self) -> Result<String, magnus::Error> {
670670
let node = self.inner.borrow();
671671

672+
match node.data.value {
673+
ComrakNodeValue::Code(ref code) => return Ok(code.literal.to_string()),
674+
ComrakNodeValue::CodeBlock(ref code_block) => {
675+
return Ok(code_block.literal.to_string())
676+
}
677+
_ => {}
678+
}
679+
672680
match node.data.value.text() {
673681
Some(s) => Ok(s.to_string()),
674682
None => Err(magnus::Error::new(
@@ -681,6 +689,18 @@ impl CommonmarkerNode {
681689
fn set_string_content(&self, new_content: String) -> Result<bool, magnus::Error> {
682690
let mut node = self.inner.borrow_mut();
683691

692+
match node.data.value {
693+
ComrakNodeValue::Code(ref mut code) => {
694+
code.literal = new_content;
695+
return Ok(true);
696+
}
697+
ComrakNodeValue::CodeBlock(ref mut code_block) => {
698+
code_block.literal = new_content;
699+
return Ok(true);
700+
}
701+
_ => {}
702+
}
703+
684704
match node.data.value.text_mut() {
685705
Some(s) => {
686706
*s = new_content;

lib/commonmarker/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module Commonmarker
4-
VERSION = "1.1.1"
4+
VERSION = "1.1.2"
55
end

test/node_test.rb

+12-1
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,10 @@ def test_delete
102102

103103
class StringContentTest < Minitest::Test
104104
def setup
105-
@document = Commonmarker.parse("**HELLO!** \n***\n This has nodes!")
105+
@document = Commonmarker.parse("**HELLO!** \n***\n This has `nodes`!")
106106
@paragraph = @document.first_child
107107
@emph = @paragraph.first_child
108+
@code_inline = @document.last_child.last_child.previous_sibling
108109
end
109110

110111
def test_node_can_get_string_content
@@ -128,6 +129,16 @@ def test_node_can_protect_against_nodes_without_string_content
128129

129130
assert_match(%r{<strong>HELLO!</strong>}, @document.to_html)
130131
end
132+
133+
def test_code_inline_can_get_string_content
134+
assert_equal("nodes", @code_inline.string_content)
135+
end
136+
137+
def test_code_inline_can_set_string_content
138+
@code_inline.string_content = "string content"
139+
140+
assert_match(%r{<code>string content</code>}, @document.to_html)
141+
end
131142
end
132143

133144
class UrlTest < Minitest::Test

0 commit comments

Comments
 (0)