Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738interface FoundMention { value: string index: number}
export function getMentionAt( text: string, cursorPos: number,): FoundMention | undefined { let re = /(^|\s)@([a-z0-9.-]*)/gi let match while ((match = re.exec(text))) { const spaceOffset = match[1].length const index = match.index + spaceOffset if ( cursorPos >= index && cursorPos <= index + match[0].length - spaceOffset ) { return {value: match[2], index} } } return undefined}
export function insertMentionAt( text: string, cursorPos: number, mention: string,) { const target = getMentionAt(text, cursorPos) if (target) { return `${text.slice(0, target.index)}@${mention} ${text.slice( target.index + target.value.length + 1, // add 1 to include the "@" )}` } return text}