Swagger2Markup / swagger2markup

A Swagger to AsciiDoc or Markdown converter to simplify the generation of an up-to-date RESTful API documentation by combining documentation that’s been hand-written with auto-generated API documentation.
Apache License 2.0
2.49k stars 383 forks source link

Remove dependency on pegdown #423

Open Felk opened 2 years ago

Felk commented 2 years ago

Pegdown has been deprecated for a while, and it appears to not work anymore with Java 17. This suggests that the dependency should be replaced with something else to be able to move forwards. The official recommendation is flexmark-java.

Replacing it could be as simple as this:

Index: swagger2markup-builder/src/main/java/io/github/swagger2markup/markup/builder/internal/asciidoc/AsciiDocConverterExtension.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/swagger2markup-builder/src/main/java/io/github/swagger2markup/markup/builder/internal/asciidoc/AsciiDocConverterExtension.java b/swagger2markup-builder/src/main/java/io/github/swagger2markup/markup/builder/internal/asciidoc/AsciiDocConverterExtension.java
--- a/swagger2markup-builder/src/main/java/io/github/swagger2markup/markup/builder/internal/asciidoc/AsciiDocConverterExtension.java    (revision 9de5ad01579bf9ce25ffa94dc48c1dcde762355c)
+++ b/swagger2markup-builder/src/main/java/io/github/swagger2markup/markup/builder/internal/asciidoc/AsciiDocConverterExtension.java    (date 1636541724091)
@@ -1,29 +1,29 @@
 package io.github.swagger2markup.markup.builder.internal.asciidoc;

-import org.pegdown.Extensions;
-import org.pegdown.PegDownProcessor;
-import org.pegdown.ast.RootNode;
+import com.vladsch.flexmark.html.HtmlRenderer;
+import com.vladsch.flexmark.parser.Parser;
+import com.vladsch.flexmark.util.ast.Node;
+import com.vladsch.flexmark.util.data.MutableDataSet;

-import nl.jworks.markdown_to_asciidoc.Converter;
-import nl.jworks.markdown_to_asciidoc.ToAsciiDocSerializer;
+public class AsciiDocConverterExtension {

-public class AsciiDocConverterExtension extends Converter {
+    private static final Parser parser;
+    private static final HtmlRenderer renderer;
+
+    static {
+        MutableDataSet options = new MutableDataSet();
+        parser = Parser.builder(options).build();
+        renderer = HtmlRenderer.builder(options).build();
+    }

     /**
      * Converts markdown to asciidoc.
      *
      * @param markdown the markdown source to convert
-     * @param timeoutMills parsing timeout
      * @return asciidoc format
      */
     public static String convertMarkdownToAsciiDoc(String markdown, long timeoutMills) {
-        PegDownProcessor processor = new PegDownProcessor(Extensions.ALL, timeoutMills);
-        // insert blank line before fenced code block if necessary
-        if (markdown.contains("```")) {
-            markdown = markdown.replaceAll("(?m)(?<!\n\n)(\\s*)```(\\w*\n)((?:\\1[^\n]*\n)+)\\1```", "\n$1```$2$3$1```");
-        }
-        char[] markDown = markdown.toCharArray();
-        RootNode rootNode = processor.parseMarkdown(markDown);
-        return new ToAsciiDocSerializer(rootNode, markdown).toAsciiDoc();
+        Node document = parser.parse(markdown);
+        return renderer.render(document);
     }
 }
Index: libraries.gradle
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/libraries.gradle b/libraries.gradle
--- a/libraries.gradle  (revision 9de5ad01579bf9ce25ffa94dc48c1dcde762355c)
+++ b/libraries.gradle  (date 1636541268237)
@@ -16,9 +16,8 @@
         commonsText        : "org.apache.commons:commons-text:1.8",
         guava              : 'com.google.guava:guava:27.0.1-android',
         jacksonDatabind    : 'com.fasterxml.jackson.core:jackson-databind:2.9.10',
-        mark2Ascii         : "nl.jworks.markdown_to_asciidoc:markdown_to_asciidoc:1.1",
         paleo              : "ch.netzwerg:paleo-core:0.14.0",
-        pegdown            : "org.pegdown:pegdown:1.6.0",
+        flexmarkJava       : "com.vladsch.flexmark:flexmark-all:0.62.2",
         slf4j              : "org.slf4j:slf4j-api:1.7.28",
         swaggerV2          : "io.swagger:swagger-parser:1.0.47",
         swaggerV2Converter : "io.swagger.parser.v3:swagger-parser-v2-converter:2.0.15",
Index: swagger2markup-builder/build.gradle
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/swagger2markup-builder/build.gradle b/swagger2markup-builder/build.gradle
--- a/swagger2markup-builder/build.gradle   (revision 9de5ad01579bf9ce25ffa94dc48c1dcde762355c)
+++ b/swagger2markup-builder/build.gradle   (date 1636541268256)
@@ -8,9 +8,8 @@
     implementation implLibraries.commonsCodec
     implementation implLibraries.commonsLang3
     implementation implLibraries.commonsIO
-    implementation implLibraries.mark2Ascii
     implementation implLibraries.slf4j
-    implementation implLibraries.pegdown
+    implementation implLibraries.flexmarkJava
     testImplementation testLibraries.assertjDiff
     testImplementation testLibraries.junit
     testImplementation testLibraries.logback

But there are some more problems that need to be adressed:

Kabhal commented 2 years ago

Small workaround, in waiting for this to be merged, for those that want to use swagger2markup with Java 17 and do not use MARDOWN at all.

Set swagger2markup.markupLanguage AND swagger2markup.swaggerMarkupLanguage to ASCIIDOC. This will assume Swagger descriptions are written in Ascidoc and won't call the buggy markdown -> asciidoc converter.

RobWin commented 2 years ago

Hi, would you like to create a PR

Felk commented 2 years ago

Sure I just did, but I am not too invested in doing any more work beyond what I proposed, so I hope it is useful to anyone

Integer93 commented 1 year ago

@RobWin is the linked PR good to go, or does it need more work?