Repository: 11ty/eleventy (js)

Issue:
Data option to process a template file but not write it to the output directory
---
write: false
---

This would be similar to the `_underscoreprefix.html` in jekyll.

Pull request description:
Working implementation of `permalink: false` to fix #61

Patch:
diff --git a/src/Template.js b/src/Template.js
index 9429d4a4..be09c888 100644
--- a/src/Template.js
+++ b/src/Template.js
@@ -6,6 +6,7 @@ const { DateTime } = require("luxon");
 const TemplateContent = require("./TemplateContent");
 const TemplatePath = require("./TemplatePath");
 const TemplatePermalink = require("./TemplatePermalink");
+const TemplatePermalinkNoWrite = require("./TemplatePermalinkNoWrite");
 const TemplateLayout = require("./TemplateLayout");
 const TemplateFileSlug = require("./TemplateFileSlug");
 const Pagination = require("./Plugins/Pagination");
@@ -100,6 +101,8 @@ class Template extends TemplateContent {
       );
 
       return perm;
+    } else if (permalink === false) {
+      return new TemplatePermalinkNoWrite();
     }
 
     return TemplatePermalink.generate(
@@ -124,8 +127,12 @@ class Template extends TemplateContent {
   // TODO check for conflicts, see if file already exists?
   async getOutputPath(data) {
     let uri = await this.getOutputLink(data);
-    // TODO this only works with immediate front matter and not data files
-    if ((await this.getFrontMatterData())[this.config.keys.permalinkRoot]) {
+    if (uri === false) {
+      return false;
+    } else if (
+      (await this.getFrontMatterData())[this.config.keys.permalinkRoot]
+    ) {
+      // TODO this only works with immediate front matter and not data files
       return normalize(uri);
     } else {
       return normalize(this.outputDir + "/" + uri);
@@ -136,10 +143,6 @@ class Template extends TemplateContent {
     this.paginationData = paginationData;
   }
 
-  isIgnored() {
-    return this.outputDir === false;
-  }
-
   async mapDataAsRenderedTemplates(data, templateData) {
     if (Array.isArray(data)) {
       let arr = [];
@@ -417,22 +420,38 @@ class Template extends TemplateContent {
   }
 
   async _write(outputPath, finalContent) {
+    if (outputPath === false) {
+      debug(
+        "Ignored %o from %o (permalink: false).",
+        outputPath,
+        this.inputPath
+      );
+      return;
+    }
+
     this.writeCount++;
 
-    let writeDesc = this.isDryRun ? "Pretending to write" : "Writing";
+    let lang = {
+      start: "Writing",
+      finished: "written."
+    };
+
+    if (this.isDryRun) {
+      lang = {
+        start: "Pretending to write",
+        finished: "" // not used
+      };
+    }
+
     if (this.isVerbose) {
-      console.log(`${writeDesc} ${outputPath} from ${this.inputPath}.`);
+      console.log(`${lang.start} ${outputPath} from ${this.inputPath}.`);
     } else {
-      debug(`${writeDesc} %o from %o.`, outputPath, this.inputPath);
+      debug(`${lang.start} %o from %o.`, outputPath, this.inputPath);
     }
 
     if (!this.isDryRun) {
       return fs.outputFile(outputPath, finalContent).then(() => {
-        debug(
-          `${outputPath} ${
-            !this.isDryRun ? "written" : "pretended to be written"
-          }.`
-        );
+        debug(`${outputPath} ${lang.finished}.`);
       });
     }
   }
diff --git a/src/TemplatePermalink.js b/src/TemplatePermalink.js
index 20d95bd2..873e7707 100644
--- a/src/TemplatePermalink.js
+++ b/src/TemplatePermalink.js
@@ -1,5 +1,4 @@
 const parsePath = require("parse-filepath");
-const normalize = require("normalize-path");
 const TemplatePath = require("./TemplatePath");
 
 function TemplatePermalink(link, extraSubdir) {
diff --git a/src/TemplatePermalinkNoWrite.js b/src/TemplatePermalinkNoWrite.js
new file mode 100644
index 00000000..02174a22
--- /dev/null
+++ b/src/TemplatePermalinkNoWrite.js
@@ -0,0 +1,11 @@
+class TemplatePermalinkNoWrite {
+  toString() {
+    return false;
+  }
+
+  toHref() {
+    return false;
+  }
+}
+
+module.exports = TemplatePermalinkNoWrite;


Test patch:
diff --git a/test/TemplatePermalinkNoWriteTest.js b/test/TemplatePermalinkNoWriteTest.js
new file mode 100644
index 00000000..ad25b450
--- /dev/null
+++ b/test/TemplatePermalinkNoWriteTest.js
@@ -0,0 +1,8 @@
+import test from "ava";
+import TemplatePermalinkNoWrite from "../src/TemplatePermalinkNoWrite";
+
+test("Test standard method signature", t => {
+  let perm = new TemplatePermalinkNoWrite();
+  t.is(perm.toHref(), false);
+  t.is(perm.toString(), false);
+});
diff --git a/test/TemplateTest.js b/test/TemplateTest.js
index e0110133..f0dcbbe2 100644
--- a/test/TemplateTest.js
+++ b/test/TemplateTest.js
@@ -1,4 +1,5 @@
 import test from "ava";
+import fs from "fs-extra";
 import TemplateData from "../src/TemplateData";
 import Template from "../src/Template";
 import pretty from "pretty";
@@ -1114,3 +1115,29 @@ test("Test a linter", async t => {
     t.pass("Threw an error:" + e);
   }
 });
+
+test("permalink: false", async t => {
+  let tmpl = new Template(
+    "./test/stubs/permalink-false/test.md",
+    "./test/stubs/",
+    "./test/stubs/_site"
+  );
+
+  t.is(await tmpl.getOutputLink(), false);
+  t.is(await tmpl.getOutputHref(), false);
+
+  let data = await tmpl.getData();
+  await tmpl.write(false, data);
+
+  // Input file exists (sanity check for paths)
+  t.is(fs.existsSync("./test/stubs/permalink-false/"), true);
+  t.is(fs.existsSync("./test/stubs/permalink-false/test.md"), true);
+
+  // Output does not exist
+  t.is(fs.existsSync("./test/stubs/_site/permalink-false/"), false);
+  t.is(fs.existsSync("./test/stubs/_site/permalink-false/test/"), false);
+  t.is(
+    fs.existsSync("./test/stubs/_site/permalink-false/test/index.html"),
+    false
+  );
+});
diff --git a/test/stubs/permalink-false/test.md b/test/stubs/permalink-false/test.md
new file mode 100644
index 00000000..a03931c7
--- /dev/null
+++ b/test/stubs/permalink-false/test.md
@@ -0,0 +1,5 @@
+---
+permalink: false
+---
+
+This shouldn’t write