mirror of
https://codeberg.org/yeentown/barkey.git
synced 2025-05-10 07:36:56 +00:00
23 lines
578 B
TypeScript
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;
|
|
}
|