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

HTML to Markdown (MD) #582

Closed
dace opened this issue Sep 8, 2018 · 12 comments
Closed

HTML to Markdown (MD) #582

dace opened this issue Sep 8, 2018 · 12 comments
Assignees

Comments

@dace
Copy link

dace commented Sep 8, 2018

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!

@tivie tivie self-assigned this Sep 14, 2018
@tivie
Copy link
Member

tivie commented Sep 14, 2018

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 converter.makeMarkdown(htmlText)

@maciej-kolodziejczak
Copy link

@tivie After cloning a master brach I get a Module not found: Error: Can't resolve 'tls' in [...]/node_modules/tunnel-agent error. Any clues?

@andosteinmetz
Copy link

andosteinmetz commented Oct 31, 2018

@maciej-kolodziejczak cd into the showdown directory and npm install. this cleared up those errors for me.

@dessalines
Copy link

dessalines commented Dec 4, 2018

Okay I just ran into this, How exactly to I use this to convert html to markdown? npm link or something?

@mattkwiecien
Copy link

@tivie solved this in e4b0e69 (issue closed here #233 (comment))

and this issue can probably also be closed.

@tivie tivie closed this as completed Jan 13, 2019
@verheyenkoen
Copy link

Is there a way to use this in Node.js, as this seems to be using the window object?

@ilyaskarim
Copy link

ilyaskarim commented Nov 5, 2020

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)

@sojs-coder
Copy link

I tried using the converter.makeMarkdown() in my code.. but it throws the error window is not defined.
I am in a node enviroment.
Is there some way to do the same but in node?

@karthik2206
Copy link

I am facing the window is not defined error too when I am using converter.makeMarkdown(). Again in a node environment.

@cchambers
Copy link

cchambers commented Jan 12, 2024

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:

<div>
  <h2>some title</h2>
  <p>some text</p>
</div>

Whereas the following returns markdown:

<h2>some title</h2>
<p>some text</p>

@prashantbhudwal
Copy link

prashantbhudwal commented Feb 17, 2024

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
Converts an HTML string into a markdown 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);

@meAyushSharma
Copy link

On the backend, you need to pass the DOM parser. According to the docs of the function.

(method) Showdown.Converter.makeMarkdown(src: string, HTMLParser?: HTMLDocument | undefined): string Converts an HTML string into a markdown 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!

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

No branches or pull requests