mirror of
https://github.com/antebudimir/feishin.git
synced 2026-01-01 02:13:33 +00:00
enable comments, safer note
This commit is contained in:
parent
960427fce8
commit
5516daab6e
8 changed files with 62 additions and 14 deletions
38
src/renderer/utils/linkify.tsx
Normal file
38
src/renderer/utils/linkify.tsx
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
// Inspired by https://github.com/navidrome/navidrome/blob/c530ccf13854e3a840ddf63eef5e2323fbe2827d/ui/src/common/AnchorMe.js
|
||||
const URL_REGEX =
|
||||
/((?:https?:\/\/)?(?:[\w-]{1,32}(?:\.[\w-]{1,32})+)(?:\/[\w\-./?%&=][^.|^\s]*)?)/g;
|
||||
|
||||
export const replaceURLWithHTMLLinks = (text: string) => {
|
||||
const urlRegex = new RegExp(URL_REGEX, 'g');
|
||||
const matches = text.matchAll(urlRegex);
|
||||
const elements = [];
|
||||
let lastIndex = 0;
|
||||
|
||||
for (const match of matches) {
|
||||
const position = match.index!;
|
||||
|
||||
if (position > lastIndex) {
|
||||
elements.push(text.substring(lastIndex, position));
|
||||
}
|
||||
|
||||
const link = match[0];
|
||||
elements.push(
|
||||
<a
|
||||
key={lastIndex}
|
||||
href={link}
|
||||
rel="noopener noreferrer"
|
||||
target="_blank"
|
||||
>
|
||||
{link}
|
||||
</a>,
|
||||
);
|
||||
|
||||
lastIndex = position + link.length;
|
||||
}
|
||||
|
||||
if (text.length > lastIndex) {
|
||||
elements.push(text.substring(lastIndex));
|
||||
}
|
||||
|
||||
return elements;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue