| 
 | 1 | +import { Sitemap } from "../sitemap"  | 
 | 2 | + | 
 | 3 | +type CorrespondingURLParams = {  | 
 | 4 | +  selectedVersionSitemap: Sitemap  | 
 | 5 | +  selectedVersionBaseURL: URL  | 
 | 6 | +  currentLocation: URL  | 
 | 7 | +  currentBaseURL: string  | 
 | 8 | +}  | 
 | 9 | + | 
 | 10 | +/**  | 
 | 11 | + * Choose a URL to navigate to when the user chooses a version in the version  | 
 | 12 | + * selector.  | 
 | 13 | + *  | 
 | 14 | + * @param selectedVersionSitemap - as obtained by fetchSitemap from `${selectedVersionBaseURL}/sitemap.xml`  | 
 | 15 | + * @param selectedVersionBaseURL - usually `${currentBaseURL}/../selectedVersion`  | 
 | 16 | + * @param currentLocation - current web browser location  | 
 | 17 | + * @param currentBaseURL - as obtained from `config.base`  | 
 | 18 | + * @returns the URL to navigate to or null if we can't be sure that the  | 
 | 19 | + * corresponding page to the current page exists in the selected version  | 
 | 20 | + */  | 
 | 21 | +export function selectedVersionCorrespondingURL(  | 
 | 22 | +  {selectedVersionSitemap,  | 
 | 23 | +    selectedVersionBaseURL,  | 
 | 24 | +    currentLocation,  | 
 | 25 | +    currentBaseURL}: CorrespondingURLParams  | 
 | 26 | +): URL | undefined {  | 
 | 27 | +  const current_path = (new URL(currentBaseURL)).pathname  | 
 | 28 | +  const currentRelativePath = stripPrefix(currentLocation.pathname, current_path)  | 
 | 29 | +  if (currentRelativePath === undefined) {  | 
 | 30 | +    return  | 
 | 31 | +  }  | 
 | 32 | +  const sitemapCommonPrefix = shortestCommonPrefix(selectedVersionSitemap.keys())  | 
 | 33 | +  if (!selectedVersionSitemap.has(sitemapCommonPrefix)) {  | 
 | 34 | +    // We could also check that `commonSitemapPrefix` ends in the canonical version,  | 
 | 35 | +    // similarly to https://github.com/squidfunk/mkdocs-material/pull/7227. However,  | 
 | 36 | +    // I don't believe that Mike/MkDocs ever generate sitemaps where it would matter  | 
 | 37 | +    return  | 
 | 38 | +  }  | 
 | 39 | + | 
 | 40 | +  const potentialSitemapURL = new URL(currentRelativePath, sitemapCommonPrefix)  | 
 | 41 | +  if (!selectedVersionSitemap.has(potentialSitemapURL.href)) {  | 
 | 42 | +    return  | 
 | 43 | +  }  | 
 | 44 | + | 
 | 45 | +  const result = new URL(currentRelativePath, selectedVersionBaseURL)  | 
 | 46 | +  result.hash = currentLocation.hash  | 
 | 47 | +  result.search = currentLocation.search  | 
 | 48 | +  return result  | 
 | 49 | +}  | 
 | 50 | + | 
 | 51 | +// Basic string manipulation  | 
 | 52 | + | 
 | 53 | +/**  | 
 | 54 | + *  | 
 | 55 | + * @param s - string  | 
 | 56 | + * @param prefix - prefix to strip  | 
 | 57 | + *  | 
 | 58 | + * @returns either the string with the prefix stripped or undefined if the  | 
 | 59 | + * string did not begin with the prefix.  | 
 | 60 | + */  | 
 | 61 | +export function stripPrefix(s: string, prefix: string): string | undefined {  | 
 | 62 | +  if (s.startsWith(prefix)) {  | 
 | 63 | +    return s.slice(prefix.length)  | 
 | 64 | +  }  | 
 | 65 | +  return undefined  | 
 | 66 | +}  | 
 | 67 | + | 
 | 68 | +/**  | 
 | 69 | + *  | 
 | 70 | + * @param s1 - first string  | 
 | 71 | + * @param s2 - second string  | 
 | 72 | + *  | 
 | 73 | + * @returns - the length of the longest common prefix of the two strings.  | 
 | 74 | + */  | 
 | 75 | +function commonPrefixLen(s1: string, s2: string): number {  | 
 | 76 | +  const max = Math.min(s1.length, s2.length)  | 
 | 77 | +  let result  | 
 | 78 | +  for (result = 0; result < max; ++result) {  | 
 | 79 | +    if (s1[result] !== s2[result]) {  | 
 | 80 | +      break  | 
 | 81 | +    }  | 
 | 82 | +  }  | 
 | 83 | +  return result  | 
 | 84 | +}  | 
 | 85 | + | 
 | 86 | +/**  | 
 | 87 | + *  | 
 | 88 | + * @param strs - an iterable of strings  | 
 | 89 | + *  | 
 | 90 | + * @returns the longest common prefix of all the strings  | 
 | 91 | + */  | 
 | 92 | +export function shortestCommonPrefix(strs: Iterable<string>): string {  | 
 | 93 | +  let result  // Undefined if no iterations happened  | 
 | 94 | +  for (const s of strs) {  | 
 | 95 | +    if (result === undefined) {  | 
 | 96 | +      result = s  | 
 | 97 | +    } else {  | 
 | 98 | +      result = result.slice(0, commonPrefixLen(result, s))  | 
 | 99 | +    }  | 
 | 100 | +  }  | 
 | 101 | +  return result ?? ""  | 
 | 102 | +}  | 
0 commit comments