Sharkey/src/mfm/parse/elements/url.ts

21 lines
335 B
TypeScript
Raw Normal View History

2016-12-29 07:49:51 +09:00
/**
* URL
*/
2018-06-17 19:55:39 +09:00
export type TextElementUrl = {
2018-06-18 14:28:43 +09:00
type: 'url'
2018-06-17 19:55:39 +09:00
content: string
url: string
};
export default function(text: string) {
2017-03-18 01:16:32 +09:00
const match = text.match(/^https?:\/\/[\w\/:%#@\$&\?!\(\)\[\]~\.=\+\-]+/);
2017-02-11 02:32:00 +09:00
if (!match) return null;
2017-03-18 01:16:32 +09:00
const url = match[0];
2017-02-11 02:32:00 +09:00
return {
2017-03-18 01:16:32 +09:00
type: 'url',
content: url,
url: url
2018-06-17 19:55:39 +09:00
} as TextElementUrl;
}