Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dependency on pegdown #423

Open
Felk opened this issue Nov 10, 2021 · 4 comments
Open

Remove dependency on pegdown #423

Felk opened this issue Nov 10, 2021 · 4 comments

Comments

@Felk
Copy link

Felk commented Nov 10, 2021

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:

  • I couldn't find a 1-to-1 replacement for the timeout being passed.
    Given that flexmark is significantly faster and that the timeout parameter has "pegdown" in its name everywhere, it might be better to just remove the setting altogether.
  • There are very likely a lot of subtle differences to pegdown when using flexmark in its default configuration.
    One could possibly try to add exactly those extensions that were added for pegdown.
    But is that desirable? Which extensions should be enabled by default? All again? This could be (another) breaking change.
@Kabhal
Copy link
Member

Kabhal commented Apr 21, 2022

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
Copy link
Member

RobWin commented Apr 26, 2022

Hi, would you like to create a PR

@Felk
Copy link
Author

Felk commented Apr 26, 2022

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants