JSFiddle - React, Tailwind, and code Playground

by gchoken

JavaScript

var urls = [
'https://greenblue.com/gb/about',
'https://greenblue.com/gb/us/meet-our-team/about',
'https://greenblue.com/gb/about-us/meet-our-team/',
'https://greenblue.com/gb/about-us/pilot-projects/',
'https://greenblue.com/gb/solutions/stormwater-management/',
'https://greenblue.com/gb/solutions/stormwater-management/term',
'https://gb.gleeds.com/address/gleeds-privacy-policy',
'https://greenblue.com/gb/contact',
'https://gb.gleeds.com/useful-links/gleeds-privacy-policy/#Part02',
'https://gb.gleeds.com/useful-links/gleeds-privacy-policy/#'
]

const  keywords =  "contact|address|location|about|find|direction|office|campus|centre|term|privacy";

let site = "greenblue.com"
let siteURL = new RegExp(`\(https?:\/\/)?(www\.)?${site}\/(.*)(${keywords})(.*)`);

let metched_urls = urls.filter((url)=>siteURL.test(url))

let parseable_urls = [];

 for (let index = 0; index < metched_urls.length; index++) {
                const url = metched_urls[index];
                parseable_urls.push(calcRanks(url))
}


console.log(parseable_urls);

parseable_urls.sort( (x, y)=> {
    return x.path_length - y.path_length;
});


parseable_urls.sort( (x, y)=> {
    return x.matching_term_rank - y.matching_term_rank;
});


parseable_urls.sort( (x, y)=> {
    return x.matching_directory - y.matching_directory;
});

console.log(parseable_urls);





function calcRanks(href){

  let keywords_arr = keywords.split("|");


  let url = new URL(href);

  let paths = (url.pathname).split('/').filter(n => n);
  var matching_term = "";
  var matching_directory = "";
  var matching_term_rank = "";
  let matched_path = paths.filter((w,index)=>{
    let res = keywords_arr.some(substring=>{

      let has_str = w.includes(substring)

      if(has_str){
        matching_term = substring;
        matching_term_rank = keywords_arr.indexOf(substring)+1;
      }

      return has_str;

    });

    if(res){
      matching_directory = index+1;
    }

    return res

  })

  var page_type = ""
 ...