showdownjs / showdown

A bidirectional Markdown to HTML to Markdown converter written in Javascript
http://www.showdownjs.com/
MIT License
14.1k stars 1.57k forks source link

No documentation on makeMarkdown function #966

Open BlackDragonBE opened 1 year ago

BlackDragonBE commented 1 year ago

The description of this converter reads "A bidirectional Markdown to HTML to Markdown converter written in Javascript". This was my main reason for using this package as I need to convert both ways.

However, I can't find any examples or documentation on how convert HTML to markdown using makeMarkdown().

SARAsBooks commented 1 year ago

Same. I'm trying:

const converter = new Showdown.Converter();
const markdown = converter.makeMarkdown(html);

And I get ReferenceError: window is not defined

shuiRong commented 1 year ago

same error.

shuiRong commented 1 year ago

@SARAsBooks I got it.

the correct way is:

import showdown from 'showdown';
import { JSDOM } from 'jsdom';

const converter = new showdown.Converter();

const text = `<h1 id="hellomarkdown">hello, markdown!</h1>`;

const doc = new JSDOM(text);

const html = converter.makeMarkdown(text, doc.window.document);
console.log('html', html);

here is the type:

/**
         * 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.
         */
        makeMarkdown(src: string, HTMLParser?: HTMLDocument): string;

If the code is used in Nodejs, you must provide a virtual document, otherwise the code will use window.document by default, and then it will report an error: ReferenceError: window is not defined