Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
html2commonmark
Advanced tools
Convert's HTML to markdown using commonmark.js. Compliant with the commmonmark markdown specification.
CommonMark is a rationalized version of Markdown syntax, with a spec and BSD-licensed reference implementations in C and JavaScript. The problem
For more information, see http://commonmark.org.
This repository contains a JavaScript implementation for converting html back to markdown using the same specification. It uses the same Abstract Syntax Tree (AST) as commonmark.js does (and thus has a runtime dependency on it). It even implements all its (600+) examples as mocha unit tests to verify the conversion against the spec. Runs in the browser or on the server using nodejs.
See a live demo at https://nicojs.github.io/html2commonmark
You can install the library using npm
:
npm install html2commonmark --save
This package includes a dependency on commonmark.js.
For client-side use, you can include the node_modules/html2commonmark/dist/browser/bundle.js
on your web page. It exposes the global html2commonmark
variable.
For server-side use, you can simply require it: var html2commonmark = require('html2commonmark');
.
As this npm package is writen in typescript, you can also import the module if you're using "moduleResolution": "node"
: import * as html2commonmark from 'html2commonmark'
.
This will also work for the browser, but than you'll need a tool like webpack or browserify
to package it (instead of using the bundle node_modules/html2commonmark/dist/browser/bundle.js
)
However: you will also need the commonmark typings in order for this to work (using tsd: tsd install commonmark
);
Here's a basic example:
var converter = new html2commonmark.BrowserConverter();
// From nodejs: var converter = new html2commonmark.JSDomConverter();
var renderer = new html2commonmark.Renderer();
var ast = converter.convert('<p>This <i>is</i> <strong>awesome!</strong></p>');
var markdown = renderer.render(ast); // "This *is* **awesome\!**"
The html2commonmark
object provides the following constructor functions:
html2commonmark.Converter
: can convert HTML DOM nodes to the AST nodes.
html2commonmark.BrowserConverter
: can convert HTML to AST nodes using the DOM parser of your browserhtml2commonmark.JSDomConverter
: can convert HTML to AST nodes using the JSDom parser of your browserhtml2commonmark.Renderer
: can convert the AST nodes to markdown.The Converter's take an optional option
parameter for configuring what to do with unknown html elements):
new html2commonmark.BrowserConverter({ // this should be html2commonmark.JSDomConverter in NodeJS
rawHtmlElements: ['div', 'table', 'td', 'tr', 'th', 'tbody', 'thead'],
ignoredHtmlElements: ['custom-root', 'body'],
interpretUnknownHtml: true
});
The following options are supported:
rawHtmlElements
: A (case insensitive) whitelist of html elements which you want to interpret as raw html elements. Default: ['div', 'table', 'td', 'tr', 'th', 'tbody', 'thead']
note: when interpretUnknwonHtml = true, all unknown html nodes will be preserved.ignoreHtmlElements
: A (case insensitive) blacklist of html elements to ignore (not interpret as raw html elements). Default: ['custom-root', 'body']
. note: when interpretUnknwonHtml = false: all unknown html elements will be ignoredinterpretUnknownHtml
: Describes what to do with unknown html elements. Default: true
A more advanced example:
// From nodejs: var converter = new html2commonmark.JSDomConverter();
var converter = new html2commonmark.BrowserConverter({interpretUnknownHtml: false});
var spanConverter = new html2commonmark.BrowserConverter({interpretUnknownHtml: false, rawHtmlElements: ['span']});
var renderer = new html2commonmark.Renderer();
var input = 'a <span>span</span> of <days>days</days>';
var ast = converter.convert(input);
var spanAst = spanConverter.convert(input);
var markdown = renderer.render(ast); // "a span of days"
var markdownWithSpan = renderer.render(spanAst); // "a <span>span</span> of days"
The html2markdown uses an html parser. It uses the JSDom parser for nodejs and uses the parser of the browser for parsing on the client side. This means that some examples of the commonmark specifications are not implemented
Take example 141:
<p>Foo
<a href="bar">
baz</p>
Foo
<a href="bar">
baz
This html is a perfectly valid output of markdown (garbage in-garbage out). But because of our limitations of using the html parser JSDom, we cannot reproduce the exact same markdown.
FAQs
Convert's HTML to markdown using commonmark.js. Compliant with the commmonmark markdown specification.
The npm package html2commonmark receives a total of 39 weekly downloads. As such, html2commonmark popularity was classified as not popular.
We found that html2commonmark demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.