From 05aa59a37e5c716f6349c4ac304e9ab8bab55434 Mon Sep 17 00:00:00 2001 From: Archie Kennedy <71777327+archiekennedy@users.noreply.github.com> Date: Thu, 15 Apr 2021 13:56:52 +0100 Subject: [PATCH 1/4] Add {{path}} variable containing path to the current page Variable includes leading and trailing slash. Excludes filename. --- lib/render.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/render.js b/lib/render.js index 082e58b..b5ac62a 100644 --- a/lib/render.js +++ b/lib/render.js @@ -50,9 +50,14 @@ function render(file, enc, cb) { // Add this page's front matter pageData = extend(pageData, page.attributes); + // Full path to page excluding filename + var pagePath = path.relative(this.options.root, path.dirname(file.path)); + pagePath = (pagePath) ? '/' + pagePath + '/' : '/'; + // Finish by adding constants pageData = extend(pageData, { page: path.basename(file.path, path.extname(file.path)), + path: pagePath, layout: layout, root: processRoot(file.path, this.options.root) }); From e0cdfd8ac6939646c14e80907e3e7dc082bfef48 Mon Sep 17 00:00:00 2001 From: Archie Kennedy <71777327+archiekennedy@users.noreply.github.com> Date: Thu, 15 Apr 2021 14:02:10 +0100 Subject: [PATCH 2/4] Add unit test for {{path}} variable --- test/fixtures/variable-path/build/index.html | 7 +++++++ .../build/subfolder/another/index.html | 7 +++++++ .../variable-path/build/subfolder/index.html | 7 +++++++ .../fixtures/variable-path/expected/index.html | 7 +++++++ .../expected/subfolder/another/index.html | 7 +++++++ .../expected/subfolder/index.html | 7 +++++++ .../variable-path/layouts/default.html | 5 +++++ test/fixtures/variable-path/pages/index.html | 2 ++ .../pages/subfolder/another/index.html | 2 ++ .../variable-path/pages/subfolder/index.html | 2 ++ .../variable-path/partials/partial.html | 1 + test/test.js | 18 ++++++++++++++++++ 12 files changed, 72 insertions(+) create mode 100644 test/fixtures/variable-path/build/index.html create mode 100644 test/fixtures/variable-path/build/subfolder/another/index.html create mode 100644 test/fixtures/variable-path/build/subfolder/index.html create mode 100644 test/fixtures/variable-path/expected/index.html create mode 100644 test/fixtures/variable-path/expected/subfolder/another/index.html create mode 100644 test/fixtures/variable-path/expected/subfolder/index.html create mode 100644 test/fixtures/variable-path/layouts/default.html create mode 100644 test/fixtures/variable-path/pages/index.html create mode 100644 test/fixtures/variable-path/pages/subfolder/another/index.html create mode 100644 test/fixtures/variable-path/pages/subfolder/index.html create mode 100644 test/fixtures/variable-path/partials/partial.html diff --git a/test/fixtures/variable-path/build/index.html b/test/fixtures/variable-path/build/index.html new file mode 100644 index 0000000..1d43b62 --- /dev/null +++ b/test/fixtures/variable-path/build/index.html @@ -0,0 +1,7 @@ + +
+ / +/
+ + + diff --git a/test/fixtures/variable-path/build/subfolder/another/index.html b/test/fixtures/variable-path/build/subfolder/another/index.html new file mode 100644 index 0000000..5ec739a --- /dev/null +++ b/test/fixtures/variable-path/build/subfolder/another/index.html @@ -0,0 +1,7 @@ + + + /subfolder/another/ +/subfolder/another/
+ + + diff --git a/test/fixtures/variable-path/build/subfolder/index.html b/test/fixtures/variable-path/build/subfolder/index.html new file mode 100644 index 0000000..f99656c --- /dev/null +++ b/test/fixtures/variable-path/build/subfolder/index.html @@ -0,0 +1,7 @@ + + + /subfolder/ +/subfolder/
+ + + diff --git a/test/fixtures/variable-path/expected/index.html b/test/fixtures/variable-path/expected/index.html new file mode 100644 index 0000000..1d43b62 --- /dev/null +++ b/test/fixtures/variable-path/expected/index.html @@ -0,0 +1,7 @@ + + + / +/
+ + + diff --git a/test/fixtures/variable-path/expected/subfolder/another/index.html b/test/fixtures/variable-path/expected/subfolder/another/index.html new file mode 100644 index 0000000..5ec739a --- /dev/null +++ b/test/fixtures/variable-path/expected/subfolder/another/index.html @@ -0,0 +1,7 @@ + + + /subfolder/another/ +/subfolder/another/
+ + + diff --git a/test/fixtures/variable-path/expected/subfolder/index.html b/test/fixtures/variable-path/expected/subfolder/index.html new file mode 100644 index 0000000..f99656c --- /dev/null +++ b/test/fixtures/variable-path/expected/subfolder/index.html @@ -0,0 +1,7 @@ + + + /subfolder/ +/subfolder/
+ + + diff --git a/test/fixtures/variable-path/layouts/default.html b/test/fixtures/variable-path/layouts/default.html new file mode 100644 index 0000000..1ac4174 --- /dev/null +++ b/test/fixtures/variable-path/layouts/default.html @@ -0,0 +1,5 @@ + + + {{> body}} + + diff --git a/test/fixtures/variable-path/pages/index.html b/test/fixtures/variable-path/pages/index.html new file mode 100644 index 0000000..2c741f9 --- /dev/null +++ b/test/fixtures/variable-path/pages/index.html @@ -0,0 +1,2 @@ +{{path}} +{{> partial}} diff --git a/test/fixtures/variable-path/pages/subfolder/another/index.html b/test/fixtures/variable-path/pages/subfolder/another/index.html new file mode 100644 index 0000000..2c741f9 --- /dev/null +++ b/test/fixtures/variable-path/pages/subfolder/another/index.html @@ -0,0 +1,2 @@ +{{path}} +{{> partial}} diff --git a/test/fixtures/variable-path/pages/subfolder/index.html b/test/fixtures/variable-path/pages/subfolder/index.html new file mode 100644 index 0000000..2c741f9 --- /dev/null +++ b/test/fixtures/variable-path/pages/subfolder/index.html @@ -0,0 +1,2 @@ +{{path}} +{{> partial}} diff --git a/test/fixtures/variable-path/partials/partial.html b/test/fixtures/variable-path/partials/partial.html new file mode 100644 index 0000000..5c69979 --- /dev/null +++ b/test/fixtures/variable-path/partials/partial.html @@ -0,0 +1 @@ +{{path}}
diff --git a/test/test.js b/test/test.js index 8506373..89dddb5 100644 --- a/test/test.js +++ b/test/test.js @@ -205,6 +205,24 @@ describe('Panini variables', () => { }); }); + it('{{path}} variable that stores the path to the current page, excluding filename', done => { + var p = new Panini({ + root: FIXTURES + 'variable-path/pages/', + layouts: FIXTURES + 'variable-path/layouts/', + partials: FIXTURES + 'variable-path/partials/' + }); + + p.refresh(); + + src(FIXTURES + 'variable-path/pages/**/*.html') + .pipe(p.render()) + .pipe(dest(FIXTURES + 'variable-path/build')) + .on('finish', () => { + equal(FIXTURES + 'variable-path/expected', FIXTURES + 'variable-path/build'); + done(); + }); + }); + it('{{layout}} variable that stores the current layout', done => { var p = new Panini({ root: FIXTURES + 'variable-layout/pages/', From 02a34f1b1e0c073fc086039551618af7d8ac142a Mon Sep 17 00:00:00 2001 From: Archie Kennedy <71777327+archiekennedy@users.noreply.github.com> Date: Thu, 15 Apr 2021 14:42:52 +0100 Subject: [PATCH 3/4] Document the {{path}} variable in readme.md --- readme.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/readme.md b/readme.md index 83dd9b8..116b186 100644 --- a/readme.md +++ b/readme.md @@ -135,6 +135,8 @@ Data can also be a `.js` file with a `module.exports`. The data returned by the Data can also be inserted into the page itself with a Front Matter template at the top of the file. +The reserved `path` variable is added to every page. This stores the path to the current page, excluding the filename. + Lastly, the reserved `page` variable is added to every page template as it renders. It contains the name of the page being rendered, without the extension. ## CLI From c30d8bff598779a1ebb84b38f372538948d56a47 Mon Sep 17 00:00:00 2001 From: Archie Kennedy <71777327+archiekennedy@users.noreply.github.com> Date: Thu, 22 Apr 2021 13:06:14 +0100 Subject: [PATCH 4/4] Make path variable relative to keep it consistent with root and page --- lib/render.js | 2 +- readme.md | 2 +- test/fixtures/variable-path/build/index.html | 4 ++-- .../fixtures/variable-path/build/subfolder/another/index.html | 4 ++-- test/fixtures/variable-path/build/subfolder/index.html | 4 ++-- test/fixtures/variable-path/expected/index.html | 4 ++-- .../variable-path/expected/subfolder/another/index.html | 4 ++-- test/fixtures/variable-path/expected/subfolder/index.html | 4 ++-- 8 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/render.js b/lib/render.js index b5ac62a..735a4d0 100644 --- a/lib/render.js +++ b/lib/render.js @@ -52,7 +52,7 @@ function render(file, enc, cb) { // Full path to page excluding filename var pagePath = path.relative(this.options.root, path.dirname(file.path)); - pagePath = (pagePath) ? '/' + pagePath + '/' : '/'; + if (pagePath) pagePath += '/'; // Finish by adding constants pageData = extend(pageData, { diff --git a/readme.md b/readme.md index 116b186..82b8101 100644 --- a/readme.md +++ b/readme.md @@ -135,7 +135,7 @@ Data can also be a `.js` file with a `module.exports`. The data returned by the Data can also be inserted into the page itself with a Front Matter template at the top of the file. -The reserved `path` variable is added to every page. This stores the path to the current page, excluding the filename. +The reserved `path` variable is added to every page. This stores the path to the current page, relative to the root and without the filename. Lastly, the reserved `page` variable is added to every page template as it renders. It contains the name of the page being rendered, without the extension. diff --git a/test/fixtures/variable-path/build/index.html b/test/fixtures/variable-path/build/index.html index 1d43b62..169fc68 100644 --- a/test/fixtures/variable-path/build/index.html +++ b/test/fixtures/variable-path/build/index.html @@ -1,7 +1,7 @@ - / -/
+ + diff --git a/test/fixtures/variable-path/build/subfolder/another/index.html b/test/fixtures/variable-path/build/subfolder/another/index.html index 5ec739a..89b4f50 100644 --- a/test/fixtures/variable-path/build/subfolder/another/index.html +++ b/test/fixtures/variable-path/build/subfolder/another/index.html @@ -1,7 +1,7 @@ - /subfolder/another/ -/subfolder/another/
+ subfolder/another/ +subfolder/another/
diff --git a/test/fixtures/variable-path/build/subfolder/index.html b/test/fixtures/variable-path/build/subfolder/index.html index f99656c..83dac37 100644 --- a/test/fixtures/variable-path/build/subfolder/index.html +++ b/test/fixtures/variable-path/build/subfolder/index.html @@ -1,7 +1,7 @@ - /subfolder/ -/subfolder/
+ subfolder/ +subfolder/
diff --git a/test/fixtures/variable-path/expected/index.html b/test/fixtures/variable-path/expected/index.html index 1d43b62..169fc68 100644 --- a/test/fixtures/variable-path/expected/index.html +++ b/test/fixtures/variable-path/expected/index.html @@ -1,7 +1,7 @@ - / -/
+ + diff --git a/test/fixtures/variable-path/expected/subfolder/another/index.html b/test/fixtures/variable-path/expected/subfolder/another/index.html index 5ec739a..89b4f50 100644 --- a/test/fixtures/variable-path/expected/subfolder/another/index.html +++ b/test/fixtures/variable-path/expected/subfolder/another/index.html @@ -1,7 +1,7 @@ - /subfolder/another/ -/subfolder/another/
+ subfolder/another/ +subfolder/another/
diff --git a/test/fixtures/variable-path/expected/subfolder/index.html b/test/fixtures/variable-path/expected/subfolder/index.html index f99656c..83dac37 100644 --- a/test/fixtures/variable-path/expected/subfolder/index.html +++ b/test/fixtures/variable-path/expected/subfolder/index.html @@ -1,7 +1,7 @@ - /subfolder/ -/subfolder/
+ subfolder/ +subfolder/