links: enforce valid URI on render

Fixes urbit/landscape#280
This commit is contained in:
Matilde Park 2021-02-25 14:30:50 -05:00
parent cc6adb3ffd
commit 52e5d4a011

View File

@ -67,6 +67,7 @@ export const LinkItem = (props: LinkItemProps): ReactElement => {
const size = node.children ? node.children.size : 0; const size = node.children ? node.children.size : 0;
const contents = node.post.contents; const contents = node.post.contents;
const hostname = URLparser.exec(contents[1].url) ? URLparser.exec(contents[1].url)[4] : null; const hostname = URLparser.exec(contents[1].url) ? URLparser.exec(contents[1].url)[4] : null;
const href = URLparser.exec(contents[1].url) ? contents[1].url : `http://${contents[1].url}`
const baseUrl = props.baseUrl || `/~404/${resource}`; const baseUrl = props.baseUrl || `/~404/${resource}`;
@ -120,7 +121,7 @@ export const LinkItem = (props: LinkItemProps): ReactElement => {
<RemoteContent <RemoteContent
ref={r => { remoteRef.current = r }} ref={r => { remoteRef.current = r }}
renderUrl={false} renderUrl={false}
url={contents[1].url} url={href}
text={contents[0].text} text={contents[0].text}
unfold={true} unfold={true}
onLoad={onMeasure} onLoad={onMeasure}
@ -145,7 +146,7 @@ export const LinkItem = (props: LinkItemProps): ReactElement => {
}} }}
/> />
<Text color="gray" p={2} flexShrink={0}> <Text color="gray" p={2} flexShrink={0}>
<Anchor target="_blank" rel="noopener noreferrer" style={{ textDecoration: 'none' }} href={contents[1].url}> <Anchor target="_blank" rel="noopener noreferrer" style={{ textDecoration: 'none' }} href={href}>
<Box display='flex'> <Box display='flex'>
<Icon icon='ArrowExternal' mr={1} />{hostname} <Icon icon='ArrowExternal' mr={1} />{hostname}
</Box> </Box>