A JS library for converting HTML to Markdown.中文
-
speed, none of dependencies,
gizp
10kb -
support
nodeJs
-
200+
unit test
andmodule test
, code coverage97%
Only valid HTML will be output correctly, eg.
<p>abc<
,<i>abc</>
are Not Valid text.
npm i html-to-md
const html2md = require('html-to-md')
// or if you're using ES6
import html2md from 'html-to-md'
console.log(html2md('<strong><em>strong and italic</em></strong>', options))
// ***strong and italic***
name | date type | default value | description |
---|---|---|---|
skipTags | Array |
|
Declare which tags need to skip |
emptyTags | Array | [] |
Skip all the tags inside it |
ignoreTags | Array |
|
Ignore all tag and content inside the tag |
aliasTags | Object |
|
Define an alias tag name |
renderCustomTags | Boolean | 'SKIP' | 'EMPTY' | 'IGNORE' |
true |
Define how to render not valida HTML tags
|
tagListener | Function | (tagName: String, props: TagListenerProps): TagListenerReturnProps => props | Custom the tag props |
Priority:skipTags > emptyTags > ignoreTags > aliasTags
Example:
html2md('<><b><i>abc</i></b></>', { ignoreTags: [''] })
// ''
html2md('<><b><i>abc</i></b></>', { skipTags: [''] })
// ***abc***
html2md('<><b><i>abc</i></b></>', { emptyTags: [''] })
// abc
html2md('<><b><i>abc</i></b></>', {
skipTags: [''],
aliasTags: { b: 'ul', i: 'li' },
})
// * abc
html2md('<test><b><i>abc</i></b></test>', { renderCustomTags: 'SKIP' })
// ***abc***
value | description |
---|---|
true | Overwrite by your custom options |
false | Use Object.assign to combine custom options and default options |
Example:
// The default skipTags value is ['div','html','body']
// ex1:
html2md('<div><b><i>abc</i></b></div>', { skipTags: ['b'] }, false)
// skipTags value become ['div','html','body','b']
// ex2:
html2md('<div><b><i>abc</i></b></div>', { skipTags: ['b'] }, true)
// skipTags value become ['b']
key | 说明 |
---|---|
parentTag | parent tag nam, null if not exist |
prevTagName | previous tag name, null if not exist |
nextTagName | next tag name, null if not exist |
isFirstSubTag | if the current tag is the first tag of its parent tag |
attrs | tag's attributes, format as object, e.g. { src, href ... } |
innerHTML | inner html string |
match | the match symbol of markdown for current tag |
language? | language for pre tag |
isSelfClosing | is the tag a self-closing tag |
key | 说明 |
---|---|
attrs | tag's attributes, format as object, e.g. { src, href ... } |
match | the match symbol of markdown for current tag |
language? | language for pre tag |
a
b
blockquote
code
del
em
h1~h6
hr
i
img
input
li
ol
p
pre
s
strong
table
tbody
td
th
thead
tr
ul