mirror of
https://github.com/anatolykopyl/vue-highlights.git
synced 2026-03-26 12:55:35 +00:00
first commit
This commit is contained in:
25
src/utils/extractMentions.js
Normal file
25
src/utils/extractMentions.js
Normal file
@@ -0,0 +1,25 @@
|
||||
// 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
|
||||
}
|
||||
Reference in New Issue
Block a user