https://github.com/stonehank/html-to-md
A JS library for convert HTML<String> to markdown<String>, gzip 10kb
https://github.com/stonehank/html-to-md
html javascript markdown nodejs
Last synced: 7 months ago
JSON representation
A JS library for convert HTML<String> to markdown<String>, gzip 10kb
- Host: GitHub
- URL: https://github.com/stonehank/html-to-md
- Owner: stonehank
- License: mit
- Created: 2019-06-13T06:44:41.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2025-04-27T13:05:00.000Z (12 months ago)
- Last Synced: 2025-08-09T08:19:21.832Z (8 months ago)
- Topics: html, javascript, markdown, nodejs
- Language: JavaScript
- Homepage: https://stonehank.github.io/html-to-md/
- Size: 964 KB
- Stars: 149
- Watchers: 1
- Forks: 17
- Open Issues: 3
-
Metadata Files:
- Readme: README-EN.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
> A JS library for converting HTML to Markdown.[中文](./README.md)
---
[](https://travis-ci.org/stonehank/html-to-md)
[](https://www.npmjs.com/package/html-to-md)
[](https://codecov.io/gh/stonehank/html-to-md)


### Feature
- speed, none of dependencies, `gizp` 10kb
- support `nodeJs`
- 200+`unit test` and `module test`, code coverage `97%`
> Only valid HTML will be output correctly, eg. `
abc<`, `abc>` are **Not Valid** text.
### Live Demo
[live-demo](https://stonehank.github.io/html-to-md/)
### Useage
##### install
`npm i html-to-md`
##### example
```js
const html2md = require('html-to-md')
// or if you're using ES6
import html2md from 'html-to-md'
console.log(html2md('strong and italic', options))
// ***strong and italic***
```
### Config(Optional):
#### options:
name
date type
default value
description
skipTags
Array
[
'div',
'html',
'body',
'nav',
'section',
'footer',
'main',
'aside',
'article',
'header'
]
Declare which tags need to skip
emptyTags
Array
[]
Skip all the tags inside it
ignoreTags
Array
[
'',
'style',
'head',
'!doctype',
'form',
'svg',
'noscript',
'script',
'meta'
]
Ignore all tag and content inside the tag
aliasTags
Object
{
figure :'p',
figcaption:'p',
dl:'p',
dd:'p',
dt:'p'
}
Define an alias tag name
renderCustomTags
Boolean
|'SKIP'
|'EMPTY'
|'IGNORE'
true
Define how to render not valida HTML tags
-
true: render all custom tags -
false | SKIP: render asskipTags -
EMPTY: render asemptyTags -
IGNORE: render asignoreTags
tagListener
Function
(tagName: String, props: TagListenerProps): TagListenerReturnProps => props
Custom the tag props
> Priority:skipTags > emptyTags > ignoreTags > aliasTags
Example:
```javascript
html2md('<>abc>', { ignoreTags: [''] })
// ''
html2md('<>abc>', { skipTags: [''] })
// ***abc***
html2md('<>abc>', { emptyTags: [''] })
// abc
html2md('<>abc>', {
skipTags: [''],
aliasTags: { b: 'ul', i: 'li' },
})
// * abc
html2md('abc', { renderCustomTags: 'SKIP' })
// ***abc***
```
#### force(Boolean)(Default value is false)
| value | description |
| :---: | :---------------------------------------------------------------: |
| true | Overwrite by your custom options |
| false | Use `Object.assign` to combine custom options and default options |
Example:
```javascript
// The default skipTags value is ['div','html','body']
// ex1:
html2md('
// skipTags value become ['div','html','body','b']
// ex2:
html2md('
// skipTags value become ['b']
```
#### TagListenerProps
| 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 |
#### TagListenerReturnProps
| 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 |
### Support Tags
- `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`