~jan0sch/smederee

Showing details for patch 3f5a64791e03a1e0de75a97b7e56c8648ab3d8b3.
2022-11-06 (Sun), 7:28 PM - Jens Grassel - 3f5a64791e03a1e0de75a97b7e56c8648ab3d8b3

VCS: Always render markdown files when browsing a repository.

Summary of changes
4 files modified with 30 lines added and 15 lines removed
  • CHANGELOG.md with 1 added and 0 removed lines
  • modules/hub/src/main/scala/de/smederee/hub/MarkdownRenderer.scala with 10 added and 7 removed lines
  • modules/hub/src/main/scala/de/smederee/hub/VcsRepositoryRoutes.scala with 14 added and 7 removed lines
  • modules/hub/src/main/twirl/de/smederee/hub/views/showRepositoryFiles.scala.html with 5 added and 1 removed lines
diff -rN -u old-smederee/CHANGELOG.md new-smederee/CHANGELOG.md
--- old-smederee/CHANGELOG.md	2025-02-01 15:47:28.854001819 +0000
+++ new-smederee/CHANGELOG.md	2025-02-01 15:47:28.854001819 +0000
@@ -23,6 +23,7 @@
 ### Added
 
 - use the CSRF protection middleware of http4s
+- always render markdown files when browsing the repository
 
 ### Fixed
 
diff -rN -u old-smederee/modules/hub/src/main/scala/de/smederee/hub/MarkdownRenderer.scala new-smederee/modules/hub/src/main/scala/de/smederee/hub/MarkdownRenderer.scala
--- old-smederee/modules/hub/src/main/scala/de/smederee/hub/MarkdownRenderer.scala	2025-02-01 15:47:28.854001819 +0000
+++ new-smederee/modules/hub/src/main/scala/de/smederee/hub/MarkdownRenderer.scala	2025-02-01 15:47:28.854001819 +0000
@@ -30,18 +30,17 @@
   private val log = LoggerFactory.getLogger(getClass())
 
   /** Render the given markdown sources and adjust all relative links by prefixing them with the path for the repostiory
-    * file browsing (`repo-name/files`). This function is implemented to work with the README file shown on the
-    * repository overview page.
+    * file browsing (`repo-name/files`).
     *
     * @param repo
-    *   The repository which contains the markdown sources.
+    *   The repository which contains the markdown sources. If the option is empty then no URI path prefix will be set.
     * @param markdownSource
     *   A string containing the markdown sources to be rendered (usually the content of the README.md file in the
     *   repository root).
     * @return
     *   A string containing the rendered markdown (HTML).
     */
-  def renderRepositoryOverviewReadme(repo: Option[VcsRepository])(markdownSource: String): String = {
+  def renderRepositoryMarkdownFile(repo: Option[VcsRepository])(markdownSource: String): String = {
     val parser   = Parser.builder().build()
     val markdown = parser.parse(markdownSource)
     val renderer = HtmlRenderer
@@ -59,7 +58,7 @@
     * are not absolute.
     *
     * @param repo
-    *   The repository which contains the markdown sources.
+    *   The repository which contains the markdown sources. If the option is empty then no URI path prefix will be set.
     */
   class LinkHrefCorrector(private val repo: Option[VcsRepository]) extends AttributeProvider {
     override def setAttributes(node: Node, tagName: String, attributes: java.util.Map[String, String]): Unit =
@@ -67,8 +66,12 @@
         (repo, attributes.asScala.get("href").flatMap(href => Uri.fromString(href).toOption)).mapN {
           case (repository, uri) =>
             if (uri.scheme.isEmpty) {
-              val pathPrefix   = Uri.Path(Vector(Uri.Path.Segment(repository.name.toString), Uri.Path.Segment("files")))
-              val correctedUri = uri.copy(path = pathPrefix |+| uri.path)
+              val pathPrefix = Uri.Path(Vector(Uri.Path.Segment(repository.name.toString), Uri.Path.Segment("files")))
+              val correctedUri =
+                if (uri.path.startsWith(pathPrefix))
+                  uri
+                else
+                  uri.copy(path = pathPrefix |+| uri.path)
               log.debug(s"Corrected URI for repository overview README rendering: $uri -> $correctedUri")
               attributes.put("href", correctedUri.toString)
             }
diff -rN -u old-smederee/modules/hub/src/main/scala/de/smederee/hub/VcsRepositoryRoutes.scala new-smederee/modules/hub/src/main/scala/de/smederee/hub/VcsRepositoryRoutes.scala
--- old-smederee/modules/hub/src/main/scala/de/smederee/hub/VcsRepositoryRoutes.scala	2025-02-01 15:47:28.854001819 +0000
+++ new-smederee/modules/hub/src/main/scala/de/smederee/hub/VcsRepositoryRoutes.scala	2025-02-01 15:47:28.854001819 +0000
@@ -319,7 +319,9 @@
       readme <- readmeData match {
         case Some((lines, Some(filename))) =>
           if (filename.matches("(?iu).*\\.(md|markdown)$")) {
-            Sync[F].delay(MarkdownRenderer.renderRepositoryOverviewReadme(repo)(lines.mkString("\n"))).map(_.some)
+            Sync[F]
+              .delay(MarkdownRenderer.renderRepositoryMarkdownFile(repo)(lines.mkString("\n")))
+              .map(_.some)
           } else {
             Sync[F].delay(lines.mkString("\n").some)
           }
@@ -414,12 +416,6 @@
             else
               Sync[F].delay(IndexedSeq.empty)
         }
-      fileContent <- Sync[F].delay {
-        if (content.isEmpty)
-          None
-        else
-          Option(content.mkString("\n"))
-      }
       repositoryBaseUri <- Sync[F].delay(
         linkConfig.createFullUri(
           Uri(path =
@@ -437,6 +433,17 @@
           Uri(path = Uri.Path(actionBaseUri.path.segments.reverse.drop(1).reverse))
         )
       )
+      fileContent <- content.isEmpty match {
+        case false =>
+          if (actionBaseUri.path.toString.toLowerCase(java.util.Locale.ROOT).endsWith(".md"))
+            Sync[F].delay(
+              MarkdownRenderer.renderRepositoryMarkdownFile(None)(content.mkString("\n")).some
+            )
+          else
+            Sync[F].delay(content.mkString("\n").some)
+        case true =>
+          Sync[F].pure(None)
+      }
       resp <-
         repo match {
           case None => NotFound("Repository not found!")
diff -rN -u old-smederee/modules/hub/src/main/twirl/de/smederee/hub/views/showRepositoryFiles.scala.html new-smederee/modules/hub/src/main/twirl/de/smederee/hub/views/showRepositoryFiles.scala.html
--- old-smederee/modules/hub/src/main/twirl/de/smederee/hub/views/showRepositoryFiles.scala.html	2025-02-01 15:47:28.854001819 +0000
+++ new-smederee/modules/hub/src/main/twirl/de/smederee/hub/views/showRepositoryFiles.scala.html	2025-02-01 15:47:28.854001819 +0000
@@ -60,7 +60,11 @@
               <a href="@link"><i class="fa-solid fa-angle-up"></i></a>&nbsp;<a href="@link">..</a>
             }
             @for(content <- fileContent) {
-              <pre class="repository-file-content"><code>@content</code></pre>
+              @if(actionBaseUri.path.toString.toLowerCase(java.util.Locale.ROOT).endsWith(".md")) {
+                @Html(content)
+              } else {
+                <pre class="repository-file-content"><code>@content</code></pre>
+              }
             }
           }
         </div>