28 lines
519 B
TypeScript
Raw Normal View History

2017-03-18 01:16:32 +09:00
/**
* Link
*/
2018-06-17 19:55:39 +09:00
export type TextElementLink = {
2018-06-18 14:28:43 +09:00
type: 'link'
2018-06-17 19:55:39 +09:00
content: string
title: string
url: string
silent: boolean
};
export default function(text: string) {
2017-03-18 04:20:25 +09:00
const match = text.match(/^\??\[([^\[\]]+?)\]\((https?:\/\/[\w\/:%#@\$&\?!\(\)\[\]~\.=\+\-]+?)\)/);
2017-03-18 01:16:32 +09:00
if (!match) return null;
const silent = text.startsWith('?');
2017-03-18 01:16:32 +09:00
const link = match[0];
const title = match[1];
const url = match[2];
return {
type: 'link',
content: link,
title: title,
url: url,
silent: silent
2018-06-17 19:55:39 +09:00
} as TextElementLink;
}