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

@coco-box/md-diff

v0.0.1

Published

无关框架的 Markdown AST diff 与 HTML 渲染工具

Readme

@coco-box/md-diff

@coco-box/md-diff 是一个无关框架的 Markdown AST diff 工具包。

它负责两件事:

  1. 把两份 Markdown 转成 diff AST。
  2. 把 diff AST 渲染成带有 diff class 的 HTML。

包本身不依赖 Vue、React 等框架。你可以在任意前端项目里直接使用它,再按需引入 @coco-box/md-diff/styles.css 来加载 diff 样式。

安装

pnpm add @coco-box/md-diff

基础用法

先引入样式,再调用 renderMarkdownDiff:

import '@coco-box/md-diff/styles.css';
import { renderMarkdownDiff } from '@coco-box/md-diff';

const { html, diffAst } = renderMarkdownDiff({
  oldMarkdown,
  newMarkdown,
});

container.innerHTML = html;

如果你想自己接管 AST 到 HTML 的过程,也可以拆开使用:

import '@coco-box/md-diff/styles.css';
import { createMarkdownDiffAst, renderDiffAstToHtml } from '@coco-box/md-diff';

const diffAst = createMarkdownDiffAst(oldMarkdown, newMarkdown);
const html = renderDiffAstToHtml(diffAst);

Vue 3 用法

main.ts

import { createApp } from 'vue';
import App from './App.vue';
import '@coco-box/md-diff/styles.css';
import './style.css';

createApp(App).mount('#app');

App.vue

<script setup lang="ts">
import { computed, ref } from 'vue';
import { renderMarkdownDiff } from '@coco-box/md-diff';

const oldMarkdown = ref('# 旧版本\n\n第一段内容。');
const newMarkdown = ref('# 新版本\n\n第一段内容');

const diffResult = computed(() => renderMarkdownDiff({
  oldMarkdown: oldMarkdown.value,
  newMarkdown: newMarkdown.value,
}));
</script>

<template>
  <div class="markdown-diff-host" v-html="diffResult.html"></div>
</template>

React 用法

import { useMemo } from 'react';
import '@coco-box/md-diff/styles.css';
import { renderMarkdownDiff } from '@coco-box/md-diff';

type Props = {
  oldMarkdown: string;
  newMarkdown: string;
};

export function MarkdownDiffPreview({ oldMarkdown, newMarkdown }: Props) {
  const result = useMemo(() => {
    return renderMarkdownDiff({
      oldMarkdown,
      newMarkdown,
    });
  }, [oldMarkdown, newMarkdown]);

  return (
    <div
      className="markdown-diff-host"
      dangerouslySetInnerHTML={{ __html: result.html }}
    />
  );
}

暗黑模式与样式覆盖

默认样式包含两层能力:

  1. 自动跟随 prefers-color-scheme: dark。
  2. 支持 .dark、[data-theme="dark"]、[data-color-mode="dark"] 这类常见显式暗黑模式容器。

如果你想自定义主题,推荐在自己的容器上覆盖 CSS 变量:

.markdown-diff-host {
  --md-diff-color: #1e293b;
  --md-diff-link-color: #0f766e;
  --md-diff-insert-bg: #dcfce7;
  --md-diff-delete-bg: #fee2e2;
}

[data-theme="dark"] .markdown-diff-host {
  --md-diff-color: #e2e8f0;
  --md-diff-pre-bg: #0f172a;
  --md-diff-insert-border: #4ade80;
  --md-diff-delete-border: #fb7185;
}

如果你想直接覆盖具体节点样式,也可以在本地样式文件中放在包样式之后声明:

.markdown-diff-host .md-diff-inline-ins {
  border-bottom: 1px solid currentColor;
}

.markdown-diff-host .md-diff-del {
  border-left-width: 6px;
}

导出内容

  • createMarkdownDiffAst
  • renderMarkdownDiff
  • renderDiffAstToHtml
  • markdownToAst
  • MARKDOWN_DIFF_BODY_CLASS_NAME
  • @coco-box/md-diff/styles.css