monkeeShark/src/mfm/parse/elements/mention.ts

25 lines
493 B
TypeScript
Raw Normal View History

2016-12-28 22:49:51 +00:00
/**
* Mention
*/
2018-07-07 10:19:00 +00:00
import parseAcct from '../../../misc/acct/parse';
2016-12-28 22:49:51 +00:00
2018-06-17 10:55:39 +00:00
export type TextElementMention = {
2018-06-18 05:28:43 +00:00
type: 'mention'
2018-06-17 10:55:39 +00:00
content: string
username: string
host: string
};
export default function(text: string) {
const match = text.match(/^@[a-z0-9_]+(?:@[a-z0-9\.\-]+[a-z0-9])?/i);
2017-02-10 17:32:00 +00:00
if (!match) return null;
const mention = match[0];
2018-03-27 07:51:12 +00:00
const { username, host } = parseAcct(mention.substr(1));
2017-02-10 17:32:00 +00:00
return {
type: 'mention',
content: mention,
2018-03-27 07:51:12 +00:00
username,
host
2018-06-17 10:55:39 +00:00
} as TextElementMention;
}