github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/web/src/links.ts (about)

     1  // Helper functions for displaying links
     2  
     3  // If a URL contains the hostname 0.0.0.0, resolve that to the current
     4  // window.location.hostname. This ensures that tilt sends users to the
     5  // right place when it's being accessed from a remote machine.
     6  export function resolveURL(input: string): string {
     7    if (!input) {
     8      return input
     9    }
    10    try {
    11      let url = new URL(input)
    12      if (url.hostname === "0.0.0.0") {
    13        url.hostname = window.location.hostname
    14        return url.toString()
    15      }
    16    } catch (err) {} // fall through
    17    return input
    18  }
    19  
    20  export function displayURL(url: string): string {
    21    url = url.replace(/^http:\/\//, "")
    22    url = url.replace(/^https:\/\//, "")
    23    url = url.replace(/^www\./, "")
    24    url = url.replace(/^([^\/]+)\/$/, "$1")
    25    return url || ""
    26  }