npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

html-to-md

v0.8.5

Published

A JS library for convert HTML<String> to markdown<String>, gzip 10kb

Downloads

17,220

Readme

一个用于转换HTMLMarkdown的工具。English


Build Status npm codecov npm bundle size

特点

  • 快速,小巧,无任何依赖,gzip 10kb

  • 支持nodeJS,参数(html 文本)为字符串

  • 200+单元测试和模块测试,覆盖率97%

注意:只有有效规范的 HTML 文本才能准确显示结果,如<p>abc<<i>abc</>等都是无效文本

效果

live-demo

为什么做这个工具

最初的动机是希望将leetcode-cn上的题目和自己的解答搬到github, 但是获取的介绍都是html格式文本,因此有了将html转换为markdown的需求。

找了几个工具,结果并不是很合胃口,有的不支持nodejs,有的并不能很好的转换,最终决定自己写一个来用。

刚开始只是写了一个比较简单的,但已经能够处理我的需求。

但后来偶尔一次使用,面对更复杂的html格式,就会出现混乱,这个库也就是一个重构版, 当然,它可能还存在很多bug没有发现,但希望能在后续不断完善,如果有发现bug,请提issuePR,我会第一时间进行处理。

使用说明

安装

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, force)
)
// ***strong and italic***

参数(可选):

options:

优先权:skipTags > emptyTags > ignoreTags > aliasTags

例:

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***

force(Boolean)(默认 false)

| 值 | 说明 | | :---: | :---------------------------------: | | true | 表示强制使用自定义配置 | | false | 对自定义配置使用Object.assign操作 |

例:

// 默认 skipTags 为 ['div','html','body']

// 配置一:
html2md('<div><b><i>abc</i></b></div>', { skipTags: ['b'] }, false)
// skipTags 为 ['div','html','body','b']

// 配置二:
html2md('<div><b><i>abc</i></b></div>', { skipTags: ['b'] }, true)
// 经过配置后 skipTags 为 ['b']

TagListenerProps

| key | 说明 | | ------------- | ----------------------------------------------------------------- | | parentTag | 父标签名,没有则为 null | | prevTagName | 上一个标签名,没有则为 null | | nextTagName | 下一个标签名,没有则为 null | | isFirstSubTag | 是否当前父标签内部的第一个子标签 | | attrs | 当前标签的 attributes,以 object 集合方式,例如 { src, href ... } | | innerHTML | 内部 HTML 字符串 | | match | 当前的 HTML 对应 Markdown 的匹配符号 | | language? | 当前标签语言,只在 pre 标签中出现 | | isSelfClosing | 是否自闭和标签 |

TagListenerReturnProps

| key | 说明 | | --------- | ----------------------------------------------------------------- | | attrs | 当前标签的 attributes,以 object 集合方式,例如 { src, href ... } | | match | 返回一个新的自定义匹配符号 | | language? | 返回自定义 pre 标签的 language |

支持标签

  • 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