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

24 lines
578 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-11-03 22:38:12 +09:00
type: 'url';
content: string;
url: string;
2018-06-17 19:55:39 +09:00
};
export default function(text: string, before: string) {
2018-11-16 21:30:01 +09:00
const match = text.match(/^https?:\/\/[\w\/:%#@\$&\?!\(\)\[\]~\.,=\+\-]+/);
2017-02-11 02:32:00 +09:00
if (!match) return null;
2018-11-16 21:30:01 +09:00
let url = match[0];
if (url.endsWith('.')) url = url.substr(0, url.lastIndexOf('.'));
if (url.endsWith(',')) url = url.substr(0, url.lastIndexOf(','));
if (url.endsWith(')') && before.endsWith('(')) url = url.substr(0, url.lastIndexOf(')'));
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;
}