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 🙏

© 2026 – Pkg Stats / Ryan Hefner

markdown-to-mfuns-json

v1.0.1

Published

将 Markdown 转换为喵御宅(Mfuns)定制的 Quill Delta JSON 格式

Readme

markdown-to-mfuns-json

将 Markdown 格式的内容转换为喵御宅(Mfuns)定制的 Quill Delta JSON 格式。

适用于:文章正文、视频简介、动态正文、评论正文等场景。

安装

npm install markdown-to-mfuns-json
yarn add markdown-to-mfuns-json

快速开始

ESM (import)

import { markdownToQuillDelta } from "markdown-to-mfuns-json";

const markdown = `
## 标题

这是一段**粗体**和*斜体*的文字。

- 列表项 1
- 列表项 2
`;

const deltaJson = markdownToQuillDelta(markdown);
console.log(deltaJson);
// 输出: {"ops":[{"insert":"标题"},{"insert":"\n","attributes":{"header":2}},{"insert":"这是一段"},{"insert":"粗体","attributes":{"bold":true}},{"insert":"和"},{"insert":"斜体","attributes":{"italic":true}},{"insert":"的文字。"},{"insert":"\n"},{"insert":"列表项 1"},{"insert":"\n","attributes":{"list":"bullet"}},{"insert":"列表项 2"},{"insert":"\n","attributes":{"list":"bullet"}}]}

CommonJS (require)

const { markdownToQuillDelta } = require("markdown-to-mfuns-json");

const markdown = `## Hello World`;
const deltaJson = markdownToQuillDelta(markdown);
console.log(deltaJson);

API

markdownToQuillDelta(markdown, options?)

将 Markdown 文本转换为 Quill Delta JSON 字符串。

参数:

| 参数 | 类型 | 必填 | 说明 | | ---------- | --------------- | ---- | ------------------- | | markdown | string | 是 | Markdown 格式的文本 | | options | ParserOptions | 否 | 解析器配置选项 |

返回值:

string - Quill Delta JSON 格式的字符串

示例:

import { markdownToQuillDelta } from "markdown-to-mfuns-json";

const deltaJson = markdownToQuillDelta("# Hello");

markdownToQuillDeltaObject(markdown, options?)

将 Markdown 文本转换为 Quill Delta 对象(非 JSON 字符串)。

参数:

| 参数 | 类型 | 必填 | 说明 | | ---------- | --------------- | ---- | ------------------- | | markdown | string | 是 | Markdown 格式的文本 | | options | ParserOptions | 否 | 解析器配置选项 |

返回值:

QuillDelta - Quill Delta 对象

示例:

import { markdownToQuillDeltaObject } from "markdown-to-mfuns-json";

const deltaObj = markdownToQuillDeltaObject("# Hello");
console.log(deltaObj.ops); // 访问 ops 数组

空行处理说明

默认情况下,空行会被跳过,不生成任何 Delta 操作。

这是因为 Quill Delta 中的空行({ insert: "\n" })在 Mfuns 平台的某些场景(如文章正文、评论等)可能会导致显示异常或多余的换行。因此默认行为是忽略所有空行。

如果需要保留空行,可以设置 preserveEmptyLines: true

const markdown = `第一行

第二行`;

// 默认:跳过空行
const result1 = markdownToQuillDelta(markdown);
// 输出: [{ insert: '第一行' }, { insert: '\n' }, { insert: '第二行' }, { insert: '\n' }]

// 保留空行
const result2 = markdownToQuillDelta(markdown, { preserveEmptyLines: true });
// 输出: [{ insert: '第一行' }, { insert: '\n' }, { insert: '\n' }, { insert: '第二行' }, { insert: '\n' }]

ParserOptions

解析器配置选项接口。

interface ParserOptions {
  /**
   * 是否跳过一级标题(#)
   * @default true
   */
  skipH1?: boolean;

  /**
   * 是否跳过四级及以上标题(####, #####, ######)
   * @default true
   */
  skipHighLevelHeaders?: boolean;

  /**
   * 是否保留空行
   * **注意**:默认 false(跳过空行),这是为了避免 Mfuns 平台显示异常
   * @default false
   */
  preserveEmptyLines?: boolean;
}

示例:

import { markdownToQuillDelta } from "markdown-to-mfuns-json";

const markdown = `
# 一级标题(会被跳过)
## 二级标题

正文内容
`;

// 默认配置(跳过一级标题)
const defaultResult = markdownToQuillDelta(markdown);

// 保留一级标题
const withH1 = markdownToQuillDelta(markdown, { skipH1: false });

// 保留空行
const withEmptyLines = markdownToQuillDelta(markdown, {
  preserveEmptyLines: true,
});

类型定义

// Delta 操作项
interface DeltaOp {
  insert: string | { [key: string]: unknown };
  attributes?: { [key: string]: unknown };
}

// Quill Delta 对象
interface QuillDelta {
  ops: DeltaOp[];
}

支持的 Markdown 语法

块级元素

| Markdown | 说明 | 输出属性 | | ------------------ | -------- | ---------------------- | | ## 标题 | 二级标题 | {"header": 2} | | ### 标题 | 三级标题 | {"header": 3} | | - 项目 | 无序列表 | {"list": "bullet"} | | 1. 项目 | 有序列表 | {"list": "ordered"} | | \``code```| 代码块 |{"code-block": true}| |---*** | 分割线 |{"divider": true}` |

行内格式

| Markdown | 说明 | 输出属性 | | --------------- | -------- | --------------------- | | **粗体** | 粗体 | {"bold": true} | | *斜体* | 斜体 | {"italic": true} | | ~~删除线~~ | 删除线 | {"strike": true} | | <u>下划线</u> | 下划线 | {"underline": true} | | [链接](url) | 链接 | {"link": "url"} | | ![图片](url) | 图片 | {"image": "url"} | | `代码` | 行内代码 | {"code": true} |

不支持的语法

以下 Markdown 语法会被跳过或作为普通文本处理:

  • 一级标题 # (默认跳过,可通过 skipH1: false 开启)
  • 四级及以上标题 #### (默认跳过)
  • 引用块 >
  • 表格
  • 任务列表 - [ ]

完整示例

import {
  markdownToQuillDelta,
  markdownToQuillDeltaObject,
} from "markdown-to-mfuns-json";

const markdown = `
## 文章标题

这是一篇**示例文章**,包含多种格式:

### 列表

- 无序列表项 1
- 无序列表项 2

1. 有序列表项 1
2. 有序列表项 2

### 格式

- **粗体文字**
- *斜体文字*
- ~~删除线~~
- <u>下划线</u>
- [链接](https://example.com)
- 行内代码 \`console.log('hello')\`

### 代码块

\`\`\`javascript
function hello() {
  console.log('Hello World');
}
\`\`\`

---

![示例图片](https://example.com/image.png)
`;

// 获取 JSON 字符串
const deltaJson = markdownToQuillDelta(markdown);
console.log("JSON:", deltaJson);

// 获取对象
const deltaObj = markdownToQuillDeltaObject(markdown);
console.log("对象:", deltaObj);
console.log("操作数:", deltaObj.ops.length);

应用场景

本库专为喵御宅(Mfuns)平台设计,可用于:

  1. 文章正文 - 将用户输入的 Markdown 转换为平台文章格式
  2. 视频简介 - 转换视频描述内容
  3. 动态正文 - 转换用户动态/帖子内容
  4. 评论正文 - 转换评论内容

Node.js 版本要求

  • Node.js >= 16.0.0

License

MIT