2023-07-25 01:03:56 +00:00
|
|
|
import { findAndReplace } from 'mdast-util-find-and-replace'
|
2021-07-15 19:56:23 +00:00
|
|
|
|
|
|
|
const userGroup = '[\\w_]+'
|
|
|
|
|
|
|
|
const mentionRegex = new RegExp(
|
|
|
|
'@(' + userGroup + '(?:\\/' + userGroup + ')?)',
|
|
|
|
'gi'
|
|
|
|
)
|
|
|
|
|
|
|
|
export default function mention (options) {
|
|
|
|
return function transformer (tree) {
|
|
|
|
findAndReplace(
|
|
|
|
tree,
|
|
|
|
[
|
|
|
|
[mentionRegex, replaceMention]
|
|
|
|
],
|
|
|
|
{ ignore: ['link', 'linkReference'] }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function replaceMention (value, username, match) {
|
|
|
|
if (
|
|
|
|
/[\w`]/.test(match.input.charAt(match.index - 1)) ||
|
|
|
|
/[/\w`]/.test(match.input.charAt(match.index + value.length))
|
|
|
|
) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-07-25 01:03:56 +00:00
|
|
|
const node = { type: 'text', value }
|
2021-07-15 19:56:23 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
type: 'link',
|
|
|
|
title: null,
|
|
|
|
url: '/' + username,
|
|
|
|
children: [node]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|