Skip to content

Commit

Permalink
Add markdown extension
Browse files Browse the repository at this point in the history
  • Loading branch information
mcruzdev committed Jun 27, 2024
1 parent 58ae45b commit ebd375d
Show file tree
Hide file tree
Showing 18 changed files with 610 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
* xref:index.adoc[Quarkus Qute Web]
* xref:index.adoc[Quarkus Qute Markdown]
60 changes: 60 additions & 0 deletions docs/modules/ROOT/pages/markdown.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
= Quarkus Qute Markdown

include::./includes/attributes.adoc[]

The goal of this extension is to provide a simple way to render Markdown templates using Qute. It adds a new Qute section
named `markdown` or `md` that can be used to convert the content of the section to HTML using a Markdown processor.

== Installation

To install the extension, add the following dependency to your project:

[source,xml,subs=attributes+]
----
<dependency>
<groupId>io.quarkiverse.qute-markdown</groupId>
<artifactId>quarkus-qute-markdown</artifactId>
<version>${project.version}</version>
</dependency>
----

== Usage

This extension can be combined with the `quarkus-qute-web` extension to render Markdown templates in a web application.

Add the `quarkus-qute-web` extension to your project:

[source,xml,subs=attributes+]
----
<dependency>
<groupId>io.quarkiverse.qute-web</groupId>
<artifactId>quarkus-qute-web</artifactId>
<version>${project.version}</version>
</dependency>
----

Then, create a new Qute template with the `.txt` or `.html` extension.

[source,html]
----
<!-- src/resources/templates/pub/markdown.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Qute Page with Markdown</title>
</head>
<body>
<h1>Hello World!</h1>
{#md}
# This is a Markdown section
It will be converted to HTML using a Markdown processor.
{/md}
</body>
</html>
----

Finally, start your application and open the following URL in your browser: `http://localhost:8080/markdown`.



124 changes: 124 additions & 0 deletions examples/markdown-example/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>io.quarkiverse.qute.web</groupId>
<artifactId>quarkus-qute-web-restclient-example</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
<compiler-plugin.version>3.10.1</compiler-plugin.version>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
<quarkus.platform.version>3.6.6</quarkus.platform.version>
<qute.web.version>3.0.0</qute.web.version>
<qute.web.markdown.version>999-SNAPSHOT</qute.web.markdown.version>
<skipITs>true</skipITs>
<surefire-plugin.version>3.0.0-M7</surefire-plugin.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>${quarkus.platform.artifact-id}</artifactId>
<version>${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-reactive-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkiverse.qute.web</groupId>
<artifactId>quarkus-qute-web</artifactId>
<version>${qute.web.version}</version>
</dependency>
<dependency>
<groupId>io.quarkiverse.qute.web</groupId>
<artifactId>quarkus-qute-web-markdown</artifactId>
<version>${qute.web.markdown.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>${quarkus.platform.version}</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>build</goal>
<goal>generate-code</goal>
<goal>generate-code-tests</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin.version}</version>
<configuration>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<configuration>
<systemPropertyVariables>
<java.util.logging.manager>
org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemPropertyVariables>
<native.image.path>
${project.build.directory}/${project.build.finalName}-runner</native.image.path>
<java.util.logging.manager>
org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>native</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<properties>
<skipITs>false</skipITs>
<quarkus.package.type>native</quarkus.package.type>
</properties>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
quarkus.tls.trust-all=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!-- src/resources/templates/pub/markdown.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Qute Page with Markdown</title>
</head>
<body>
<h1>Hello World!</h1>
{#md}
## This is a Markdown section
It will be converted to HTML using a Markdown processor.
{/md}
</body>
</html>
46 changes: 46 additions & 0 deletions markdown/deployment/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.quarkiverse.qute.web</groupId>
<artifactId>quarkus-qute-web-markdown-parent</artifactId>
<version>999-SNAPSHOT</version>
</parent>
<artifactId>quarkus-qute-web-markdown-deployment</artifactId>
<name>Quarkus Qute Web - Markdown - Deployment</name>

<dependencies>
<dependency>
<groupId>io.quarkiverse.qute.web</groupId>
<artifactId>quarkus-qute-web-markdown</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-qute-deployment</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-extension-processor</artifactId>
<version>${quarkus.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.quarkiverse.qute.web.markdown.deployment;

import io.quarkiverse.qute.web.markdown.runtime.QuteMarkdownConfigurator;
import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.FeatureBuildItem;

class QuteWebMarkdownProcessor {

private static final String FEATURE = "qute-web-markdown";

@BuildStep
FeatureBuildItem feature() {
return new FeatureBuildItem(FEATURE);
}

@BuildStep
void initMarkdown(BuildProducer<AdditionalBeanBuildItem> beans) {
beans.produce(new AdditionalBeanBuildItem(QuteMarkdownConfigurator.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.quarkiverse.qute.web.markdown.test;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import io.quarkiverse.qute.web.markdown.runtime.MarkdownSectionHelper;
import io.quarkiverse.qute.web.markdown.runtime.commonmark.CommonMarkConverter;
import io.quarkus.qute.Engine;

public class QuarkusMarkdownTest {

@Test
public void testMd() {
Engine engine = Engine.builder().addDefaults()
.addSectionHelper(new MarkdownSectionHelper.Factory(new CommonMarkConverter())).build();
assertEquals("<p>...</p>\n", engine.parse("{#md}...{/md}").render());
}

@Test
public void testMarkdown() {
Engine engine = Engine.builder().addDefaults()
.addSectionHelper(new MarkdownSectionHelper.Factory(new CommonMarkConverter())).build();
assertEquals("<p>...</p>\n", engine.parse("{#markdown}...{/markdown}").render());
}

@Test
public void testH1() {
Engine engine = Engine.builder().addDefaults()
.addSectionHelper(new MarkdownSectionHelper.Factory(new CommonMarkConverter())).build();
assertEquals("<h1>Quarkus and Roq</h1>\n", engine.parse("{#md}# Quarkus and Roq{/md}").render());
}

@Test
void testJsonObjectValueResolver() {

Engine engine = Engine.builder().addDefaults()
.addSectionHelper(new MarkdownSectionHelper.Factory(new CommonMarkConverter())).build();

String result = engine.parse("""
<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}
""").data("items", List.of("apple", "banana", "cherry"))
.render();

Assertions.assertEquals("""
<h1>Quarkus and Qute</h1>
<h1>Qute and Roq</h1>
<p>Here is a list:</p>
<ul>
<li>an apple as a list item</li>
<li>an banana as a list item</li>
<li>an cherry as a list item</li>
</ul>
""", result);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.quarkiverse.qute.web.markdown.test;

import jakarta.inject.Inject;

import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.qute.Template;
import io.quarkus.test.QuarkusUnitTest;

public class QuteMarkdownSectionHelperTest {

@RegisterExtension
static final QuarkusUnitTest quarkusApp = new QuarkusUnitTest()
.withApplicationRoot(
app -> app.addAsResource(new StringAsset(
"{#markdown}# Qute and Roq{/markdown}"),
"templates/foo.txt"));

@Inject
Template foo;

@Test
void testJsonObjectValueResolver() {
String result = foo.render();

System.out.println(result);

Assertions.assertEquals("<h1>Qute and Roq</h1>\n", result);
}
}
Loading

0 comments on commit ebd375d

Please sign in to comment.