Skip to content

Commit

Permalink
Markdown handle receiving the same text multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
rorJeremy committed Jan 22, 2019
1 parent c274c3b commit 1d38b80
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
Expand Up @@ -53,11 +53,12 @@ def markdown(text)
}

renderer = ::WCC::Contentful::App::CustomMarkdownRender.new(options)
remove_markdown_href_class_syntax(raw_classes, text)
markdown = ::Redcarpet::Markdown.new(renderer, extensions)

content_tag(:div,
markdown.render(text).html_safe,
markdown.render(
remove_markdown_href_class_syntax(raw_classes, text)
).html_safe,
class: 'formatted-content')
end

Expand Down Expand Up @@ -85,7 +86,9 @@ def gather_links_with_classes_data(markdown_links)
end

def remove_markdown_href_class_syntax(raw_classes, text)
raw_classes.each { |klass| text.slice!(klass) }
text_without_markdown_class_syntax = text.dup
raw_classes.each { |klass| text_without_markdown_class_syntax.slice!(klass) }
text_without_markdown_class_syntax
end

def url_and_title(markdown_link_and_title)
Expand Down
Expand Up @@ -9,6 +9,12 @@
' Last line goes here [Test](https://test.com).'
}

let(:markdown_string_with_links_that_have_classes2) {
+"Ministry by [Awaken](/awaken \"Awaken's Homepage\"){: .button .white} in Dallas, Texas."\
" Ministry by [Awaken](/awaken \"Awaken's Homepage\"){: .button .white} in Dallas, Texas."\
" Ministry by [Awaken](/awaken \"Awaken's Homepage\"){: .button .white} in Dallas, Texas."
}

let(:markdown_string_with_classless_link) {
'Test line goes here [Test](https://test.com).'
}
Expand All @@ -34,6 +40,30 @@
end
end

context 'when markdown includes the same link with classes multiple times' do
it 'builds the hyperlink with classes each time it appears in the markdown' do
html_to_render =
helper.markdown(markdown_string_with_links_that_have_classes2)

# The {: .button .white} is in the markdown_string_with_links_that_have_classes2
# 3 times, so 3 hyperlinks with those classes should be returned in the html_to_render
expect(html_to_render).to have_selector('.button.white', count: 3)
end
end

context 'when markdown receives the same text twice' do
it 'applies the classes to the hyperlinks both times' do
html_to_render =
helper.markdown(markdown_string_with_links_that_have_classes)

html_to_render2 =
helper.markdown(markdown_string_with_links_that_have_classes)

expect(html_to_render).to have_selector('.button.white', count: 1)
expect(html_to_render2).to have_selector('.button.white', count: 1)
end
end

context 'when markdown does NOT include links with classes' do
it 'returns html with hyperlinks that do not have class attributes' do
html_to_render =
Expand Down Expand Up @@ -306,7 +336,7 @@
end

describe '#remove_markdown_href_class_syntax' do
it "removes all of the '{: .button}' syntax from markdown text" do
it "returns the markdown text without all of the '{: .button}' syntax" do
raw_classes =
[
'{: .button .white}',
Expand All @@ -317,11 +347,17 @@
' [Watermark Community Church](http://www.watermark.org){: .button-medium .green}'\
' [Test](https://test.com).'

text_without_class_syntax =
+"Ministry developed by [Awaken](/awaken \"Awaken's Homepage\")"\
' [Watermark Community Church](http://www.watermark.org)'\
' [Test](https://test.com).'

expect(text.include?('{: .button .white}')).to be true

helper.remove_markdown_href_class_syntax(raw_classes, text)
transformed_text = helper.remove_markdown_href_class_syntax(raw_classes, text)

expect(text.include?('{: .button .white}')).to be false
expect(transformed_text.include?('{: .button .white}')).to be false
expect(transformed_text).to eq(text_without_class_syntax)
end
end

Expand Down

0 comments on commit 1d38b80

Please sign in to comment.