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

@knewbeing/markdown-it-element-transform

v1.1.1

Published

[![npm version](https://img.shields.io/npm/v/@knewbeing/markdown-it-element-transform.svg)](https://www.npmjs.com/package/@knewbeing/markdown-it-element-transform) [![license](https://img.shields.io/npm/l/@knewbeing/markdown-it-element-transform.svg)](./

Downloads

818

Readme

@knewbeing/markdown-it-element-transform

npm version license

在 markdown-it Token 级别对 AST 进行自定义转换的插件。

通过注入 core ruler 钩子,遍历所有 Token(包括子 Token),执行自定义 transform 函数,实现对 Markdown 渲染结果的精细化控制。


版权声明 / Copyright

本包代码派生自 @nolebase/markdown-it-element-transform(MIT 许可证)。
原始版权归 nolebase 所有。
knewbeing(知在,[email protected])改进并重新发布,修改部分仍遵循 MIT 许可证。

This package is derived from @nolebase/markdown-it-element-transform (MIT License).
Original copyright belongs to nolebase.
Improved and republished by knewbeing ([email protected]). Modifications remain under MIT License.


功能特性 / Features

  • 🔧 Token 级别转换 — 在 markdown-it core ruler 阶段遍历所有 Token(含子 Token)
  • 🧩 灵活的 transform 函数 — 接收 (token, state, env) 三个参数,可修改 token 的任意属性
  • 🪄 递归子 Token — 自动遍历 token.children,无需手动递归
  • 📦 零额外依赖 — 仅依赖 markdown-it peerDependency

安装 / Installation

npm install @knewbeing/markdown-it-element-transform
# or
pnpm add @knewbeing/markdown-it-element-transform

快速开始 / Quick Start

import MarkdownIt from 'markdown-it'
import { ElementTransform } from '@knewbeing/markdown-it-element-transform'

const md = new MarkdownIt()

md.use(ElementTransform, {
  transform(token, state, env) {
    // 为所有外链添加 target="_blank"
    if (token.type === 'link_open') {
      const href = token.attrGet('href') ?? ''
      if (href.startsWith('http')) {
        token.attrSet('target', '_blank')
        token.attrSet('rel', 'noopener noreferrer')
      }
    }
  },
})

在 VitePress 中使用

// .vitepress/config.ts
import { defineConfig } from 'vitepress'
import { ElementTransform } from '@knewbeing/markdown-it-element-transform'

export default defineConfig({
  markdown: {
    config: (md) => {
      md.use(ElementTransform, {
        transform(token) {
          // 为图片添加懒加载属性
          if (token.type === 'image') {
            token.attrSet('loading', 'lazy')
          }
        },
      })
    },
  },
})

API

ElementTransform

类型:PluginWithOptions<Options>

| 参数 | 类型 | 必填 | 说明 | |------|------|------|------| | options.transform | (token, state, env) => void | ✅ | Token 转换函数 |

transform 函数参数:

| 参数 | 类型 | 说明 | |------|------|------| | token | Token | 当前 markdown-it Token | | state | StateCore | markdown-it 解析状态 | | env | any | 环境对象(VitePress 中含 pathrelativePath 等) |


许可证 / License

MIT

相关链接 / Links