https://github.com/fedify-dev/markdown-it-hashtag
A markdown-it plugin that parses and renders Mastodon-style #hashtags
https://github.com/fedify-dev/markdown-it-hashtag
fediverse hashtag markdown markdown-it markdown-it-plugin
Last synced: 6 months ago
JSON representation
A markdown-it plugin that parses and renders Mastodon-style #hashtags
- Host: GitHub
- URL: https://github.com/fedify-dev/markdown-it-hashtag
- Owner: fedify-dev
- License: mit
- Created: 2024-05-24T09:04:18.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-30T01:10:32.000Z (12 months ago)
- Last Synced: 2025-06-17T12:15:59.347Z (7 months ago)
- Topics: fediverse, hashtag, markdown, markdown-it, markdown-it-plugin
- Language: TypeScript
- Homepage: https://jsr.io/@fedify/markdown-it-hashtag
- Size: 21.5 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
- Funding: .github/FUNDING.yaml
- License: LICENSE
Awesome Lists containing this project
README
@fedify/markdown-it-hashtag
===========================
[![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 #hashtags.
It converts, for example, `#FooBar` into a link:
~~~~ html
#FooBar
~~~~
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 { hashtag, spanHashAndTag } from "@fedify/markdown-it-hashtag";
const md = new MarkdownIt();
md.use(hashtag, {
link: (tag: string) => `https://example.com/tags/${tag.substring(1)}`,
linkAttributes: (handle: string) => ({ class: "hashtag" }),
label: spanHashAndTag,
});
~~~~
If you want to collect all hashtags in a document, you can pass an environment
object to the `render()` method:
~~~~ typescript
const env = {};
md.render(
"Hello, #FooBar\n\n> #BazQux",
env,
);
console.log(env.hashtags); // ["#FooBar", "#BazQux"]
~~~~
[JSR]: https://jsr.io/@fedify/markdown-it-hashtag
[JSR badge]: https://jsr.io/badges/@fedify/markdown-it-hashtag
[npm]: https://www.npmjs.com/package/@fedify/markdown-it-hashtag
[npm badge]: https://img.shields.io/npm/v/%40fedify%2Fmarkdown-it-hashtag?logo=npm
[GitHub Actions]: https://github.com/fedify-dev/markdown-it-hashtag/actions/workflows/main.yaml
[GitHub Actions badge]: https://github.com/fedify-dev/markdown-it-hashtag/actions/workflows/main.yaml/badge.svg
[markdown-it]: https://github.com/markdown-it/markdown-it
[options]: https://jsr.io/@fedify/markdown-it-hashtag/doc/~/PluginOptions
Installation
------------
### Deno
~~~~ sh
deno add @fedify/markdown-it-hashtag
~~~~
### Node.js
~~~~ sh
npm add @fedify/markdown-it-hashtag
~~~~
### Bun
~~~~ sh
bun add @fedify/markdown-it-hashtag
~~~~