-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
HTML to Markdown (MD) #582
Comments
HTML to MD converter is a new feature, available only in master branch. The documentation hasn't been written yet. To use it you can clone the master branch and use |
@tivie After cloning a master brach I get a |
@maciej-kolodziejczak |
Okay I just ran into this, How exactly to I use this to convert html to markdown? npm link or something? |
@tivie solved this in e4b0e69 (issue closed here #233 (comment)) and this issue can probably also be closed. |
Is there a way to use this in Node.js, as this seems to be using the window object? |
I am late here but with an answer: const converter = new showdown.Converter();
// To HTML from Markdown:
converter.makeHtml(__YOUR_MARKDOWN)
// To Markdown from HTML:
converter.makeMarkdown(__YOUR_HTML) |
I tried using the |
I am facing the |
What I've discovered is that it does work (at least on the frontend) but only if everything you're trying to convert is at the top level. For instance, this will just return the HTML:
Whereas the following returns markdown:
|
Frontend has a window object, back end does not. So, on the backend, you need to pass the DOM parser. This will help in parsing your HTML to markdown. According to the docs of the function: (method) Showdown.Converter.makeMarkdown(src: string, HTMLParser?: HTMLDocument | undefined): string @param src — The input text (HTML) @param HTMLParser — A WHATWG DOM and HTML parser, such as JSDOM. If none is supplied, window.document will be used. @returns — The output markdown. Example. This works on node. const convertor = new showdown.Converter();
const dom = new JSDOM(result.value);
console.log("Parsing HTML");
const parsedHTML = dom.window.document;
const markdown = convertor.makeMarkdown(result.value, parsedHTML);
console.log("Parsed markdown", markdown); |
Thanks it helped! |
Can confirm, even after correctly invoking everything in the nodejs environment the makeMarkdown function is still pretty useless as it won't do much at converting html documents with nested tags... |
Hey! For some reason I run into weird issues with this setup. Here is a reproductible example on showdown 2.1.0: import showdown from "showdown";
import { JSDOM } from "jsdom";
/**
* The converter used across all the app
*/
export const converter = new showdown.Converter({
simplifiedAutoLink: true,
headerLevelStart: 1,
strikethrough: true,
tasklists: false,
tables: false,
simpleLineBreaks: true,
ghMentions: false,
openLinksInNewWindow: true,
emoji: true
});
const html = `<h1>Hello!</h1>
<p>This is a test <br /> say hello!</p>`;
const dom = new JSDOM(html);
const document = dom.window.document;
const markdown = converter.makeMarkdown(html, document);
console.log(markdown); What is weird is that the output on the console is this:
I have so many questions on why does it does this. It keep Can someone help? |
Thanks for all the great work.
The repo describes showdown as "A bidirectional MD to HTML to MD converter written in Javascript ", but I can't find how to convert HTML to MD. Is this supported? I can't seem to find it in the documentation.
Thank you!
The text was updated successfully, but these errors were encountered: