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

rehype-mention

v1.1.0

Published

A rehype plugin to transform mention text patterns into HTML elements

Readme

rehype-mention

A rehype plugin to transform mention text patterns into HTML elements.

What is this?

This package is a plugin that transforms mention-style text patterns into structured HTML elements with semantic data attributes.

When should I use this?

Use this plugin when you need to convert text-based mentions (like @[displayText](data)) into proper HTML elements for rich text editing or display purposes.

Install

This package is ESM only. In Node.js (version 16+), install with npm:

npm install rehype-mention

Use

Basic HTML Processing

Say our module contains:

import rehypeParse from 'rehype-parse'
import rehypeStringify from 'rehype-stringify'
import {unified} from 'unified'
import rehypeMention from 'rehype-mention'

const file = await unified()
  .use(rehypeParse, {fragment: true})
  .use(rehypeMention)
  .use(rehypeStringify)
  .process('<p>这是一个@[张三](xiaoming)测试。</p>')

console.log(String(file))

…then running the code will output:

<p>这是一个<span data-type="mention" data-id="xiaoming">@张三</span>测试。</p>

Markdown Integration

⚠️ Important: In Markdown environments, @[text](id) patterns will be converted to links by Markdown parsers. This plugin handles both scenarios:

import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import rehypeStringify from 'rehype-stringify'
import {unified} from 'unified'
import rehypeMention from 'rehype-mention'

// Process Markdown with mentions
const result = await unified()
  .use(remarkParse)          // Parse Markdown
  .use(remarkRehype)         // Convert to HTML AST
  .use(rehypeMention)        // Handle mentions (works after link conversion)
  .use(rehypeStringify)      // Output HTML
  .process('User @[张三](user-123) sent a message.')

console.log(String(result))
// Output: <p>User <span data-type="mention" data-id="user-123">@张三</span> sent a message.</p>

API

This package exports no identifiers. The default export is rehypeMention.

unified().use(rehypeMention)

Transform mention text patterns into HTML elements.

The plugin searches for text patterns in two formats:

  1. Text format: @[displayText](uuid)
  2. Link format: @<a href="uuid">displayText</a> (from Markdown conversion)

Both are transformed into:

<span data-type="mention" data-id="uuid">@displayText</span>

Processing Order

This plugin is designed to work after Markdown processing. The recommended order is:

  1. remarkParse (if using Markdown)
  2. remarkRehype (if using Markdown)
  3. rehypeMention ← Insert here
  4. rehypeStringify

Parameters

This plugin takes no parameters.

Returns

Transform function (Transformer).

Pattern Format

The plugin recognizes mention patterns with the following structure:

  • Starts with @
  • Followed by [displayText]
  • Followed by (uuid)

Where:

  • displayText: The text to display (will be prefixed with @)
  • uuid: The unique identifier that becomes the data-id attribute

Examples

Basic mention (Text format)

<!-- Input -->
<p>Contact @[admin](admin-123) for help.</p>

<!-- Output -->
<p>Contact <span data-type="mention" data-id="admin-123">@admin</span> for help.</p>

Multiple mentions

<!-- Input -->
<p>Meeting with @[张三](user-1) and @[李四](user-2)</p>

<!-- Output -->
<p>Meeting with <span data-type="mention" data-id="user-1">@张三</span> and <span data-type="mention" data-id="user-2">@李四</span></p>

Markdown-converted mentions (Link format)

<!-- Input (after Markdown processing) -->
<p>User @<a href="user-123">张三</a> sent a message.</p>

<!-- Output -->
<p>User <span data-type="mention" data-id="user-123">@张三</span> sent a message.</p>

Mixed scenario

<!-- Input -->
<p>@[源地址](f14e1cf3)和@<a href="user-123">张三</a>都在线。</p>

<!-- Output -->
<p><span data-type="mention" data-id="f14e1cf3">@源地址</span>和<span data-type="mention" data-id="user-123">@张三</span>都在线。</p>

Compatibility

This package works with Node.js 16+. This package works with rehype version 13+.

Security

This plugin does not sanitize input. Always use proper sanitization when dealing with user-generated content.

License

MIT