Skip to content

Commit

Permalink
fix: regressions in ghostery#16
Browse files Browse the repository at this point in the history
- Handle image files
- Fix path handling
  • Loading branch information
seia-soto committed Aug 7, 2024
1 parent d2352e9 commit ba96754
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/converters/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function getPathBasename(path) {
if (lastIndex === -1) {
return path;
}
return path.slice(lastIndex);
return path.slice(lastIndex + 1);
}

function getPathDirname(path) {
Expand All @@ -32,17 +32,41 @@ const allowedResourceExtensions = [
'xml',
'txt',
'json',
'png',
'gif',
'empty',
];

function getPreferredResource(aliases) {
// ignore non-supported files and manually created uBO aliases by AdGuard
return aliases.find(alias => {
const extension = alias.split('.').pop();
return allowedResourceExtensions.includes(extension) && !alias.startsWith('ubo-');
return extension !== undefined &&
allowedResourceExtensions.includes(extension) &&
!alias.startsWith('ubo-') &&
!alias.includes('-transparent');
});
}

function getFileExtensionByContentType(contentType) {
if (contentType.includes(';')) {
contentType = contentType.slice(0, contentType.indexOf(';'));
}
switch (contentType) {
case 'text/html':
return '.html';
case 'text/css':
return '.css';
case 'text/plain':
case 'application/javascript':
return '.js';
case 'application/json':
return '.json';
}

return '';
}

export function generateResourcesMapping() {
const resourcesMapping = new Map();

Expand All @@ -61,6 +85,7 @@ export function generateResourcesMapping() {

// Register to mapping
resourcesMapping.set(redirect.title, preferredResourceName);
resourcesMapping.set(redirect.title + getFileExtensionByContentType(redirect.contentType), preferredResourceName);
for (const alias of redirect.aliases) {
if (alias !== preferredResourceName) {
resourcesMapping.set(alias, preferredResourceName);
Expand Down

0 comments on commit ba96754

Please sign in to comment.