require "rails_helper" RSpec.describe MarkdownProcessor::Parser, type: :service do let(:random_word) { Faker::Lorem.word } let(:basic_parsed_markdown) { described_class.new(random_word) } def generate_and_parse_markdown(raw_markdown) described_class.new(raw_markdown).finalize end it "renders plain text as-is" do expect(basic_parsed_markdown.finalize).to include(random_word) end it "escapes liquid tags in codeblock" do code_block = "```\n{% what %}\n```" expect(generate_and_parse_markdown(code_block)).to include("{% what %}") end it "escapes the `raw` Liquid tag in codeblocks" do code_block = "```\n{% raw %}some text{% endraw %}\n```" expect(generate_and_parse_markdown(code_block)).to include("{% raw %}", "{% endraw %}") end it "does not allow button tag" do button = "" expect(generate_and_parse_markdown(button)).not_to include("button") end it "does not render the escaped dashes when using a `raw` Liquid tag in codeblocks with syntax highlighting" do code_block = "```js\n{% raw %}some text{% endraw %}\n```" expect(generate_and_parse_markdown(code_block)).not_to include("----") end it "escapes some triple backticks within a codeblock when using tildes" do code_block = "​~~~\nhello\n// ```\nwhatever\n// ```\n~~~" number_of_triple_backticks = generate_and_parse_markdown(code_block).scan("```").count expect(number_of_triple_backticks).to eq(2) end it "does not remove the non-'raw tag related' four dashes" do code_block = "```\n----\n```" expect(generate_and_parse_markdown(code_block)).to include("----") end it "escapes the `raw` Liquid tag in codespans" do code_block = "``{% raw %}some text{% endraw %}``" expect(generate_and_parse_markdown(code_block)).to include("{% raw %}", "{% endraw %}") end it "escapes the `raw` Liquid tag in inline code" do code_block = "`{% raw %}some text{% endraw %}`" expect(generate_and_parse_markdown(code_block)).to include("{% raw %}", "{% endraw %}") end it "escapes codeblocks in numbered lists" do code_block = <<~CODE_BLOCK 1. Define your hooks in config file `lefthook.yml` ```yaml pre-push: parallel: true commands: rubocop: run: bundle exec rspec --fail-fast ``` CODE_BLOCK escaped_codeblock = generate_and_parse_markdown(code_block) expect(escaped_codeblock).not_to include("```") expect(escaped_codeblock).not_to include("`") expect(escaped_codeblock).to include("bundle exec rspec --fail-fast") end it "escapes liquid tags in code spans" do code_span = "``{% what %}``" expect(generate_and_parse_markdown(code_span)).to include("{% what %}") end it "renders double backtick code spans properly" do code_span = "``#{random_word}``" expect(generate_and_parse_markdown(code_span)).to include random_word end it "wraps figcaptions with figures" do code_span = "

Statement

\n
A fig
" test = generate_and_parse_markdown("

case:

#{code_span}") expect(test).to eq("

case:

\n
#{code_span}
\n\n\n\n") end it "does not wrap figcaptions already in figures" do code_span = "

Statement

\n
A fig
" test = generate_and_parse_markdown(code_span) expect(test).to eq("#{code_span}\n\n\n\n") end it "does not wrap figcaptions without predecessors" do code_span = "
A fig
" test = generate_and_parse_markdown(code_span) expect(test).to eq("#{code_span}\n\n") end it "converts code tag to triple backticks" do content = "\n this is some random code \n" code_block_object = described_class.new(content) test = code_block_object.convert_code_tags_to_triple_backticks(content) expect(test).not_to include("") expect(test).not_to include("") expect(test).to include("```") end it "converts multiple code tags to triple backticks" do content = "\n this is some random code \n\n\n\n more random code \n" code_block_object = described_class.new(content) test = code_block_object.convert_code_tags_to_triple_backticks(content) expect(test).not_to include("") expect(test).not_to include("") expect(test).to include("```") end it "ignores code tag if pre tag is present" do content = "
\n\n this is some random code \n\n
" code_block_object = described_class.new(content) test = code_block_object.convert_code_tags_to_triple_backticks(content) expect(test).to include("
\n")
    expect(test).to include("\n
") expect(test).not_to include("```") end it "ignores code tag if tags are inline" do content = " this is some random code " code_block_object = described_class.new(content) test = code_block_object.convert_code_tags_to_triple_backticks(content) expect(test).to include("") expect(test).to include("") expect(test).not_to include("```") end it "returns original content if code tag is not present" do content = "this is some random code" code_block_object = described_class.new(content) test = code_block_object.convert_code_tags_to_triple_backticks(content) expect(test).to be(content) end it "permits abbr tags" do result = generate_and_parse_markdown("OK") expect(result).to include("OK") end context "when rendering links markdown" do # the following specs are testing HTMLRouge it "renders properly if protocol http is included" do code_span = "[github](http://github.com)" test = generate_and_parse_markdown(code_span) expect(test).to eq("

github

\n\n") end it "renders properly if protocol https is included" do code_span = "[github](https://github.com)" test = generate_and_parse_markdown(code_span) expect(test).to eq("

github

\n\n") end it "renders properly if protocol is not included" do code_span = "[github](github.com)" test = generate_and_parse_markdown(code_span) expect(test).to eq("

github

\n\n") end it "renders properly relative paths" do code_span = "[career tag](/t/career)" test = generate_and_parse_markdown(code_span) app_protocol = ApplicationConfig["APP_PROTOCOL"] app_domain = ApplicationConfig["APP_DOMAIN"] expect(test).to eq("

career tag

\n\n") end it "renders properly anchored links" do code_span = "[Chapter 1](#chapter-1)" test = generate_and_parse_markdown(code_span) expect(test).to eq("

Chapter 1

\n\n") end it "does not render CSS classes" do expect(generate_and_parse_markdown("
")) .to exclude("class") .and exclude("w-100") end end describe "mentions" do let(:user) { build_stubbed(:user) } before { allow(User).to receive(:find_by).with(username: user.username).and_return(user) } it "works normally" do mention = "@#{user.username}" result = generate_and_parse_markdown(mention) expect(result).to include "" \ "@#{user.username} three four:

\n\n\n\n" # rubocop:enable Layout/LineLength expect(result).to eq(expected_result) end it "will not work in code tag" do mention = "this is a chunk of text `@#{user.username}`" result = generate_and_parse_markdown(mention) expect(result).to include "x{animation:s}@User1 s{}