Skip to content

Commit

Permalink
feat: allow to skip assets upload
Browse files Browse the repository at this point in the history
  • Loading branch information
kptdobe committed Aug 27, 2021
1 parent 4902eb0 commit 4d08a04
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
36 changes: 24 additions & 12 deletions src/importer/PageImporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,19 +187,31 @@ export default abstract class PageImporter implements Importer {
}
});

// upload all assets
const current = this;
await Promise.all(assets.map((asset) => {
const u = new URL(decodeURI(asset.url), url);
return current.upload(u.href).then(newSrc => {
if (asset.append) {
newSrc = `${newSrc}${asset.append}`;
}
contents = contents
.replace(new RegExp(`${asset.url.replace('.', '\\.').replace('?', '\\?')}`, 'gm'), newSrc)
.replace(new RegExp(`${decodeURI(asset.url).replace('.', '\\.')}`, 'gm'), newSrc);
const patchSrcInContent = (c, oldSrc, newSrc) => {
return contents
.replace(new RegExp(`${oldSrc.replace('.', '\\.').replace('?', '\\?')}`, 'gm'), newSrc)
.replace(new RegExp(`${decodeURI(oldSrc).replace('.', '\\.')}`, 'gm'), newSrc);
}

if (!this.params.skipAssetsUpload) {
// upload all assets
const current = this;
await Promise.all(assets.map((asset) => {
const u = new URL(decodeURI(asset.url), url);
return current.upload(u.href).then(newSrc => {
if (asset.append) {
newSrc = `${newSrc}${asset.append}`;
}
contents = patchSrcInContent(contents, asset.url, newSrc);
});
}));
} else {
// still need to adjust assets url (from relative to absolute)
assets.forEach((asset) => {
const u = new URL(decodeURI(asset.url), url);
contents = patchSrcInContent(contents, asset.url, u.toString());
});
}));
}

if (resource.prepend) {
contents = resource.prepend + contents;
Expand Down
1 change: 1 addition & 0 deletions src/importer/PageImporterParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ export default class PageImporterParams implements ImporterParams {
storageHandler: StorageHandler;
blobHandler: BlobHandler;
cache?: string;
skipAssetsUpload?: boolean
}

0 comments on commit 4d08a04

Please sign in to comment.