Skip to content

Commit b4f0ea9

Browse files
authored
Convert url based tag values into links (#2710)
- Fixes #2098
1 parent ba21098 commit b4f0ea9

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

src/components/EnhancedMap/PropertyList/PropertyList.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import _isObject from "lodash/isObject";
55
import _map from "lodash/map";
66
import _truncate from "lodash/truncate";
77
import { FormattedMessage } from "react-intl";
8+
import { valueOrExternalLink } from "../../../utils/linkUtils";
89
import SvgSymbol from "../../SvgSymbol/SvgSymbol";
910
import messages from "./Messages";
1011

@@ -69,7 +70,7 @@ const PropertyList = (props) => {
6970
<td
7071
className={classNames("value", { "mr-border-none mr-break-words mr-pb-1": darkMode })}
7172
>
72-
{value}
73+
{valueOrExternalLink(key, value)}
7374
</td>
7475
</tr>
7576
);

src/components/OSMElementTags/OSMElementTags.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import SvgSymbol from "../SvgSymbol/SvgSymbol";
1212
import messages from "./Messages";
1313
import "./OSMElementTags.scss";
1414
import { useQuery } from "react-query";
15+
import { valueOrExternalLink } from "../../utils/linkUtils";
1516

1617
const OSM_SERVER = window.env.REACT_APP_OSM_SERVER;
1718

@@ -86,7 +87,7 @@ const OSMElementTags = (props) => {
8687
const tagsValues = _map(element.tag, ({ k, v }) => (
8788
<Fragment key={k}>
8889
<dt>{k}</dt>
89-
<dd>{v}</dd>
90+
<dd>{valueOrExternalLink(k, v)}</dd>
9091
</Fragment>
9192
));
9293

src/components/TagDiffVisualization/TagDiffVisualization.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { Light as SyntaxHighlighter } from "react-syntax-highlighter";
1010
import xmlLang from "react-syntax-highlighter/dist/esm/languages/hljs/xml";
1111
import highlightColors from "react-syntax-highlighter/dist/esm/styles/hljs/agate";
1212
import vkbeautify from "vkbeautify";
13+
import { valueOrExternalLink } from "../../utils/linkUtils";
1314
import BusySpinner from "../BusySpinner/BusySpinner";
1415
import SvgSymbol from "../SvgSymbol/SvgSymbol";
1516
import messages from "./Messages";
@@ -309,7 +310,7 @@ export class TagDiffVisualization extends Component {
309310
key={`${change.name}_value`}
310311
>
311312
<div className="mr-px-2 mr-overflow-x-hidden mr-truncate mr-text-base" title={change.value}>
312-
{change.value}
313+
{valueOrExternalLink(change.name, change.value)}
313314
</div>
314315
</li>
315316
));
@@ -371,7 +372,7 @@ export class TagDiffVisualization extends Component {
371372
className="mr-px-2 mr-overflow-x-hidden mr-truncate mr-text-base"
372373
title={change.newValue}
373374
>
374-
{change.newValue}
375+
{valueOrExternalLink(change.name, change.newValue)}
375376
</div>
376377
)}
377378
</li>

src/utils/linkUtils.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Converts linkable field values to external links, otherwise returns plain text
3+
*/
4+
export function valueOrExternalLink(key, value) {
5+
const linkableFields = ["contact:website", "website", "url"];
6+
7+
// If the key is one of the known linkable fields and valid, inject a link rather than the raw value
8+
if (linkableFields.includes(key) && isValidUrl(value)) {
9+
return (
10+
<a target="_blank" rel="noopener noreferrer" href={value}>
11+
{value}
12+
</a>
13+
);
14+
}
15+
16+
// Otherwise, return the value as plain text
17+
return <>{value}</>;
18+
}
19+
20+
// Parse and test for usable urls
21+
function isValidUrl(urlText) {
22+
try {
23+
// Validate that the url is well-formed
24+
const url = new URL(urlText);
25+
// Only allow http and https protocols
26+
return url.protocol === "http:" || url.protocol === "https:";
27+
} catch {
28+
// If URL is invalid, just return false
29+
}
30+
return false;
31+
}

0 commit comments

Comments
 (0)