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

Test fixes #4

Merged
merged 2 commits into from
Oct 14, 2019
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
26 changes: 15 additions & 11 deletions lib/markdown_translation_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
"<h#{node.header_level} id=\"foo\">#{node.first_child.string_content}</h#{node.header_level}>\n\n"
class HeaderRenderer
def header(node)
original = node.to_commonmark(:DEFAULT, -1).chomp
inner = ''
node.each { |child| inner += child.to_html }
replacement = "<h#{node.header_level} id=\"foo\">#{inner}</h#{node.header_level}>"
[original, replacement]
end
end
4 changes: 2 additions & 2 deletions test/markdown_translation_filter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_it_renders_header_as_html
# current solution does not properly render children of a node, in this
# case, <em>
md = "## Foo bar *baz*"
assert_equal '<h2 id="foo-bar-baz">Foo bar baz</h2>', pipeline.call(md)[:output].chomp
assert_equal '<h2 id="foo">Foo bar <em>baz</em></h2>', pipeline.call(md)[:output].chomp
end

def test_it_doesnt_add_weird_linebreaks
Expand Down Expand Up @@ -45,7 +45,7 @@ def test_ideal_scenario
md = article_markdown('## Local Workstation Setup', '## Write your App')
expected = article_markdown("<h2 id=\"foo\">Local Workstation Setup</h2>", "<h2 id=\"foo\">Write your App</h2>")

assert_equal expected, pipeline.call(md)[:output].chomp
assert_equal expected, pipeline.call(md)[:output]
end

def article_markdown(*replacements)
Expand Down