diff options
Diffstat (limited to 'src/lib/Markdown/Markdown.tsx')
-rw-r--r-- | src/lib/Markdown/Markdown.tsx | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/src/lib/Markdown/Markdown.tsx b/src/lib/Markdown/Markdown.tsx index 09ad54a..cfcf117 100644 --- a/src/lib/Markdown/Markdown.tsx +++ b/src/lib/Markdown/Markdown.tsx @@ -8,15 +8,26 @@ interface PropTypes { url?: string; } +const resolveUrls = (line: string, baseUrl: string): string => line.replace( + /src="(?!http)(.*)"[\s>]/, + (match, url) => `src="${baseUrl}/${url}?sanitize=true"`, +).replace( + /\[(.*\]?.*)\]\((?!http)(.+?)\)/, + (match, text, url) => `[${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('\n')} /> + const baseUrl = url?.slice(0, url.lastIndexOf('/')) || ''; + const lines = markdown.split(/\r?\n/).map(line => resolveUrls(line, baseUrl)); + return <Section rawLines={lines} />; }; |