Skip to content

Commit

Permalink
Add --assetrefs=disabled option to disable downloading any assets
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Dec 13, 2024
1 parent 104a0de commit d160767
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
3 changes: 2 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,11 @@ if(help) {
# Change output format (default: markdown)
npx @11ty/import [type] [target] --format=html
# Change asset reference URLs: relative (default), absolute, colocate
# Change asset reference URLs: relative (default), absolute, colocate, disabled
npx @11ty/import [type] [target] --assetrefs=relative
npx @11ty/import [type] [target] --assetrefs=absolute
npx @11ty/import [type] [target] --assetrefs=colocate
npx @11ty/import [type] [target] --assetrefs=disabled
`);

process.exit();
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
},
"scripts": {
"test": "node --test",
"demo": "node .",
"prepare": "husky"
},
"publishConfig": {
Expand Down
21 changes: 15 additions & 6 deletions src/Fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ const xmlParser = new XMLParser({
});

class Fetcher {
#cacheDuration = "0s";
#directoryManager;
#assetsFolder = "assets";
#persistManager;
#outputFolder = ".";
#downloadAssets = true;

static USER_AGENT = "Eleventy Import v1.0.0";

static getContextPathname(url) {
Expand Down Expand Up @@ -64,12 +71,6 @@ class Fetcher {
return xmlParser.parse(content);
}

#cacheDuration = "0s";
#directoryManager;
#assetsFolder = "assets";
#persistManager;
#outputFolder = ".";

constructor() {
this.fetchedUrls = new Set();
this.writtenAssetFiles = new Set();
Expand Down Expand Up @@ -99,6 +100,10 @@ class Fetcher {
this.#assetsFolder = folder;
}

setDownloadAssets(download) {
this.#downloadAssets = Boolean(download);
}

setUseRelativeAssetPaths(use) {
this.useRelativeAssets = Boolean(use);
}
Expand Down Expand Up @@ -153,6 +158,10 @@ class Fetcher {
}

async fetchAsset(assetUrl, contextEntry) {
if(!this.#downloadAssets) {
return assetUrl;
}

// Adds protocol from original page URL if a protocol relative URL
if(assetUrl.startsWith("//") && contextEntry.url) {
let contextUrl = new URL(contextEntry.url);
Expand Down
16 changes: 13 additions & 3 deletions src/Importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ class Importer {
this.fetcher.setAssetsFolder(folder);
}

shouldDownloadAssets() {
return this.#assetReferenceType !== "disabled";
}

isAssetsColocated() {
return this.#assetReferenceType === "colocate";
}
Expand All @@ -104,12 +108,14 @@ class Importer {
this.setAssetsFolder("");
}

if(refType === "absolute") {
if(refType === "disabled") {
this.fetcher.setDownloadAssets(false);
} else if(refType === "absolute") {
this.fetcher.setUseRelativeAssetPaths(false);
} else if(refType === "relative" || refType === "colocate") {
this.fetcher.setUseRelativeAssetPaths(true);
} else {
throw new Error(`Invalid value for --assetrefs, must be one of: relative, colocate, or absolute. Received: ${refType} (${typeof refType})`);
throw new Error(`Invalid value for --assetrefs, must be one of: relative, colocate, absolute, or disabled. Received: ${refType} (${typeof refType})`);
}

this.#assetReferenceType = refType;
Expand Down Expand Up @@ -275,7 +281,11 @@ class Importer {

if(Importer.isHtml(entry)) {
let decodedHtml = entities.decodeHTML(entry.content);
entry.content = await this.htmlTransformer.transform(decodedHtml, entry);
if(!this.shouldDownloadAssets()) {
entry.content = decodedHtml;
} else {
entry.content = await this.htmlTransformer.transform(decodedHtml, entry);
}
}

if(isWritingToMarkdown) {
Expand Down

0 comments on commit d160767

Please sign in to comment.