misskey/src/mfm/parse/elements/url.ts
2018-11-16 21:30:01 +09:00

22 lines
471 B
TypeScript

/**
* URL
*/
export type TextElementUrl = {
type: 'url';
content: string;
url: string;
};
export default function(text: 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(','));
return {
type: 'url',
content: url,
url: url
} as TextElementUrl;
}