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

[PLAY-1428] Fix Undefined Method Error in SamplesController#show #3675

Merged
merged 2 commits into from
Sep 13, 2024
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
3 changes: 3 additions & 0 deletions playbook-website/lib/markdown_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ def self.cache_key(text)
def render_code(text, language)
formatter = Rouge::Formatters::HTML.new(scope: ".highlight")
lexer = Rouge::Lexer.find(language)

lexer = Rouge::Lexers::PlainText.new if lexer.nil?

formatter.format(lexer.lex(text))
end

Expand Down
47 changes: 47 additions & 0 deletions playbook-website/spec/helpers/markdown_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

# spec/helpers/markdown_helper_spec.rb
require "rails_helper"

RSpec.describe PlaybookWebsite::Markdown::Helper do
describe "#render_code" do
let(:helper) { Class.new { include PlaybookWebsite::Markdown::Helper }.new }
let(:formatter) { instance_double(Rouge::Formatters::HTML) }
let(:ruby_lexer) { instance_double(Rouge::Lexers::Ruby) }
let(:plain_text_lexer) { instance_double(Rouge::Lexers::PlainText) }

before do
allow(Rouge::Formatters::HTML).to receive(:new).and_return(formatter)
allow(formatter).to receive(:format).and_return("<formatted_code>")
end

it "formats code with the correct lexer when language is found" do
allow(Rouge::Lexer).to receive(:find).with("ruby").and_return(ruby_lexer)
allow(ruby_lexer).to receive(:lex).with("puts 'Hello, World!'").and_return("lexed_code")

expect(formatter).to receive(:format).with("lexed_code")
result = helper.render_code("puts 'Hello, World!'", "ruby")
expect(result).to eq("<formatted_code>")
end

it "uses PlainText lexer when language is not found" do
allow(Rouge::Lexer).to receive(:find).with("unknown_language").and_return(nil)
allow(Rouge::Lexers::PlainText).to receive(:new).and_return(plain_text_lexer)
allow(plain_text_lexer).to receive(:lex).with("Some plain text").and_return("lexed_plain_text")

expect(formatter).to receive(:format).with("lexed_plain_text")
result = helper.render_code("Some plain text", "unknown_language")
expect(result).to eq("<formatted_code>")
end

it "handles nil language parameter" do
allow(Rouge::Lexer).to receive(:find).with(nil).and_return(nil)
allow(Rouge::Lexers::PlainText).to receive(:new).and_return(plain_text_lexer)
allow(plain_text_lexer).to receive(:lex).with("Some text").and_return("lexed_text")

expect(formatter).to receive(:format).with("lexed_text")
result = helper.render_code("Some text", nil)
expect(result).to eq("<formatted_code>")
end
end
end
Loading