From 4e5519d00cd93fdcae83c65b82ed2ebb35564aa3 Mon Sep 17 00:00:00 2001 From: Neeraj Date: Wed, 9 Jul 2025 14:22:54 +0530 Subject: [PATCH] Fix: Preserve HTML in runkit source code blocks This change modifies the RunkitTag class to use inner_html instead of text when parsing the content, which preserves HTML tags in the runkit source. Fixes #1228 --- app/liquid_tags/runkit_tag.rb | 3 ++- spec/liquid_tags/runkit_tag_spec.rb | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/app/liquid_tags/runkit_tag.rb b/app/liquid_tags/runkit_tag.rb index e7ce7a4e64642..c3230d9caf020 100644 --- a/app/liquid_tags/runkit_tag.rb +++ b/app/liquid_tags/runkit_tag.rb @@ -77,7 +77,8 @@ def initialize(_tag_name, markup, _parse_context) def render(context) content = Nokogiri::HTML.parse(super) - parsed_content = content.xpath("//html/body").text + # Get the inner HTML instead of just text to preserve HTML tags + parsed_content = content.xpath("//html/body").inner_html ApplicationController.render( partial: PARTIAL, locals: { diff --git a/spec/liquid_tags/runkit_tag_spec.rb b/spec/liquid_tags/runkit_tag_spec.rb index 74dfbf9375ddc..804ff7c9341ed 100644 --- a/spec/liquid_tags/runkit_tag_spec.rb +++ b/spec/liquid_tags/runkit_tag_spec.rb @@ -17,6 +17,19 @@ CODE end + let(:content_with_html) do + <<~CODE + const { ValueViewerSymbol } = require("@runkit/value-viewer"); + + const myCustomObject = { + [ValueViewerSymbol]: { + title: "My First Viewer", + HTML: "๐Ÿ”Hello, World!๐Ÿ”" + } + }; + CODE + end + def generate_runkit_liquid(preamble_str, block) Liquid::Template.register_tag("runkit", described_class) Liquid::Template.parse("{% runkit #{preamble_str}%}#{block}{% endrunkit %}") @@ -31,5 +44,13 @@ def generate_runkit_liquid(preamble_str, block) expect(rendered).to include('await getJSON("https://storage.googleapis.com/maps-devrel/google.json");') # rubocop:enable Style/StringLiterals end + + it "preserves HTML tags in content" do + rendered = generate_runkit_liquid(preamble, content_with_html).render + + # Check that the HTML tags are preserved + expect(rendered).to include('<marquee>๐Ÿ”Hello, World!๐Ÿ”</marquee>') + expect(rendered).not_to include('๐Ÿ”Hello, World!๐Ÿ”') # This would indicate HTML was stripped + end end end