From 65b1dedac547ca5a89e5613554e8ddacf8ae1018 Mon Sep 17 00:00:00 2001 From: James Meng Date: Thu, 1 May 2025 09:11:15 -0700 Subject: [PATCH] Expose tag body in the Doc tag --- lib/liquid/tags/doc.rb | 5 +++++ test/unit/tags/doc_tag_unit_test.rb | 15 +++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/liquid/tags/doc.rb b/lib/liquid/tags/doc.rb index 2138b641..4c650b8c 100644 --- a/lib/liquid/tags/doc.rb +++ b/lib/liquid/tags/doc.rb @@ -30,8 +30,11 @@ module Liquid class Doc < Block NO_UNEXPECTED_ARGS = /\A\s*\z/ + attr_reader :body + def initialize(tag_name, markup, parse_context) super + @body = +"" ensure_valid_markup(tag_name, markup, parse_context) end @@ -43,8 +46,10 @@ module Liquid if tag_name == block_delimiter parse_context.trim_whitespace = (token[-3] == WhitespaceControl) + @body << Regexp.last_match(1) if Regexp.last_match(1) != "" return end + @body << token unless token.empty? end raise_tag_never_closed(block_name) diff --git a/test/unit/tags/doc_tag_unit_test.rb b/test/unit/tags/doc_tag_unit_test.rb index a0a36629..1d5db06d 100644 --- a/test/unit/tags/doc_tag_unit_test.rb +++ b/test/unit/tags/doc_tag_unit_test.rb @@ -20,6 +20,21 @@ class DocTagUnitTest < Minitest::Test assert_template_result('', template) end + def test_doc_tag_body_content + doc_content = " Documentation content\n @param {string} foo - test\n" + template_source = "{% doc %}#{doc_content}{% enddoc %}" + + doc_tag = nil + ParseTreeVisitor + .for(Template.parse(template_source).root) + .add_callback_for(Liquid::Doc) do |tag| + doc_tag = tag + end + .visit + + assert_equal(doc_content, doc_tag.body) + end + def test_doc_tag_does_not_support_extra_arguments error = assert_raises(Liquid::SyntaxError) do template = <<~LIQUID.chomp