Files
vue-highlights/src/utils/extractMentions.js
Pedro G. Galaviz 4a99a58085 first commit
2019-11-28 20:55:32 -06:00

26 lines
655 B
JavaScript

// Extracts mentions from text.
import { atSigns, endMentionMatch, validMention } from './regex'
export default function (text) {
if (!text || !text.match(atSigns)) {
return []
}
const mentions = []
text.replace(validMention, function (match, before, atSign, mentionText, offset, chunk) {
const after = chunk.slice(offset + match.length)
if (!after.match(endMentionMatch)) {
const startPosition = offset + before.length
const endPosition = startPosition + mentionText.length + 1
mentions.push({
username: mentionText,
indices: [startPosition, endPosition]
})
}
})
return mentions
}