quarkiverse / quarkus-qute-web

Automatically expose Qute templates via HTTP
Apache License 2.0
4 stars 5 forks source link

Add markdown extension #92

Open mcruzdev opened 1 week ago

mcruzdev commented 1 week ago

Fixes #91

mcruzdev commented 1 week ago

Hi @ia3andy, this is the first draft, to see if I am on the right path

ia3andy commented 1 week ago

This looks good right @mkouba ?

ia3andy commented 1 week ago

Could you add this test:

<h1>some html</h2>

{#md}
# Hello markdown

Here is a list:
{#for item in items}
* an {item} as a list
{/for}

<a href="/path">some {html} in the markdown</a>
{/md}
mcruzdev commented 1 week ago

Could you add this test:

<h1>some html</h2>

{#md}
# Hello markdown

Here is a list:
{#for item in items}
* an {item} as a list
{/for}

<a href="/path">some {html} in the markdown</a>
{/md}

Probably this one will fail 😆

mcruzdev commented 1 week ago

Thank you for all your comments, I will take a look here

mcruzdev commented 1 week ago

Hi @mkouba and @ia3andy, actually when we are using this one:

  <h1>Quarkus and Qute</h1>
  {#md}
  # Qute and Roq
  Here is a list:
  {#for item in items}
  - an {item} as a list item
  {/for}
  {/md}

We are getting:

<h1>Quarkus and Qute</h1>
<h1>Qute and Roq</h1>
<p>Here is a list:</p>
<ul>
<li>an</li>
</ul>
<p>apple</p>
<p>as a list item</p>
<ul>
<li>an</li>
</ul>
<p>banana</p>
<p>as a list item</p>
<ul>
<li>an</li>
</ul>
<p>cherry</p>
<p>as a list item</p>

I think that the expected behavior is:

  1. Resolve{#for} (more specific section);
  2. Resolve {#md} with {#for} result;

In other words, resolve all sections inside the markdown first. I am new to qute API, do you know how to solve this one?

mkouba commented 1 week ago

In other words, resolve all sections inside the markdown first. I am new to qute API, do you know how to solve this one?

@mcruzdev Your code looks good. I think there's some problem with line separators or something like that. What does the content passed to the MarkdownConverter look like?

mkouba commented 1 week ago

Ah, I see where the problem is. You need to first collect all the results and then apply the converter:

    public CompletionStage<ResultNode> resolve(SectionResolutionContext context) {
        return context.execute().thenCompose(rn -> {
            StringBuilder sb = new StringBuilder();
            rn.process(sb::append);
            return CompletedStage.of(new SingleResultNode(markdownConverter.convert(sb.toString())));
        });
    }
mkouba commented 1 week ago

By the way, you don't need a QuarkusUnitTest to test the functionality of the section itself. A simple unit test would be enough:

public class MdTest {
    @Test
    public void testMd() {
        Engine engine = Engine.builder().addDefaults()
                .addSectionHelper(new MarkdownSectionHelper.Factory(new CommonMarkConverter())).build();
        assertEquals("...", engine.parse("{#md}...{/md}").data("items", List.of("apple", "pie")).render());
    }
}
ia3andy commented 4 days ago

@mkouba good?

mkouba commented 2 days ago

@mcruzdev Could you pls squash the commits? I'll merge the PR afterwards...