Skip to content

fedify-dev/markdown-it-mention

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@fedify/markdown-it-mention

JSR npm GitHub Actions

This is a markdown-it plugin that parses and renders Mastodon-style @mentions, e.g., @[email protected]. It converts them to a link, e.g.:

<a href="acct:@[email protected]"><span class="at">@</span><span
  class="user">username</span></a>

The value of href attributes, other attributes (if any), and the content of the link can be customized by passing options to the plugin:

import MarkdownIt from "markdown-it";
import { mention, toFullHandle } from "@fedify/markdown-it-mention";

const md = new MarkdownIt();
md.use(mention, {
  link: (handle: string, env: any) => `https://example.com/${handle}`,
  linkAttributes: (handle: string, env: any) => ({ class: "mention" }),
  label: toFullHandle,
});

Tip

The link callback can return null to disable the link.

If you want to collect all the handles mentioned in the document, you can pass the environment object to the render() method:

const env = {};
md.render(
  "# Hello, @[email protected]\n\n> @[email protected]",
  env,
);
console.log(env.mentions);  // ["@[email protected]", "@[email protected]"]

Installation

Deno

deno add jsr:@fedify/markdown-it-mention

Node.js

npm add @fedify/markdown-it-mention

Bun

bun add @fedify/markdown-it-mention

About

A markdown-it plugin that parses and renders Mastodon-style @mentions

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project