Utilities

Standalone functions for parsing and extracting mention data. Work server-side.

Import

// From React package (re-exported):
import { extractMentions, markupToPlainText, parseMarkup } from "@skyastrall/mentions-react";

// Or from core (no React dependency, works server-side):
import { extractMentions, markupToPlainText, parseMarkup } from "@skyastrall/mentions-core";

extractMentions

Extract all mentions from a markup string.

const mentions = extractMentions("@[Alice](1) said #[urgent](t1)", triggers);
// [{ id: "1", label: "Alice" }, { id: "t1", label: "urgent" }]

markupToPlainText

Convert markup to plain text.

const plain = markupToPlainText("@[Alice](1) hello #[bug](t2)", triggers);
// "@Alice hello #bug"

parseMarkup

Parse into typed segments.

const segments = parseMarkup("hello @[Alice](1) world", triggers);
// [
//   { type: "text", text: "hello ", markupStart: 0, markupEnd: 6 },
//   { type: "mention", text: "@Alice", id: "1", trigger: "@", ... },
//   { type: "text", text: " world", markupStart: 22, markupEnd: 28 },
// ]

Server-Side Usage

Import from @skyastrall/mentions-core for server-side usage. Zero dependencies, no DOM.

// Next.js API route or server action:
import { extractMentions } from "@skyastrall/mentions-core";

export async function processMessage(markup: string) {
  const triggers = [{ char: "@", data: [] }];
  const mentions = extractMentions(markup, triggers);
  // Notify mentioned users...
}