feat: link implementation (#100)

* link implementation

* add docs for links

* use btn instead of badge
This commit is contained in:
Hayden 2022-10-19 21:31:08 -08:00 committed by GitHub
parent 97a34475c8
commit 2d34557f69
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 120 additions and 6 deletions

View file

@ -33,3 +33,35 @@ export function fmtCurrency(value: number | string, currency = "USD", locale = "
});
return formatter.format(value);
}
export type MaybeUrlResult = {
isUrl: boolean;
url: string;
text: string;
};
export function maybeUrl(str: string): MaybeUrlResult {
const result: MaybeUrlResult = {
isUrl: str.startsWith("http://") || str.startsWith("https://"),
url: "",
text: "",
};
if (!result.isUrl && !str.startsWith("[")) {
return result;
}
if (str.startsWith("[")) {
const match = str.match(/\[(.*)\]\((.*)\)/);
if (match && match.length === 3) {
result.isUrl = true;
result.text = match[1];
result.url = match[2];
}
} else {
result.url = str;
result.text = "Link";
}
return result;
}