{"version":3,"file":"filter-url-for-display.cjs","names":[],"sources":["../src/filter-url-for-display.ts"],"sourcesContent":["/**\n * Returns a URL for display.\n *\n * @param url       Original URL.\n * @param maxLength URL length.\n *\n * @example\n * ```js\n * const displayUrl = filterURLForDisplay( 'https://www.wordpress.org/gutenberg/' ); // wordpress.org/gutenberg\n * const imageUrl = filterURLForDisplay( 'https://www.wordpress.org/wp-content/uploads/img.png', 20 ); // …ent/uploads/img.png\n * ```\n *\n * @return Displayed URL.\n */\nexport function filterURLForDisplay(\n\turl: string,\n\tmaxLength: number | null = null\n): string {\n\tif ( ! url ) {\n\t\treturn '';\n\t}\n\n\t// Remove protocol and www prefixes.\n\tlet filteredURL = url\n\t\t.replace( /^[a-z\\-.\\+]+[0-9]*:(\\/\\/)?/i, '' )\n\t\t.replace( /^www\\./i, '' );\n\n\t// Ends with / and only has that single slash, strip it.\n\tif ( filteredURL.match( /^[^\\/]+\\/$/ ) ) {\n\t\tfilteredURL = filteredURL.replace( '/', '' );\n\t}\n\n\t// capture file name from URL\n\tconst fileRegexp = /\\/([^\\/?]+)\\.(?:[\\w]+)(?=\\?|$)/;\n\n\tif (\n\t\t! maxLength ||\n\t\tfilteredURL.length <= maxLength ||\n\t\t! filteredURL.match( fileRegexp )\n\t) {\n\t\treturn filteredURL;\n\t}\n\n\t// If the file is not greater than max length, return last portion of URL.\n\tfilteredURL = filteredURL.split( '?' )[ 0 ];\n\tconst urlPieces = filteredURL.split( '/' );\n\tconst file = urlPieces[ urlPieces.length - 1 ];\n\tif ( file.length <= maxLength ) {\n\t\treturn '…' + filteredURL.slice( -maxLength );\n\t}\n\n\t// If the file is greater than max length, truncate the file.\n\tconst index = file.lastIndexOf( '.' );\n\tconst [ fileName, extension ] = [\n\t\tfile.slice( 0, index ),\n\t\tfile.slice( index + 1 ),\n\t];\n\tconst truncatedFile = fileName.slice( -3 ) + '.' + extension;\n\treturn (\n\t\t// A negative end would be read as an offset from the end of the string,\n\t\t// keeping most of the file name and returning more than `maxLength`.\n\t\tfile.slice( 0, Math.max( 0, maxLength - truncatedFile.length - 1 ) ) +\n\t\t'…' +\n\t\ttruncatedFile\n\t);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAcA,SAAgB,oBACf,KACA,YAA2B,MAClB;CACT,IAAK,CAAE,KACN,OAAO;CAIR,IAAI,cAAc,IAChB,QAAS,+BAA+B,EAAG,CAAC,CAC5C,QAAS,WAAW,EAAG;CAGzB,IAAK,YAAY,MAAO,YAAa,GACpC,cAAc,YAAY,QAAS,KAAK,EAAG;CAM5C,IACC,CAAE,aACF,YAAY,UAAU,aACtB,CAAE,YAAY,MAAO,gCAAW,GAEhC,OAAO;CAIR,cAAc,YAAY,MAAO,GAAI,CAAC,CAAE;CACxC,MAAM,YAAY,YAAY,MAAO,GAAI;CACzC,MAAM,OAAO,UAAW,UAAU,SAAS;CAC3C,IAAK,KAAK,UAAU,WACnB,OAAO,MAAM,YAAY,MAAO,CAAC,SAAU;CAI5C,MAAM,QAAQ,KAAK,YAAa,GAAI;CACpC,MAAM,CAAE,UAAU,aAAc,CAC/B,KAAK,MAAO,GAAG,KAAM,GACrB,KAAK,MAAO,QAAQ,CAAE,CACvB;CACA,MAAM,gBAAgB,SAAS,MAAO,EAAG,IAAI,MAAM;CACnD,OAGC,KAAK,MAAO,GAAG,KAAK,IAAK,GAAG,YAAY,cAAc,SAAS,CAAE,CAAE,IACnE,MACA;AAEF"}