From f0994d0b048606408941bf3bf210e799a5429d0a Mon Sep 17 00:00:00 2001 From: "Garen J. Torikian" Date: Fri, 11 Oct 2019 12:27:52 +0100 Subject: [PATCH 1/2] Handle first few tests --- lib/markdown_translation_filter.rb | 12 +++++++----- test/markdown_translation_filter_test.rb | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/markdown_translation_filter.rb b/lib/markdown_translation_filter.rb index 00c5419..8a16b20 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 @@ -27,7 +27,7 @@ def call elsif node.type == :document next else - text += "#{node.to_commonmark}" + text += "#{node.to_commonmark(:DEFAULT, -1)}" end end @@ -35,8 +35,10 @@ def call end end -class HeaderRenderer# < CommonMarker::HtmlRenderer - def self.header(node) - "#{node.first_child.string_content}\n\n" +class HeaderRenderer #< CommonMarker::HtmlRenderer + def header(node) + inner = '' + node.each { |child| inner += child.to_html } + "#{inner}\n" end end diff --git a/test/markdown_translation_filter_test.rb b/test/markdown_translation_filter_test.rb index c79bf13..b5e7e0f 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 From a3fa0931053c7cf4f6e20865a43e76ef98a27cf7 Mon Sep 17 00:00:00 2001 From: "Garen J. Torikian" Date: Sun, 13 Oct 2019 21:53:07 +0100 Subject: [PATCH 2/2] Pass tests --- lib/markdown_translation_filter.rb | 20 +++++++++++--------- test/markdown_translation_filter_test.rb | 2 +- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/markdown_translation_filter.rb b/lib/markdown_translation_filter.rb index 8a16b20..5bab8ef 100644 --- a/lib/markdown_translation_filter.rb +++ b/lib/markdown_translation_filter.rb @@ -20,25 +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(:DEFAULT, -1)}" + 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 +class HeaderRenderer def header(node) + original = node.to_commonmark(:DEFAULT, -1).chomp inner = '' node.each { |child| inner += child.to_html } - "#{inner}\n" + replacement = "#{inner}" + [original, replacement] end end diff --git a/test/markdown_translation_filter_test.rb b/test/markdown_translation_filter_test.rb index b5e7e0f..0e29031 100644 --- a/test/markdown_translation_filter_test.rb +++ b/test/markdown_translation_filter_test.rb @@ -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)