diff --git a/cli.js b/cli.js index a65c2ba..9c58c49 100755 --- a/cli.js +++ b/cli.js @@ -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(); diff --git a/package.json b/package.json index e63eab3..6a24f04 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,6 @@ }, "scripts": { "test": "node --test", - "demo": "node .", "prepare": "husky" }, "publishConfig": { diff --git a/src/Fetcher.js b/src/Fetcher.js index 6474bc1..a28546c 100644 --- a/src/Fetcher.js +++ b/src/Fetcher.js @@ -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) { @@ -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(); @@ -99,6 +100,10 @@ class Fetcher { this.#assetsFolder = folder; } + setDownloadAssets(download) { + this.#downloadAssets = Boolean(download); + } + setUseRelativeAssetPaths(use) { this.useRelativeAssets = Boolean(use); } @@ -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); diff --git a/src/Importer.js b/src/Importer.js index 3994cc1..e8ca16e 100644 --- a/src/Importer.js +++ b/src/Importer.js @@ -94,6 +94,10 @@ class Importer { this.fetcher.setAssetsFolder(folder); } + shouldDownloadAssets() { + return this.#assetReferenceType !== "disabled"; + } + isAssetsColocated() { return this.#assetReferenceType === "colocate"; } @@ -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; @@ -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) {