Skip to content

Commit

Permalink
Avoid raw asset URLs (#11785)
Browse files Browse the repository at this point in the history
  • Loading branch information
LitoMore authored Sep 17, 2024
1 parent b1d67ef commit ef1d49b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
12 changes: 6 additions & 6 deletions _data/simple-icons.json
Original file line number Diff line number Diff line change
Expand Up @@ -2796,7 +2796,7 @@
{
"title": "Carrefour",
"hex": "004E9F",
"source": "https://upload.wikimedia.org/wikipedia/commons/5/5b/Carrefour_logo.svg"
"source": "https://en.wikipedia.org/wiki/File:Carrefour_logo_no_tag.svg"
},
{
"title": "Carto",
Expand Down Expand Up @@ -3276,7 +3276,7 @@
{
"title": "Coca Cola",
"hex": "D00013",
"source": "https://upload.wikimedia.org/wikipedia/commons/c/ce/Coca-Cola_logo.svg"
"source": "https://commons.wikimedia.org/wiki/File:Coca-Cola_logo.svg"
},
{
"title": "Cockpit",
Expand Down Expand Up @@ -3941,12 +3941,12 @@
{
"title": "DaisyUI",
"hex": "5A0EF8",
"source": "https://raw.githubusercontent.com/saadeghi/files/main/daisyui/logo-4.svg"
"source": "https://github.com/saadeghi/files/blob/5c82a1f4428b7c0af6ecfe921a424ebc9d92dc2c/daisyui/logo-4.svg"
},
{
"title": "Dapr",
"hex": "0D2192",
"source": "https://raw.githubusercontent.com/dapr/dapr/18575823c74318c811d6cd6f57ffac76d5debe93/img/dapr_logo.svg"
"source": "https://github.com/dapr/dapr/blob/18575823c74318c811d6cd6f57ffac76d5debe93/img/dapr_logo.svg"
},
{
"title": "Dark Reader",
Expand Down Expand Up @@ -5860,7 +5860,7 @@
{
"title": "Game & Watch",
"hex": "000000",
"source": "https://upload.wikimedia.org/wikipedia/commons/4/41/Game_and_watch_logo.svg"
"source": "https://commons.wikimedia.org/wiki/File:Game_and_watch_logo.svg"
},
{
"title": "Game Developer",
Expand Down Expand Up @@ -6492,7 +6492,7 @@
{
"title": "Google Maps",
"hex": "4285F4",
"source": "https://upload.wikimedia.org/wikipedia/commons/a/a9/Google_Maps_icon.svg"
"source": "https://commons.wikimedia.org/wiki/File:Google_Maps_icon.svg"
},
{
"title": "Google Marketing Platform",
Expand Down
52 changes: 41 additions & 11 deletions scripts/lint/ourlint.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* @typedef {IconData[]} IconsData
*/

import path from 'node:path';
import process from 'node:process';
import fakeDiff from 'fake-diff';
import {collator, getIconsDataString, normalizeNewlines} from '../../sdk.mjs';
Expand Down Expand Up @@ -88,13 +89,27 @@ const TESTS = {
checkUrl(data) {
/**
* Check if an URL has a redundant trailing slash.
* @param {string} url URL to check
* @param {URL} $url URL instance
* @param {string} url Original URL string
* @returns {boolean} Whether the URL has a redundant trailing slash
*/
const hasRedundantTrailingSlash = (url) => {
const {origin} = new global.URL(url);
return /^\/+$/.test(url.replace(origin, ''));
};
const hasRedundantTrailingSlash = ($url, url) => url === $url.origin + '/';

/**
* Check if an URL is static wikimedia asset URL.
* @param {URL} $url URL instance
* @returns {boolean} Whether the URL is static wikimedia asset URL
*/
const isStaticWikimediaAssetUrl = ($url) =>
$url.hostname === 'upload.wikimedia.org';

/**
* Check if an URL is raw GitHub asset URL.
* @param {string} $url URL instance
* @returns {boolean} Whether the URL is raw GitHub asset URL
*/
const isRawGithubAssetUrl = ($url) =>
$url.hostname === 'raw.githubusercontent.com';

const allUrlFields = [
...new Set(
Expand All @@ -117,14 +132,29 @@ const TESTS = {
),
];

const invalidUrls = allUrlFields.filter((url) =>
hasRedundantTrailingSlash(url),
);
const invalidUrls = [];
for (const url of allUrlFields) {
const $url = new global.URL(url);

if (hasRedundantTrailingSlash($url, url)) {
invalidUrls.push(fakeDiff(url, $url.origin));
}

if (isStaticWikimediaAssetUrl($url)) {
const expectedUrl = `https://commons.wikimedia.org/wiki/File:${path.basename($url.pathname)}`;
invalidUrls.push(fakeDiff(url, expectedUrl));
}

if (isRawGithubAssetUrl($url)) {
// https://github.com/LitoMore/simple-icons-cdn/blob/main/media/imgcat-screenshot.webp
const [, owner, repo, hash, ...directory] = $url.pathname.split('/');
const expectedUrl = `https://github.com/${owner}/${repo}/blob/${hash}/${directory.join('/')}`;
invalidUrls.push(fakeDiff(url, expectedUrl));
}
}

if (invalidUrls.length > 0) {
return `Some URLs have a redundant trailing slash:\n\n${invalidUrls.join(
'\n',
)}`;
return `Invalid URLs:\n\n${invalidUrls.join('\n\n')}`;
}
},
};
Expand Down

0 comments on commit ef1d49b

Please sign in to comment.