From fba40c09faaa1b7a7901f70ffc29d26baf04f6ee Mon Sep 17 00:00:00 2001 From: pnezis Date: Sun, 1 Oct 2023 22:51:17 +0300 Subject: [PATCH] make recursion generic --- lib/fancy_fences.ex | 26 ++++++++++---------- test/fancy_fences_test.exs | 50 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 13 deletions(-) diff --git a/lib/fancy_fences.ex b/lib/fancy_fences.ex index 0ff6701..5e3ad39 100644 --- a/lib/fancy_fences.ex +++ b/lib/fancy_fences.ex @@ -127,19 +127,6 @@ defmodule FancyFences do end) end - defp maybe_apply_fence_processors( - {:blockquote, attrs, content, meta}, - opts, - applied_processors - ) do - content = - Enum.reduce(content, [], fn block, acc -> - acc ++ maybe_apply_fence_processors(block, opts, applied_processors) - end) - - [{:blockquote, attrs, content, meta}] - end - defp maybe_apply_fence_processors( {:pre, _pre_attrs, [{:code, [class: fence], content, _code_meta}], _pre_meta} = block, opts, @@ -168,5 +155,18 @@ defmodule FancyFences do end end + defp maybe_apply_fence_processors( + {type, attrs, content, meta}, + opts, + applied_processors + ) do + content = + Enum.reduce(content, [], fn block, acc -> + acc ++ maybe_apply_fence_processors(block, opts, applied_processors) + end) + + [{type, attrs, content, meta}] + end + defp maybe_apply_fence_processors(block, _opts, _applied_processors), do: [block] end diff --git a/test/fancy_fences_test.exs b/test/fancy_fences_test.exs index 5d50edf..25dc42c 100644 --- a/test/fancy_fences_test.exs +++ b/test/fancy_fences_test.exs @@ -32,6 +32,12 @@ defmodule FancyFencesTest do ``` """ end + + def dummy(_code) do + """ + Dummy text + """ + end end describe "to_ast/2" do @@ -63,6 +69,50 @@ defmodule FancyFencesTest do assert FancyFences.to_ast(markdown, opts) == ExDoc.Markdown.to_ast(expected) end + test "is applied in deeply nested pre code items" do + markdown = """ + # A heading + + ```dummy + ``` + + > A blockquote + > + > ```dummy + > ``` + > + >> ```dummy + >> ``` + + * A list + * sublist + + ```dummy + ``` + """ + + expected = """ + # A heading + + Dummy text + + > A blockquote + > + > Dummy text + > + >> Dummy text + + * A list + * sublist + + Dummy text + """ + + opts = [fences: %{"dummy" => {Processors, :dummy, []}}] + + assert FancyFences.to_ast(markdown, opts) == ExDoc.Markdown.to_ast(expected) + end + test "no recursion allowed" do markdown = """ ```replace