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 🙏

© 2025 – Pkg Stats / Ryan Hefner

mm_html

v1.1.7

Published

这是超级美眉用于服务端解析html模块

Downloads

28

Readme

mm_html

这是超级美眉用于服务端解析html模块,可以通过jquery在后端nodejs解析html。

功能特点

  • 基于 jsdom 和 jquery 实现服务端 HTML 解析
  • 支持 HTML 与 JSON 的双向转换
  • 提供丰富的标签配置选项
  • 支持属性解析和保留
  • 可自定义文本节点处理方式

安装

npm install mm_html

快速开始

const Dom = require('mm_html');

// 创建实例
const dom = new Dom();

// HTML 转 JSON
const html = '<div class="container"><h1>Hello World</h1></div>';
const json = dom.toJson(html);

// JSON 转 HTML
const htmlStr = dom.toHtml(json);

服务端 jQuery 操作

通过 toJQ 函数,您可以在服务端使用熟悉的 jQuery 语法来操作 DOM 元素:

const Dom = require('mm_html');
const dom = new Dom();

// HTML 字符串
const html = `
<div class="content">
    <h1>标题</h1>
    <p class="desc">这是一段描述</p>
    <ul class="list">
        <li>项目1</li>
        <li>项目2</li>
    </ul>
</div>`;

// 获取 jQuery 操作对象
var $ = dom.toJQ(html);

// 使用 jQuery 方法操作 DOM
$('.desc').text('更新后的描述');
$('.list').append('<li>新增项目</li>');

// 获取修改后的 HTML
const updatedHtml = $('body').html();
console.log(updatedHtml);

通过 toJQ 函数,您可以:

  • 在服务端使用所有标准的 jQuery 选择器和方法
  • 动态修改 DOM 结构和内容
  • 提取特定元素的信息
  • 批量处理 HTML 元素

API 文档

Dom 类

构造函数

const dom = new Dom(config);

参数

  • config (Object, 可选): 配置对象,包含以下标签配置:
    • tags_text: 文本标签列表
    • tags_col: 列标签列表
    • tags_hp: 标题和段落标签列表
    • tags_left: 自闭合标签列表
    • tags_no: 不处理子元素的标签列表
    • tags_only: 仅处理自身的标签列表
    • tags_container: 容器标签列表

toJQ(html)

将 HTML 字符串转换为 jQuery 操作对象。

参数

  • html (String): HTML 字符串

返回值

  • (Object): jQuery 操作对象

toJson(html)

将 HTML 字符串转换为 JSON 对象。

参数

  • html (String): HTML 字符串

返回值

  • (Object): JSON 对象,包含以下属性:
    • tag: 标签名
    • attr: 属性对象(可选)
    • text: 文本内容(可选)
    • sub: 子元素数组(可选)

toHtml(json, tab)

将 JSON 对象转换回 HTML 字符串。

参数

  • json (Object): JSON 对象
  • tab (String, 可选): 缩进字符,默认为 "\t"

返回值

  • (String): HTML 字符串

使用示例

基础示例

const Dom = require('mm_html');
const dom = new Dom();

// HTML 转 JSON
const html = `
<div class="breadcrum-bg py-sm-5 py-4">
    <div class="container py-lg-3">
        <h2>About Us</h2>
        <p><a href="index.html">Home</a> &nbsp; / &nbsp; About</p>
    </div>
</div>`;

const json = dom.toJson(html);
console.log(JSON.stringify(json, null, 2));

// JSON 转回 HTML
const htmlStr = dom.toHtml(json);
console.log(htmlStr);

处理文件示例

const Dom = require('mm_html');
require('mm_expand'); // 用于文件操作

async function processHtmlFile(filePath) {
    const dom = new Dom();
    
    // 读取 HTML 文件
    const text = filePath.loadText(__dirname + '/');
    
    // 转换为 JSON
    const json = dom.toJson(text);
    
    // 保存 JSON 文件
    './output.json'.saveJson(json, true);
    
    // 转换回 HTML 并保存
    const str = dom.toHtml(json);
    './output.html'.saveText(str);
}

processHtmlFile('./input.html');

注意事项

  1. 确保已安装所有依赖包:jqueryjsdommm_expand
  2. 处理大型 HTML 文件时注意内存使用
  3. 自定义配置时注意标签分类的合理性

许可证

ISC