mirror of
https://github.com/block/buzz.git
synced 2026-08-18 06:50:31 +02:00
Signed-off-by: npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@sprout-oss.stage.blox.sqprod.co>
37 lines
1.1 KiB
TypeScript
37 lines
1.1 KiB
TypeScript
/**
|
|
* Remark plugin that detects #channel-name patterns in text nodes and wraps them
|
|
* in custom HAST `channel-link` elements for styled rendering via react-markdown.
|
|
*
|
|
* Known channel names are matched longest-first to avoid partial matches. When
|
|
* no known names are provided, falls back to `#\S+` so that channel links still
|
|
* render while the channel list is loading asynchronously.
|
|
*/
|
|
|
|
import { createRemarkPrefixPlugin } from "./createRemarkPrefixPlugin";
|
|
import { buildPrefixPattern } from "./mentionPattern";
|
|
|
|
type RemarkChannelLinksOptions = {
|
|
channelNames?: string[];
|
|
};
|
|
|
|
export default function remarkChannelLinks(
|
|
options?: RemarkChannelLinksOptions,
|
|
) {
|
|
const channelPattern = buildPrefixPattern("#", options?.channelNames ?? [], {
|
|
fallbackToGeneric: true,
|
|
});
|
|
|
|
return createRemarkPrefixPlugin(channelPattern, (matchText) => {
|
|
const channelName = matchText.slice(1);
|
|
return {
|
|
type: "channel-link",
|
|
value: matchText,
|
|
data: {
|
|
hName: "channel-link",
|
|
hChildren: [{ type: "text", value: matchText }],
|
|
channelName,
|
|
},
|
|
};
|
|
});
|
|
}
|