aboutsummaryrefslogtreecommitdiffstats
path: root/webui/src/components/Content
diff options
context:
space:
mode:
authorSascha <GlancingMind@outlook.com>2021-04-07 18:51:51 +0200
committerSascha <GlancingMind@outlook.com>2021-04-07 19:30:09 +0200
commit23300d1c1a9f99462c08d4e43ca68b1c3fd0cfd2 (patch)
tree21c93cab199e2e2b505a8964fd8926c0bbb91d20 /webui/src/components/Content
parent50ef67664dd13ff8abc909be3c0ec0f46a616a15 (diff)
downloadgit-bug-23300d1c1a9f99462c08d4e43ca68b1c3fd0cfd2.tar.gz
Use custom anchor in markdown
The custom anchor will be more readable in dark mode. Also it will supports linking within the SPA.
Diffstat (limited to 'webui/src/components/Content')
-rw-r--r--webui/src/components/Content/AnchorTag.tsx38
-rw-r--r--webui/src/components/Content/index.tsx2
2 files changed, 40 insertions, 0 deletions
diff --git a/webui/src/components/Content/AnchorTag.tsx b/webui/src/components/Content/AnchorTag.tsx
new file mode 100644
index 00000000..47d5e2fa
--- /dev/null
+++ b/webui/src/components/Content/AnchorTag.tsx
@@ -0,0 +1,38 @@
+import React from 'react';
+import { Link } from 'react-router-dom';
+
+import { makeStyles } from '@material-ui/core/styles';
+
+const useStyles = makeStyles((theme) => ({
+ tag: {
+ color: theme.palette.text.secondary,
+ },
+}));
+
+const AnchorTag = ({ children, href }: React.HTMLProps<HTMLAnchorElement>) => {
+ const classes = useStyles();
+ const origin = window.location.origin;
+ const destination = href === undefined ? '' : href;
+ const isInternalLink =
+ destination.startsWith('/') || destination.startsWith(origin);
+ const internalDestination = destination.replace(origin, '');
+ const internalLink = (
+ <Link className={classes.tag} to={internalDestination}>
+ {children}
+ </Link>
+ );
+ const externalLink = (
+ <a
+ className={classes.tag}
+ href={destination}
+ target="_blank"
+ rel="noopener noreferrer"
+ >
+ {children}
+ </a>
+ );
+
+ return isInternalLink ? internalLink : externalLink;
+};
+
+export default AnchorTag;
diff --git a/webui/src/components/Content/index.tsx b/webui/src/components/Content/index.tsx
index 809ae818..e4018809 100644
--- a/webui/src/components/Content/index.tsx
+++ b/webui/src/components/Content/index.tsx
@@ -5,6 +5,7 @@ import parse from 'remark-parse';
import remark2react from 'remark-react';
import unified from 'unified';
+import AnchorTag from './AnchorTag';
import BlockQuoteTag from './BlockQuoteTag';
import ImageTag from './ImageTag';
import PreTag from './PreTag';
@@ -19,6 +20,7 @@ const Content: React.FC<Props> = ({ markdown }: Props) => {
remarkReactComponents: {
img: ImageTag,
pre: PreTag,
+ a: AnchorTag,
blockquote: BlockQuoteTag,
},
})