diff --git a/lib/markdown_translation_filter.rb b/lib/markdown_translation_filter.rb
index 00c5419..5bab8ef 100644
--- a/lib/markdown_translation_filter.rb
+++ b/lib/markdown_translation_filter.rb
@@ -6,7 +6,7 @@
class MarkdownTranslationFilter < HTML::Pipeline::TextFilter
def initialize(text, context = nil, result = nil)
super text, context, result
- @parser = context[:markdown_parser]
+ @parser = context[:markdown_parser].new
end
def call
@@ -20,23 +20,27 @@ def call
doc = CommonMarker.render_doc(@text, parse_options, extensions)
- text = ''
+ changes = []
doc.each do |node|
if @parser.respond_to?(node.type)
- text += @parser.send(node.type, node)
- elsif node.type == :document
- next
- else
- text += "#{node.to_commonmark}"
+ changes << @parser.send(node.type, node)
end
end
- text
+ changes.each do |original, replacement|
+ @text = @text.sub(original, replacement)
+ end
+
+ @text
end
end
-class HeaderRenderer# < CommonMarker::HtmlRenderer
- def self.header(node)
- "#{node.first_child.string_content}\n\n"
+class HeaderRenderer
+ def header(node)
+ original = node.to_commonmark(:DEFAULT, -1).chomp
+ inner = ''
+ node.each { |child| inner += child.to_html }
+ replacement = "#{inner}"
+ [original, replacement]
end
end
diff --git a/test/markdown_translation_filter_test.rb b/test/markdown_translation_filter_test.rb
index c79bf13..0e29031 100644
--- a/test/markdown_translation_filter_test.rb
+++ b/test/markdown_translation_filter_test.rb
@@ -16,7 +16,7 @@ def test_it_renders_header_as_html
# current solution does not properly render children of a node, in this
# case,
md = "## Foo bar *baz*"
- assert_equal 'Foo bar baz
', pipeline.call(md)[:output].chomp
+ assert_equal 'Foo bar baz
', pipeline.call(md)[:output].chomp
end
def test_it_doesnt_add_weird_linebreaks
@@ -45,7 +45,7 @@ def test_ideal_scenario
md = article_markdown('## Local Workstation Setup', '## Write your App')
expected = article_markdown("Local Workstation Setup
", "Write your App
")
- assert_equal expected, pipeline.call(md)[:output].chomp
+ assert_equal expected, pipeline.call(md)[:output]
end
def article_markdown(*replacements)