Marked is a powerful JavaScript library known for its speed and efficiency. It serves as a markdown parser, adept at translating Markdown syntax into HTML. Despite its lightweight nature, Marked manages to implement all markdown features from the supported flavors and specifications. Additionally, it displays great versatility as it can operate not only on a server or from a command line interface (CLI) but also in a browser.
To utilize the Marked library, installation is required and can be achieved by running the command npm install -g marked
for a command-line interface, or npm install marked
for in-browser installation. Once installed, marked can be used to parse markdown strings to HTML. However, it's crucial to remember that Marked does not sanitize the output HTML. As such, to prevent an XSS attack, use a sanitization library such as DOMPurify, sanitize-html, or insane on the output HTML. An example of usage includes:
In the browser, you should include the following JavaScript code:
<div id="content"></div>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<script>
document.getElementById('content').innerHTML =
marked.parse('# Marked in the browser\n\nRendered by **marked**.');
</script>
For a command-line interface, you may use:
marked -o hello.html
hello world
^D
At this point, hello.html
will contain the parsed HTML - <p>hello world</p>
.
Detailed information, guidelines, and additional resources about Marked are readily available on the official documentation site found at https://marked.js.org. The documentation provides comprehensive details about various options for advanced usage and extensibility of the Marked library at https://marked.js.org/#/USING_ADVANCED.md and https://marked.js.org/#/USING_PRO.md respectively.