<p>is there an easy way to transform HTML into markdown with JAVA?</p>

<p>I am currently using the Java <strong><a href="http://code.google.com/p/markdownj/" rel="noreferrer">MarkdownJ</a></strong> library to transform markdown to html.</p>

<pre><code>import com.petebevin.markdown.MarkdownProcessor;
...
public static String getHTML(String markdown) {
    MarkdownProcessor markdown_processor = new MarkdownProcessor();
    return markdown_processor.markdown(markdown);
}

public static String getMarkdown(String html) {
/* TODO Ask stackoverflow */
}
</code></pre>

[{'answer_id': 178278, 'author': 'myabc', 'author_id': 3789, 'author_profile': 'https://Stackoverflow.com/users/3789', 'pm_score': 2, 'selected': False, 'text': '<p>I am working on the same issue, and experimenting with a couple different techniques.</p>\n\n<p>The answer above could work. You could use the <a href="http://jtidy.sourceforge.net/" rel="nofollow noreferrer">jTidy library</a> to do the initial cleanup work and convert from HTML to XHTML. You use the <a href="http://www.lowerelement.com/Geekery/XML/XHTML-to-Markdown.html" rel="nofollow noreferrer">XSLT stylesheet</a> linked above.</p>\n\n<p>Unfortunately there is no library that has a one-stop function to do this in Java. You could try using the Python script <a href="http://www.aaronsw.com/2002/html2text/" rel="nofollow noreferrer">html2text</a> with Jython, but I haven\'t yet tried this!</p>\n'}, {'answer_id': 741062, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>if you are using WMD editor and want to get the markdown code on the server side, just use these options before loading the <code>wmd.js</code> script:</p>\n\n<pre><code>wmd_options = {\n        // format sent to the server.  can also be "HTML"\n        output: "Markdown",\n\n        // line wrapping length for lists, blockquotes, etc.\n        lineLength: 40,\n\n        // toolbar buttons.  Undo and redo get appended automatically.\n        buttons: "bold italic | link blockquote code image | ol ul heading hr",\n\n        // option to automatically add WMD to the first textarea found.\n        autostart: true\n    };\n</code></pre>\n'}, {'answer_id': 62105480, 'author': 'Gabriel Furstenheim', 'author_id': 1536133, 'author_profile': 'https://Stackoverflow.com/users/1536133', 'pm_score': 4, 'selected': False, 'text': '<p>There is a great library for JS called <a href="https://github.com/domchristie/turndown" rel="nofollow noreferrer">Turndown</a>, you can try it online <a href="https://mixmark-io.github.io/turndown/" rel="nofollow noreferrer">here</a>. It works for htmls that the accepted answer errors out.</p>\n<p>I needed it for Java (as the question), so I ported it. The library for Java is called <a href="https://github.com/furstenheim/copy-down" rel="nofollow noreferrer">CopyDown</a>, it has the same test suite as Turndown and I\'ve tried it with real examples that the accepted answer was throwing errors.</p>\n<p>To install with gradle:</p>\n<pre><code>dependencies {\n        compile \'io.github.furstenheim:copy_down:1.0\'\n}\n</code></pre>\n<p>Then to use it:</p>\n<pre class="lang-java prettyprint-override"><code>CopyDown converter = new CopyDown();\nString myHtml = &quot;&lt;h1&gt;Some title&lt;/h1&gt;&lt;div&gt;Some html&lt;p&gt;Another paragraph&lt;/p&gt;&lt;/div&gt;&quot;;\nString markdown = converter.convert(myHtml);\nSystem.out.println(markdown);\n&gt; Some title\\n==========\\n\\nSome html\\n\\nAnother paragraph\\n\n</code></pre>\n<p>PS. It has MIT license</p>\n'}, {'answer_id': 70212129, 'author': 'Mahozad', 'author_id': 8583692, 'author_profile': 'https://Stackoverflow.com/users/8583692', 'pm_score': 0, 'selected': False, 'text': '<p>There is a Haskell library called <a href="https://github.com/jgm/pandoc" rel="nofollow noreferrer">pandoc</a> that can convert between most markup formats.<br />\nAlthough it is not a Java library, it can be <a href="https://stackoverflow.com/a/8496537/8583692">used through its CLI in Java</a>.</p>\n<p>You can get and <a href="https://github.com/jgm/pandoc/releases/latest" rel="nofollow noreferrer">install the latest version from here</a>. Read the <a href="https://pandoc.org/getting-started.html" rel="nofollow noreferrer">getting started guides here</a>.</p>\n<pre class="lang-java prettyprint-override"><code>var command = &quot;pandoc --to=markdown_strict --output=result.md input.html&quot;;\nvar pandoc = new ProcessBuilder()\n        .command(command.split(&quot; &quot;))\n        .directory(new File(&quot;.&quot;)) // Working directory\n        .start();\npandoc.waitFor();\n// The output result.md will be created in the working directory\n</code></pre>\n<p>This tool can also be <a href="https://github.com/pandoc/pandoc-action-example" rel="nofollow noreferrer">used in GitHub Actions workflows</a>.</p>\n'}, {'answer_id': 72139380, 'author': 'Fabian Sponholz', 'author_id': 19051614, 'author_profile': 'https://Stackoverflow.com/users/19051614', 'pm_score': 2, 'selected': False, 'text': '<p>There is a Java Library called <a href="https://github.com/vsch/flexmark-java" rel="nofollow noreferrer">flexmark</a> which has such a feature.\nMaven Dependency:</p>\n<pre><code>&lt;dependency&gt;\n    &lt;groupId&gt;com.vladsch.flexmark&lt;/groupId&gt;\n    &lt;artifactId&gt;flexmark-html2md-converter&lt;/artifactId&gt;\n    &lt;version&gt;0.64.0&lt;/version&gt;\n&lt;/dependency&gt;\n</code></pre>\n<p>Using the class <code>com.vladsch.flexmark.html2md.converter.FlexmarkHtmlConverter</code> you can convert an HTML String to a Markdown String in one line like this:</p>\n<pre><code>String md = FlexmarkHtmlConverter.builder().build().convert(html);\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/59557', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2138/']