barkey/src/mfm/parse/elements/url.ts
2018-11-16 21:57:19 +09:00

23 lines
578 B
TypeScript

/**
* URL
*/
export type TextElementUrl = {
type: 'url';
content: string;
url: string;
};
export default function(text: string, before: string) {
const match = text.match(/^https?:\/\/[\w\/:%#@\$&\?!\(\)\[\]~\.,=\+\-]+/);
if (!match) return null;
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(')'));
return {
type: 'url',
content: url,
url: url
} as TextElementUrl;
}