mixmark-io / turndown

🛏 An HTML to Markdown converter written in JavaScript
https://mixmark-io.github.io/turndown
MIT License
8.94k stars 881 forks source link

Please clarify how to run this code with a complete example #465

Closed Deanozk closed 6 months ago

Deanozk commented 6 months ago

var TurndownService = require('turndown') ^ ReferenceError: require is not defined in ES module scope, you can use import instead

This is not the best way to startout using a library.

pavelhoral commented 6 months ago

There is a three line example in the README which is as complete example as you can get.

// For Node.js
var TurndownService = require('turndown')

var turndownService = new TurndownService()
var markdown = turndownService.turndown('<h1>Hello world!</h1>')

The first line (as indicated by the comment) is only for NodeJS environment (specifically for CommonJS modules). The only thing that might be added is that in ESM environment you should use import statement instead of require (possibly your use-case?). But that seems like trivial thing to deduce.

You might want to try this:

import TurndownService from 'turndown'

var turndownService = new TurndownService()
var markdown = turndownService.turndown('<h1>Hello world!</h1>')

That being said, this is not a better example than the one in the README.