Added url parser to support wider range of addresses

This commit is contained in:
unknown 2021-06-11 15:33:06 +02:00
parent 5968663be4
commit a5504e6e80
7 changed files with 63 additions and 24 deletions

View File

@ -60,3 +60,16 @@ docker run -p 5005:5005 -v <host_dir>:/app/data flame
- Customize your page by choosing from 12 color themes
![Homescreen screenshot](./github/_themes.png)
## Usage
### Supported links for applications and bookmarks
#### Rules
- URL starts with `http://`
- Format: `http://www.domain.com`, `http://domain.com`
- Redirect: `{dest}`
- URL starts with `https://`
- Format: `https://www.domain.com`, `https://domain.com`
- Redirect: `https://{dest}`
- URL without protocol
- Format: `www.domain.com`, `domain.com`, `sub.domain.com`, `local`, `ip`, `ip:port`
- Redirect: `http://{dest}`

View File

@ -11,7 +11,6 @@ import Home from './components/Home/Home';
import Apps from './components/Apps/Apps';
import Settings from './components/Settings/Settings';
import Bookmarks from './components/Bookmarks/Bookmarks';
import NotificationCenter from './components/NotificationCenter/NotificationCenter';
if (localStorage.theme) {

View File

@ -1,8 +1,6 @@
import { Link } from 'react-router-dom';
import classes from './AppCard.module.css';
import Icon from '../../UI/Icons/Icon/Icon';
import { iconParser } from '../../../utility/iconParser';
import { iconParser, urlParser } from '../../../utility';
import { App } from '../../../interfaces';
@ -12,18 +10,16 @@ interface ComponentProps {
}
const AppCard = (props: ComponentProps): JSX.Element => {
const redirectHandler = (url: string): void => {
window.open(url);
}
const [displayUrl, redirectUrl] = urlParser(props.app.url);
return (
<a href={`http://${props.app.url}`} target='_blank' className={classes.AppCard}>
<a href={redirectUrl} target='_blank' className={classes.AppCard}>
<div className={classes.AppCardIcon}>
<Icon icon={iconParser(props.app.icon)} />
</div>
<div className={classes.AppCardDetails}>
<h5>{props.app.name}</h5>
<span>{props.app.url}</span>
<span>{displayUrl}</span>
</div>
</a>
)

View File

@ -2,7 +2,7 @@ import { Bookmark, Category } from '../../../interfaces';
import classes from './BookmarkCard.module.css';
import Icon from '../../UI/Icons/Icon/Icon';
import { iconParser } from '../../../utility/iconParser';
import { iconParser, urlParser } from '../../../utility';
interface ComponentProps {
category: Category;
@ -13,9 +13,12 @@ const BookmarkCard = (props: ComponentProps): JSX.Element => {
<div className={classes.BookmarkCard}>
<h3>{props.category.name}</h3>
<div className={classes.Bookmarks}>
{props.category.bookmarks.map((bookmark: Bookmark) => (
{props.category.bookmarks.map((bookmark: Bookmark) => {
const [displayUrl, redirectUrl] = urlParser(bookmark.url);
return (
<a
href={`http://${bookmark.url}`}
href={redirectUrl}
target='_blank'
key={`bookmark-${bookmark.id}`}>
{bookmark.icon && (
@ -25,7 +28,8 @@ const BookmarkCard = (props: ComponentProps): JSX.Element => {
)}
{bookmark.name}
</a>
))}
)
})}
</div>
</div>
)

View File

@ -1,3 +1,8 @@
/**
* Parse Material Desgin icon name to be used with mdi/js
* @param mdiName Dash separated icon name from MDI, e.g. alert-box-outline
* @returns Parsed icon name to be used with mdi/js, e.g mdiAlertBoxOutline
*/
export const iconParser = (mdiName: string): string => {
let parsedName = mdiName
.split('-')

View File

@ -0,0 +1,2 @@
export * from './iconParser';
export * from './urlParser';

View File

@ -0,0 +1,20 @@
export const urlParser = (url: string): string[] => {
let parsedUrl: string;
let displayUrl: string;
if (/https?:\/\//.test(url)) {
// Url starts with http[s]:// -> leave it as it is
parsedUrl = url;
} else {
// No protocol -> apply http:// prefix
parsedUrl = `http://${url}`;
}
// Create simplified url to display as text
displayUrl = url
.replace(/https?:\/\//, '')
.replace('www.', '')
.replace(/\/$/, '');
return [displayUrl, parsedUrl]
}