Skip to content
This repository has been archived by the owner on Nov 3, 2022. It is now read-only.
/ packer Public archive
generated from wabarc/project-template

Commit

Permalink
Refine filepath
Browse files Browse the repository at this point in the history
  • Loading branch information
web-flow committed Oct 26, 2020
1 parent d4fabda commit 0d46f9d
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
const sanitize = (str: string) => {
if (typeof str !== 'string') {
return str;
}

str = str.replace(/"<>#%\{\}\|\\\^~\[\]`;\?:@=&/g, '').replace(/:\/\/|\//g, '-');

return Buffer.from(str).toString('utf8');
};

export const createFilename = (uri: string, title: string): string => {
const extension = 'html';
let filename = `unknown.${extension}`;
let url: URL;

try {
url = new URL(decodeURI(uri));
} catch (_) {
if (!uri || typeof uri != 'string' || uri.length === 0) {
return filename;
}

if (typeof title === 'string') {
title = title.replace(/\n|\r|\r\n/gm, '');
}

const sanitize = (str: string) => {
if (typeof str !== 'string') {
return str;
}

str = str.replace(/"<>#%\{\}\|\\\^~\[\]`;\?:@=&/g, '');

return Buffer.from(str).toString('utf8');
};
try {
url = new URL(decodeURI(uri));
} catch (_) {
return sanitize(`${uri.replace(/\./g, '-')}-${title}.${extension}`);
}

const hostname = url.hostname
.replace(/\./g, '-')
Expand Down

0 comments on commit 0d46f9d

Please sign in to comment.