An open API service indexing awesome lists of open source software.

https://github.com/dahlia/markdown-it-mention

A markdown-it plugin that parses and renders Mastodon-style @mentions
https://github.com/dahlia/markdown-it-mention

fediverse markdown markdown-it markdown-it-plugin mention

Last synced: 4 months ago
JSON representation

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

Awesome Lists containing this project

README

        

@fedify/markdown-it-mention
===========================

[![JSR][JSR badge]][JSR]
[![npm][npm badge]][npm]
[![GitHub Actions][GitHub Actions badge]][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.:

~~~~ html
@username
~~~~

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

~~~~ typescript
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:

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

[JSR]: https://jsr.io/@fedify/markdown-it-mention
[JSR badge]: https://jsr.io/badges/@fedify/markdown-it-mention
[npm]: https://www.npmjs.com/package/@fedify/markdown-it-mention
[npm badge]: https://img.shields.io/npm/v/%40fedify%2Fmarkdown-it-mention?logo=npm
[GitHub Actions]: https://github.com/dahlia/markdown-it-mention/actions/workflows/main.yaml
[GitHub Actions badge]: https://github.com/dahlia/markdown-it-mention/actions/workflows/main.yaml/badge.svg
[markdown-it]: https://github.com/markdown-it/markdown-it
[options]: https://jsr.io/@fedify/markdown-it-mention/doc/~/PluginOptions

Installation
------------

### Deno

~~~~ sh
deno add @fedify/markdown-it-mention
~~~~

### Node.js

~~~~ sh
npm add @fedify/markdown-it-mention
~~~~

### Bun

~~~~ sh
bun add @fedify/markdown-it-mention
~~~~