File tree Expand file tree Collapse file tree 4 files changed +38
-4
lines changed
Expand file tree Collapse file tree 4 files changed +38
-4
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import _isObject from "lodash/isObject";
55import _map from "lodash/map" ;
66import _truncate from "lodash/truncate" ;
77import { FormattedMessage } from "react-intl" ;
8+ import { valueOrExternalLink } from "../../../utils/linkUtils" ;
89import SvgSymbol from "../../SvgSymbol/SvgSymbol" ;
910import 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 ) ;
Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ import SvgSymbol from "../SvgSymbol/SvgSymbol";
1212import messages from "./Messages" ;
1313import "./OSMElementTags.scss" ;
1414import { useQuery } from "react-query" ;
15+ import { valueOrExternalLink } from "../../utils/linkUtils" ;
1516
1617const 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
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ import { Light as SyntaxHighlighter } from "react-syntax-highlighter";
1010import xmlLang from "react-syntax-highlighter/dist/esm/languages/hljs/xml" ;
1111import highlightColors from "react-syntax-highlighter/dist/esm/styles/hljs/agate" ;
1212import vkbeautify from "vkbeautify" ;
13+ import { valueOrExternalLink } from "../../utils/linkUtils" ;
1314import BusySpinner from "../BusySpinner/BusySpinner" ;
1415import SvgSymbol from "../SvgSymbol/SvgSymbol" ;
1516import 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 >
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments