aboutsummaryrefslogtreecommitdiff
path: root/src/lib/Markdown/Markdown.tsx
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2020-04-09 11:55:46 +0300
committereug-vs <eug-vs@keemail.me>2020-04-09 12:01:06 +0300
commitae3228b9b3e95ace25292d0231e98e6087781c0a (patch)
tree7b307909daea32398432d0ca45ffdc494e26bc6a /src/lib/Markdown/Markdown.tsx
parent46f410699037824408daf7c035458ba7bd958d44 (diff)
downloadreact-benzin-ae3228b9b3e95ace25292d0231e98e6087781c0a.tar.gz
feat: resolve local urls
Diffstat (limited to 'src/lib/Markdown/Markdown.tsx')
-rw-r--r--src/lib/Markdown/Markdown.tsx12
1 files changed, 10 insertions, 2 deletions
diff --git a/src/lib/Markdown/Markdown.tsx b/src/lib/Markdown/Markdown.tsx
index 0a71fd3..c929c71 100644
--- a/src/lib/Markdown/Markdown.tsx
+++ b/src/lib/Markdown/Markdown.tsx
@@ -8,15 +8,23 @@ interface PropTypes {
url?: string;
}
+const resolveUrls = (line: string, baseUrl: string = ''): string => {
+ return line.replace(/src="(?!http)(.*)"[\s>]/, (match, url, offset, string) => `src="${baseUrl}/${url}?sanitize=true"`)
+ .replace(/\[(.*\]?.*)\]\((?!http)(.+?)\)/, (match, text, url, offset, string) => `[${text}](${baseUrl}/${url})`);
+}
+
const Markdown: React.FC<PropTypes> = ({ data, url }) => {
const [markdown, setMarkdown] = useState<string>(data || '');
+ if (url) axios.get(url).then(response => setMarkdown(response.data));
+
useEffect(() => {
if (!url) setMarkdown(data || '');
}, [data, url]);
- if (url) axios.get(url).then(response => setMarkdown(response.data));
- return <Section rawLines={markdown.split(/\r?\n/)} />
+ const baseUrl = url?.slice(0, url.lastIndexOf('/'));
+ const lines = markdown.split(/\r?\n/).map(line => resolveUrls(line, baseUrl));
+ return <Section rawLines={lines} />
};