This is an automated email from the ASF dual-hosted git repository.

cziegeler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git


The following commit(s) were added to refs/heads/master by this push:
     new a5cfc3f6 Fix markup and encode title
a5cfc3f6 is described below

commit a5cfc3f6281525e3e2e226083ca73b82c24a664a
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Fri Sep 13 08:10:35 2024 +0200

    Fix markup and encode title
---
 mdresourcedecorator/pom.xml                        |  6 ++++
 .../apache/sling/mdresource/impl/HtmlServlet.java  | 36 +++++++++-------------
 .../mdresource/impl/md/handler/HeadingHandler.java |  4 ++-
 .../impl/md/handler/YamlFrontMatterHandler.java    |  5 +++
 4 files changed, 29 insertions(+), 22 deletions(-)

diff --git a/mdresourcedecorator/pom.xml b/mdresourcedecorator/pom.xml
index 84697250..c707171e 100644
--- a/mdresourcedecorator/pom.xml
+++ b/mdresourcedecorator/pom.xml
@@ -62,6 +62,12 @@
             <artifactId>slf4j-api</artifactId>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>org.owasp.encoder</groupId>
+            <artifactId>encoder</artifactId>
+            <version>1.3.1</version>
+            <scope>provided</scope>
+        </dependency>
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.api</artifactId>
diff --git 
a/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/HtmlServlet.java
 
b/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/HtmlServlet.java
index dcdd726d..aa2f85c5 100644
--- 
a/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/HtmlServlet.java
+++ 
b/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/HtmlServlet.java
@@ -37,6 +37,7 @@ import org.osgi.service.component.annotations.Component;
 import org.osgi.service.metatype.annotations.AttributeDefinition;
 import org.osgi.service.metatype.annotations.Designate;
 import org.osgi.service.metatype.annotations.ObjectClassDefinition;
+import org.owasp.encoder.Encode;
 
 @SlingServletResourceTypes(
     resourceTypes="sling/markdown/file",
@@ -79,58 +80,51 @@ public class HtmlServlet extends HttpServlet {
         resp.setCharacterEncoding("UTF-8");
         final PrintWriter pw = resp.getWriter();
         pw.println("<html>");
-        pw.println("<head>");
+        pw.println("  <head>");
         final ValueMap props = request.getResource().getValueMap();
-        boolean hasMainHeading = true;
         String title = props.get("jcr:title", String.class);
         if ( title == null ) {
             title = props.get("title", String.class);
-            hasMainHeading = false;
         }
         if (title != null) {
-            pw.print("<title>");
-            pw.print(title);
+            pw.print("    <title>");
+            pw.print(Encode.forHtmlContent(title));
             pw.println("</title>");
         }
         if (this.cfg.head_contents() != null) {
             pw.println(this.cfg.head_contents());
         }
-        pw.println("</head>");
-        pw.println("<body>");
-         pw.println("<header>");
+        pw.println("  </head>");
+        pw.println("  <body>");
+        pw.println("    <header>");
         if (this.cfg.header_resource() != null) {
             
request.getRequestDispatcher(this.cfg.header_resource()).include(request, resp);
         }
-        pw.println("</header>");
-        pw.println("<main>");
+        pw.println("    </header>");
+        pw.println("    <main>");
         final Object html = props.get(this.cfg.html_elements_property());
         if (html instanceof String) {
             pw.println(html.toString());
         } else if (html instanceof List) {
-            if (!hasMainHeading) {
-                pw.print("<h1>");
-                pw.print(title);
-                pw.println("</h1>");
-            }
             boolean startSection = true;
             for (final Map.Entry<String, String> element : 
(List<Map.Entry<String,String>>) html) {
                 if (startSection) {
-                    pw.println("<div class=\"section\">");
+                    pw.println("      <div class=\"section\">");
                     startSection = false;
                 }
                 pw.print(element.getValue());
             }
             if (!startSection) {
-                pw.println("</div>");
+                pw.println("      </div>");
             }
         }
-        pw.println("<footer>");
+        pw.println("    </main>");
+        pw.println("    <footer>");
         if (this.cfg.footer_resource() != null) {
             
request.getRequestDispatcher(this.cfg.footer_resource()).include(request, resp);
         }
-        pw.println("</footer>");
-        pw.println("</main>");
-        pw.println("</body>");
+        pw.println("    </footer>");
+        pw.println("  </body>");
         pw.println("</html>");
     }
 }
diff --git 
a/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/md/handler/HeadingHandler.java
 
b/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/md/handler/HeadingHandler.java
index ff40f36e..4184ae69 100644
--- 
a/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/md/handler/HeadingHandler.java
+++ 
b/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/md/handler/HeadingHandler.java
@@ -32,7 +32,9 @@ public class HeadingHandler implements NodeHandler {
         if ( !hasTitle && n instanceof Heading ) {
             final Heading h = (Heading) n;
             if ( h.getLevel() == 1 ) {
-                result.title = h.getText().toString();
+                if (result.title == null) {
+                    result.title = h.getText().toString();
+                }
                 this.hasTitle = true;
             }
         }
diff --git 
a/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/md/handler/YamlFrontMatterHandler.java
 
b/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/md/handler/YamlFrontMatterHandler.java
index 854569c0..5051be90 100644
--- 
a/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/md/handler/YamlFrontMatterHandler.java
+++ 
b/mdresourcedecorator/src/main/java/org/apache/sling/mdresource/impl/md/handler/YamlFrontMatterHandler.java
@@ -32,6 +32,8 @@ import com.vladsch.flexmark.util.ast.Node;
  */
 public class YamlFrontMatterHandler implements NodeHandler {
 
+    private static final String TITLE = "title";
+
     @Override
     public boolean consume(final Node n, final ProcessingResult result) {
         final AbstractYamlFrontMatterVisitor vis = new 
AbstractYamlFrontMatterVisitor();
@@ -47,6 +49,9 @@ public class YamlFrontMatterHandler implements NodeHandler {
                 result.properties.put(entry.getKey(), 
entry.getValue().toArray(new String[0]));
             }
         }
+        if (result.properties.containsKey(TITLE) ) {
+            result.title = result.properties.get(TITLE).toString();
+        }
         return true;
     }
 }

Reply via email to