From 751e24dae39fb2a2a2b216049f086a1e1c3a713e Mon Sep 17 00:00:00 2001 From: jaket-hub Date: Thu, 31 Oct 2024 19:38:55 +0000 Subject: [PATCH 1/4] page inline partials added to Handlebars.partials so as to be usable within layout pages modify render - only pages with inline partials - check for online partial(s) starting with "layout-", add them to Handlebars.partials - remove inline partial(s) text/string from page.body - all pages - check for Handlebars.partials starting with "layout-" and remove them modify test - add in test files and modify test script --- lib/render.js | 33 +++++++++++++++++++ .../fixtures/partials-inline/build/index.html | 9 +++++ .../build/no-inline-partial.html | 6 ++++ .../partials-inline/expected/index.html | 9 +++++ .../expected/no-inline-partial.html | 6 ++++ .../partials-inline/layouts/default.html | 8 +++++ .../fixtures/partials-inline/pages/index.html | 4 +++ .../pages/no-inline-partial.html | 1 + test/test.js | 17 ++++++++++ 9 files changed, 93 insertions(+) create mode 100644 test/fixtures/partials-inline/build/index.html create mode 100644 test/fixtures/partials-inline/build/no-inline-partial.html create mode 100644 test/fixtures/partials-inline/expected/index.html create mode 100644 test/fixtures/partials-inline/expected/no-inline-partial.html create mode 100644 test/fixtures/partials-inline/layouts/default.html create mode 100644 test/fixtures/partials-inline/pages/index.html create mode 100644 test/fixtures/partials-inline/pages/no-inline-partial.html diff --git a/lib/render.js b/lib/render.js index e5fb2cb..9606ab1 100644 --- a/lib/render.js +++ b/lib/render.js @@ -38,6 +38,39 @@ function render(file, enc, cb) { } } + //remove partials starting with "layout-", assume added via inline to be used within layout.hbs + //possible panini.options to add in a layouts-partial param for "layout-" + var inlineLayoutPartialList = Object.keys(this.Handlebars.partials).filter( + function (partial) { + return (partial.indexOf('layout-') === 0); + } + ); + inlineLayoutPartialList.forEach(partial => delete this.Handlebars.partials[partial]); + //add to Handlebars.partials any inline partials starting with "layout-" + if (page.body.indexOf('{{#*inline') !== -1 || page.body.indexOf('{{~#*inline') !== -1) { + // var inlinePartialRegex = /{{#\*inline[\s]+\"(?layout-[a-zA-Z_-]*)\"}}(?[.\s\S]*?){{\/inline}}/gm; + var inlinePartialRegex = /{{\~{0,1}#\*inline[\s]+(?[\"']{1})(?layout-[a-zA-Z_-]*)\k\s*}}(?[.\s\S]*?){{\/inline\s*\~{0,1}}}/gm; + // var inlinePartialMatch = page.body.match(inlinePartialRegex);//only gets match without group, can only get group when not global - later versions of javascript can use matchAll + + var replaceWith = ''; + var bodyText = page.body; + var inlinePartialMatch; + // var inlinePartialMatchList = []; + while (inlinePartialMatch = inlinePartialRegex.exec(page.body)) {//this way can get all match and group + // inlinePartialMatchList.push(inlinePartialMatch); + + // Add inline partials from page.body starting with "layout-" + this.Handlebars.registerPartial( + inlinePartialMatch.groups.name, + this.Handlebars.compile(inlinePartialMatch.groups.body + '\n') + ); + // replaceWith = '{{!--removed ' + inlinePartialMatch.groups.name + '--}}'; + bodyText = bodyText.replace(inlinePartialMatch[0], replaceWith); + } + // bodyText = bodyText.replace(inlinePartialRegex, ''); + page.body = bodyText;//amended body - removed inline partials starting with "layout-" + } + // Now create Handlebars templates out of them var pageTemplate = this.Handlebars.compile(page.body + '\n'); diff --git a/test/fixtures/partials-inline/build/index.html b/test/fixtures/partials-inline/build/index.html new file mode 100644 index 0000000..2fe75cb --- /dev/null +++ b/test/fixtures/partials-inline/build/index.html @@ -0,0 +1,9 @@ + + + +

Page Body

+ +

layout-inline-partial content

+ + + diff --git a/test/fixtures/partials-inline/build/no-inline-partial.html b/test/fixtures/partials-inline/build/no-inline-partial.html new file mode 100644 index 0000000..b06f555 --- /dev/null +++ b/test/fixtures/partials-inline/build/no-inline-partial.html @@ -0,0 +1,6 @@ + + +

Page Body

+

No layout-inline-partial default content

+ + diff --git a/test/fixtures/partials-inline/expected/index.html b/test/fixtures/partials-inline/expected/index.html new file mode 100644 index 0000000..2fe75cb --- /dev/null +++ b/test/fixtures/partials-inline/expected/index.html @@ -0,0 +1,9 @@ + + + +

Page Body

+ +

layout-inline-partial content

+ + + diff --git a/test/fixtures/partials-inline/expected/no-inline-partial.html b/test/fixtures/partials-inline/expected/no-inline-partial.html new file mode 100644 index 0000000..b06f555 --- /dev/null +++ b/test/fixtures/partials-inline/expected/no-inline-partial.html @@ -0,0 +1,6 @@ + + +

Page Body

+

No layout-inline-partial default content

+ + diff --git a/test/fixtures/partials-inline/layouts/default.html b/test/fixtures/partials-inline/layouts/default.html new file mode 100644 index 0000000..bf900ae --- /dev/null +++ b/test/fixtures/partials-inline/layouts/default.html @@ -0,0 +1,8 @@ + + + {{> body}} + {{#> layout-inline-partial}} +

No layout-inline-partial default content

+ {{/layout-inline-partial}} + + diff --git a/test/fixtures/partials-inline/pages/index.html b/test/fixtures/partials-inline/pages/index.html new file mode 100644 index 0000000..2093f1e --- /dev/null +++ b/test/fixtures/partials-inline/pages/index.html @@ -0,0 +1,4 @@ +{{#*inline "layout-inline-partial"}} +

layout-inline-partial content

+{{/inline}} +

Page Body

\ No newline at end of file diff --git a/test/fixtures/partials-inline/pages/no-inline-partial.html b/test/fixtures/partials-inline/pages/no-inline-partial.html new file mode 100644 index 0000000..87b7e72 --- /dev/null +++ b/test/fixtures/partials-inline/pages/no-inline-partial.html @@ -0,0 +1 @@ +

Page Body

\ No newline at end of file diff --git a/test/test.js b/test/test.js index 8506373..59866d6 100644 --- a/test/test.js +++ b/test/test.js @@ -78,6 +78,23 @@ describe('Panini', () => { }); }); + it('builds a page with inline partials used within layout', done => { + var p = new Panini({ + root: FIXTURES + 'partials-inline/pages/', + layouts: FIXTURES + 'partials-inline/layouts/' + }); + + p.refresh(); + + src(FIXTURES + 'partials-inline/pages/*') + .pipe(p.render()) + .pipe(dest(FIXTURES + 'partials-inline/build')) + .on('finish', () => { + equal(FIXTURES + 'partials-inline/expected', FIXTURES + 'partials-inline/build'); + done(); + }); + }); + it('builds a page with custom data', done => { var p = new Panini({ root: FIXTURES + 'data-page/pages/', From 6772a5413bdbc1f346ced8629e07a061f9878007 Mon Sep 17 00:00:00 2001 From: jaket-hub Date: Mon, 4 Nov 2024 15:28:38 +0000 Subject: [PATCH 2/4] cater for inline partial within handlebars comment https://regex101.com/r/RF1IOl/1 - for matching handlebars comment include numbers match in inline partials name https://regex101.com/r/qFKeWd/2 for matching handlebars inline partial handlebars comment removed from string for inline partial match Test - added top and bottom partials above and below body respectively - added test pages for inline partials top, bottom and commented inline partials --- lib/render.js | 6 +++-- ...mment-multiple-inline-partial-top-bot.html | 11 +++++++++ .../fixtures/partials-inline/build/index.html | 6 +++-- .../build/inline-partial-bot-comment-top.html | 9 ++++++++ .../build/inline-partial-bot.html | 9 ++++++++ ...nline-partial-top-bot-comment-top-bot.html | 7 ++++++ .../build/inline-partial-top-comment-bot.html | 9 ++++++++ .../build/inline-partial-top.html | 9 ++++++++ .../build/no-inline-partial.html | 3 ++- ...mment-multiple-inline-partial-top-bot.html | 11 +++++++++ .../partials-inline/expected/index.html | 6 +++-- .../inline-partial-bot-comment-top.html | 9 ++++++++ .../expected/inline-partial-bot.html | 9 ++++++++ ...nline-partial-top-bot-comment-top-bot.html | 7 ++++++ .../inline-partial-top-comment-bot.html | 9 ++++++++ .../expected/inline-partial-top.html | 9 ++++++++ .../expected/no-inline-partial.html | 3 ++- .../partials-inline/layouts/default.html | 9 +++++--- ...mment-multiple-inline-partial-top-bot.html | 23 +++++++++++++++++++ .../fixtures/partials-inline/pages/index.html | 7 ++++-- .../pages/inline-partial-bot-comment-top.html | 9 ++++++++ .../pages/inline-partial-bot.html | 4 ++++ ...nline-partial-top-bot-comment-top-bot.html | 9 ++++++++ .../pages/inline-partial-top-comment-bot.html | 9 ++++++++ .../pages/inline-partial-top.html | 4 ++++ 25 files changed, 193 insertions(+), 13 deletions(-) create mode 100644 test/fixtures/partials-inline/build/comment-multiple-inline-partial-top-bot.html create mode 100644 test/fixtures/partials-inline/build/inline-partial-bot-comment-top.html create mode 100644 test/fixtures/partials-inline/build/inline-partial-bot.html create mode 100644 test/fixtures/partials-inline/build/inline-partial-top-bot-comment-top-bot.html create mode 100644 test/fixtures/partials-inline/build/inline-partial-top-comment-bot.html create mode 100644 test/fixtures/partials-inline/build/inline-partial-top.html create mode 100644 test/fixtures/partials-inline/expected/comment-multiple-inline-partial-top-bot.html create mode 100644 test/fixtures/partials-inline/expected/inline-partial-bot-comment-top.html create mode 100644 test/fixtures/partials-inline/expected/inline-partial-bot.html create mode 100644 test/fixtures/partials-inline/expected/inline-partial-top-bot-comment-top-bot.html create mode 100644 test/fixtures/partials-inline/expected/inline-partial-top-comment-bot.html create mode 100644 test/fixtures/partials-inline/expected/inline-partial-top.html create mode 100644 test/fixtures/partials-inline/pages/comment-multiple-inline-partial-top-bot.html create mode 100644 test/fixtures/partials-inline/pages/inline-partial-bot-comment-top.html create mode 100644 test/fixtures/partials-inline/pages/inline-partial-bot.html create mode 100644 test/fixtures/partials-inline/pages/inline-partial-top-bot-comment-top-bot.html create mode 100644 test/fixtures/partials-inline/pages/inline-partial-top-comment-bot.html create mode 100644 test/fixtures/partials-inline/pages/inline-partial-top.html diff --git a/lib/render.js b/lib/render.js index 9606ab1..99f9e67 100644 --- a/lib/render.js +++ b/lib/render.js @@ -48,15 +48,16 @@ function render(file, enc, cb) { inlineLayoutPartialList.forEach(partial => delete this.Handlebars.partials[partial]); //add to Handlebars.partials any inline partials starting with "layout-" if (page.body.indexOf('{{#*inline') !== -1 || page.body.indexOf('{{~#*inline') !== -1) { + var commentRegex = /{{!--(?[.\s\S]*?)--}}/gm; // var inlinePartialRegex = /{{#\*inline[\s]+\"(?layout-[a-zA-Z_-]*)\"}}(?[.\s\S]*?){{\/inline}}/gm; - var inlinePartialRegex = /{{\~{0,1}#\*inline[\s]+(?[\"']{1})(?layout-[a-zA-Z_-]*)\k\s*}}(?[.\s\S]*?){{\/inline\s*\~{0,1}}}/gm; + var inlinePartialRegex = /{{\~{0,1}#\*inline[\s]+(?[\"']{1})(?layout-[0-9a-zA-Z_-]*)\k\s*}}(?[.\s\S]*?){{\/inline\s*\~{0,1}}}[\s]*/gm; // var inlinePartialMatch = page.body.match(inlinePartialRegex);//only gets match without group, can only get group when not global - later versions of javascript can use matchAll var replaceWith = ''; var bodyText = page.body; var inlinePartialMatch; // var inlinePartialMatchList = []; - while (inlinePartialMatch = inlinePartialRegex.exec(page.body)) {//this way can get all match and group + while (inlinePartialMatch = inlinePartialRegex.exec(page.body.replace(commentRegex, ''))) {//this way can get all match and group - note checking body with removed handlebars comments // inlinePartialMatchList.push(inlinePartialMatch); // Add inline partials from page.body starting with "layout-" @@ -69,6 +70,7 @@ function render(file, enc, cb) { } // bodyText = bodyText.replace(inlinePartialRegex, ''); page.body = bodyText;//amended body - removed inline partials starting with "layout-" + //note: would not harm to use body with removed handlebars comments } // Now create Handlebars templates out of them diff --git a/test/fixtures/partials-inline/build/comment-multiple-inline-partial-top-bot.html b/test/fixtures/partials-inline/build/comment-multiple-inline-partial-top-bot.html new file mode 100644 index 0000000..f6170c8 --- /dev/null +++ b/test/fixtures/partials-inline/build/comment-multiple-inline-partial-top-bot.html @@ -0,0 +1,11 @@ + + + +

layout-inline-partial-top content

+ +

Page Body

+ +

layout-inline-partial-bot content

+ + + diff --git a/test/fixtures/partials-inline/build/index.html b/test/fixtures/partials-inline/build/index.html index 2fe75cb..f6170c8 100644 --- a/test/fixtures/partials-inline/build/index.html +++ b/test/fixtures/partials-inline/build/index.html @@ -1,9 +1,11 @@ - + +

layout-inline-partial-top content

+

Page Body

-

layout-inline-partial content

+

layout-inline-partial-bot content

diff --git a/test/fixtures/partials-inline/build/inline-partial-bot-comment-top.html b/test/fixtures/partials-inline/build/inline-partial-bot-comment-top.html new file mode 100644 index 0000000..756b137 --- /dev/null +++ b/test/fixtures/partials-inline/build/inline-partial-bot-comment-top.html @@ -0,0 +1,9 @@ + + +

No layout-inline-partial-top default content

+

Page Body

+ +

layout-inline-partial-bot content - commented

+ + + diff --git a/test/fixtures/partials-inline/build/inline-partial-bot.html b/test/fixtures/partials-inline/build/inline-partial-bot.html new file mode 100644 index 0000000..7739539 --- /dev/null +++ b/test/fixtures/partials-inline/build/inline-partial-bot.html @@ -0,0 +1,9 @@ + + +

No layout-inline-partial-top default content

+

Page Body

+ +

layout-inline-partial-bot content

+ + + diff --git a/test/fixtures/partials-inline/build/inline-partial-top-bot-comment-top-bot.html b/test/fixtures/partials-inline/build/inline-partial-top-bot-comment-top-bot.html new file mode 100644 index 0000000..784499c --- /dev/null +++ b/test/fixtures/partials-inline/build/inline-partial-top-bot-comment-top-bot.html @@ -0,0 +1,7 @@ + + +

No layout-inline-partial-top default content

+

Page Body

+

No layout-inline-partial-bot default content

+ + diff --git a/test/fixtures/partials-inline/build/inline-partial-top-comment-bot.html b/test/fixtures/partials-inline/build/inline-partial-top-comment-bot.html new file mode 100644 index 0000000..9e3e041 --- /dev/null +++ b/test/fixtures/partials-inline/build/inline-partial-top-comment-bot.html @@ -0,0 +1,9 @@ + + + +

layout-inline-partial-top content

+ +

Page Body

+

No layout-inline-partial-bot default content

+ + diff --git a/test/fixtures/partials-inline/build/inline-partial-top.html b/test/fixtures/partials-inline/build/inline-partial-top.html new file mode 100644 index 0000000..9e3e041 --- /dev/null +++ b/test/fixtures/partials-inline/build/inline-partial-top.html @@ -0,0 +1,9 @@ + + + +

layout-inline-partial-top content

+ +

Page Body

+

No layout-inline-partial-bot default content

+ + diff --git a/test/fixtures/partials-inline/build/no-inline-partial.html b/test/fixtures/partials-inline/build/no-inline-partial.html index b06f555..784499c 100644 --- a/test/fixtures/partials-inline/build/no-inline-partial.html +++ b/test/fixtures/partials-inline/build/no-inline-partial.html @@ -1,6 +1,7 @@ +

No layout-inline-partial-top default content

Page Body

-

No layout-inline-partial default content

+

No layout-inline-partial-bot default content

diff --git a/test/fixtures/partials-inline/expected/comment-multiple-inline-partial-top-bot.html b/test/fixtures/partials-inline/expected/comment-multiple-inline-partial-top-bot.html new file mode 100644 index 0000000..f6170c8 --- /dev/null +++ b/test/fixtures/partials-inline/expected/comment-multiple-inline-partial-top-bot.html @@ -0,0 +1,11 @@ + + + +

layout-inline-partial-top content

+ +

Page Body

+ +

layout-inline-partial-bot content

+ + + diff --git a/test/fixtures/partials-inline/expected/index.html b/test/fixtures/partials-inline/expected/index.html index 2fe75cb..f6170c8 100644 --- a/test/fixtures/partials-inline/expected/index.html +++ b/test/fixtures/partials-inline/expected/index.html @@ -1,9 +1,11 @@ - + +

layout-inline-partial-top content

+

Page Body

-

layout-inline-partial content

+

layout-inline-partial-bot content

diff --git a/test/fixtures/partials-inline/expected/inline-partial-bot-comment-top.html b/test/fixtures/partials-inline/expected/inline-partial-bot-comment-top.html new file mode 100644 index 0000000..756b137 --- /dev/null +++ b/test/fixtures/partials-inline/expected/inline-partial-bot-comment-top.html @@ -0,0 +1,9 @@ + + +

No layout-inline-partial-top default content

+

Page Body

+ +

layout-inline-partial-bot content - commented

+ + + diff --git a/test/fixtures/partials-inline/expected/inline-partial-bot.html b/test/fixtures/partials-inline/expected/inline-partial-bot.html new file mode 100644 index 0000000..7739539 --- /dev/null +++ b/test/fixtures/partials-inline/expected/inline-partial-bot.html @@ -0,0 +1,9 @@ + + +

No layout-inline-partial-top default content

+

Page Body

+ +

layout-inline-partial-bot content

+ + + diff --git a/test/fixtures/partials-inline/expected/inline-partial-top-bot-comment-top-bot.html b/test/fixtures/partials-inline/expected/inline-partial-top-bot-comment-top-bot.html new file mode 100644 index 0000000..784499c --- /dev/null +++ b/test/fixtures/partials-inline/expected/inline-partial-top-bot-comment-top-bot.html @@ -0,0 +1,7 @@ + + +

No layout-inline-partial-top default content

+

Page Body

+

No layout-inline-partial-bot default content

+ + diff --git a/test/fixtures/partials-inline/expected/inline-partial-top-comment-bot.html b/test/fixtures/partials-inline/expected/inline-partial-top-comment-bot.html new file mode 100644 index 0000000..9e3e041 --- /dev/null +++ b/test/fixtures/partials-inline/expected/inline-partial-top-comment-bot.html @@ -0,0 +1,9 @@ + + + +

layout-inline-partial-top content

+ +

Page Body

+

No layout-inline-partial-bot default content

+ + diff --git a/test/fixtures/partials-inline/expected/inline-partial-top.html b/test/fixtures/partials-inline/expected/inline-partial-top.html new file mode 100644 index 0000000..9e3e041 --- /dev/null +++ b/test/fixtures/partials-inline/expected/inline-partial-top.html @@ -0,0 +1,9 @@ + + + +

layout-inline-partial-top content

+ +

Page Body

+

No layout-inline-partial-bot default content

+ + diff --git a/test/fixtures/partials-inline/expected/no-inline-partial.html b/test/fixtures/partials-inline/expected/no-inline-partial.html index b06f555..784499c 100644 --- a/test/fixtures/partials-inline/expected/no-inline-partial.html +++ b/test/fixtures/partials-inline/expected/no-inline-partial.html @@ -1,6 +1,7 @@ +

No layout-inline-partial-top default content

Page Body

-

No layout-inline-partial default content

+

No layout-inline-partial-bot default content

diff --git a/test/fixtures/partials-inline/layouts/default.html b/test/fixtures/partials-inline/layouts/default.html index bf900ae..033de70 100644 --- a/test/fixtures/partials-inline/layouts/default.html +++ b/test/fixtures/partials-inline/layouts/default.html @@ -1,8 +1,11 @@ + {{#> layout-inline-partial-top}} +

No layout-inline-partial-top default content

+ {{/layout-inline-partial-top}} {{> body}} - {{#> layout-inline-partial}} -

No layout-inline-partial default content

- {{/layout-inline-partial}} + {{#> layout-inline-partial-bot}} +

No layout-inline-partial-bot default content

+ {{/layout-inline-partial-bot}} diff --git a/test/fixtures/partials-inline/pages/comment-multiple-inline-partial-top-bot.html b/test/fixtures/partials-inline/pages/comment-multiple-inline-partial-top-bot.html new file mode 100644 index 0000000..9672564 --- /dev/null +++ b/test/fixtures/partials-inline/pages/comment-multiple-inline-partial-top-bot.html @@ -0,0 +1,23 @@ +{{!-- +{{#*inline "layout-inline-partial-bot"}} +

layout-inline-partial-bot content in comment

+{{/inline}} +{{#*inline "layout-inline-partial-top"}} +

layout-inline-partial-top content in comment

+{{/inline}} +--}} +{{#*inline "layout-inline-partial-bot"}} +

layout-inline-partial-bot content

+{{/inline}} +{{#*inline "layout-inline-partial-top"}} +

layout-inline-partial-top content

+{{/inline}} +

Page Body

+{{!-- +{{#*inline "layout-inline-partial-bot"}} +

layout-inline-partial-bot content in comment bottom page

+{{/inline}} +{{#*inline "layout-inline-partial-top"}} +

layout-inline-partial-top content in comment bottom page

+{{/inline}} +--}} \ No newline at end of file diff --git a/test/fixtures/partials-inline/pages/index.html b/test/fixtures/partials-inline/pages/index.html index 2093f1e..e9b6978 100644 --- a/test/fixtures/partials-inline/pages/index.html +++ b/test/fixtures/partials-inline/pages/index.html @@ -1,4 +1,7 @@ -{{#*inline "layout-inline-partial"}} -

layout-inline-partial content

+{{#*inline "layout-inline-partial-bot"}} +

layout-inline-partial-bot content

+{{/inline}} +{{#*inline "layout-inline-partial-top"}} +

layout-inline-partial-top content

{{/inline}}

Page Body

\ No newline at end of file diff --git a/test/fixtures/partials-inline/pages/inline-partial-bot-comment-top.html b/test/fixtures/partials-inline/pages/inline-partial-bot-comment-top.html new file mode 100644 index 0000000..dff4046 --- /dev/null +++ b/test/fixtures/partials-inline/pages/inline-partial-bot-comment-top.html @@ -0,0 +1,9 @@ +{{!-- +{{#*inline "layout-inline-partial-top"}} +

layout-inline-partial-top content

+{{/inline}} +--}} +{{#*inline "layout-inline-partial-bot"}} +

layout-inline-partial-bot content - commented

+{{/inline}} +

Page Body

\ No newline at end of file diff --git a/test/fixtures/partials-inline/pages/inline-partial-bot.html b/test/fixtures/partials-inline/pages/inline-partial-bot.html new file mode 100644 index 0000000..8fbc36a --- /dev/null +++ b/test/fixtures/partials-inline/pages/inline-partial-bot.html @@ -0,0 +1,4 @@ +{{#*inline "layout-inline-partial-bot"}} +

layout-inline-partial-bot content

+{{/inline}} +

Page Body

\ No newline at end of file diff --git a/test/fixtures/partials-inline/pages/inline-partial-top-bot-comment-top-bot.html b/test/fixtures/partials-inline/pages/inline-partial-top-bot-comment-top-bot.html new file mode 100644 index 0000000..46ae289 --- /dev/null +++ b/test/fixtures/partials-inline/pages/inline-partial-top-bot-comment-top-bot.html @@ -0,0 +1,9 @@ +{{!-- +{{#*inline "layout-inline-partial-bot"}} +

layout-inline-partial-bot content in comment

+{{/inline}} +{{#*inline "layout-inline-partial-top"}} +

layout-inline-partial-top content in comment

+{{/inline}} +--}} +

Page Body

\ No newline at end of file diff --git a/test/fixtures/partials-inline/pages/inline-partial-top-comment-bot.html b/test/fixtures/partials-inline/pages/inline-partial-top-comment-bot.html new file mode 100644 index 0000000..eca62c8 --- /dev/null +++ b/test/fixtures/partials-inline/pages/inline-partial-top-comment-bot.html @@ -0,0 +1,9 @@ +{{#*inline "layout-inline-partial-top"}} +

layout-inline-partial-top content

+{{/inline}} +{{!-- +{{#*inline "layout-inline-partial-bot"}} +

layout-inline-partial-bot content - commented

+{{/inline}} +--}} +

Page Body

\ No newline at end of file diff --git a/test/fixtures/partials-inline/pages/inline-partial-top.html b/test/fixtures/partials-inline/pages/inline-partial-top.html new file mode 100644 index 0000000..f428507 --- /dev/null +++ b/test/fixtures/partials-inline/pages/inline-partial-top.html @@ -0,0 +1,4 @@ +{{#*inline "layout-inline-partial-top"}} +

layout-inline-partial-top content

+{{/inline}} +

Page Body

\ No newline at end of file From b30e77a7bbe00d970392446ef47b0f90964c84b0 Mon Sep 17 00:00:00 2001 From: jaket-hub Date: Mon, 4 Nov 2024 16:52:52 +0000 Subject: [PATCH 3/4] combine regex for comment and inline partial into same match filter out comment and only use partial matches https://regex101.com/r/qPmiU8/2 https://www.rexegg.com/regex-best-trick.php#thetrick --- lib/render.js | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/lib/render.js b/lib/render.js index 99f9e67..385c6e6 100644 --- a/lib/render.js +++ b/lib/render.js @@ -48,27 +48,20 @@ function render(file, enc, cb) { inlineLayoutPartialList.forEach(partial => delete this.Handlebars.partials[partial]); //add to Handlebars.partials any inline partials starting with "layout-" if (page.body.indexOf('{{#*inline') !== -1 || page.body.indexOf('{{~#*inline') !== -1) { - var commentRegex = /{{!--(?[.\s\S]*?)--}}/gm; - // var inlinePartialRegex = /{{#\*inline[\s]+\"(?layout-[a-zA-Z_-]*)\"}}(?[.\s\S]*?){{\/inline}}/gm; - var inlinePartialRegex = /{{\~{0,1}#\*inline[\s]+(?[\"']{1})(?layout-[0-9a-zA-Z_-]*)\k\s*}}(?[.\s\S]*?){{\/inline\s*\~{0,1}}}[\s]*/gm; - // var inlinePartialMatch = page.body.match(inlinePartialRegex);//only gets match without group, can only get group when not global - later versions of javascript can use matchAll - + var inlinePartialAndCommentRegex = /{{!--(?[.\s\S]*?)--}}|(?{{#\*inline[\s]+\"(?layout-[0-9a-zA-Z_-]*)\"}}(?[.\s\S]*?){{\/inline}}[\s]*)/gm; var replaceWith = ''; var bodyText = page.body; var inlinePartialMatch; - // var inlinePartialMatchList = []; - while (inlinePartialMatch = inlinePartialRegex.exec(page.body.replace(commentRegex, ''))) {//this way can get all match and group - note checking body with removed handlebars comments - // inlinePartialMatchList.push(inlinePartialMatch); - + while (inlinePartialMatch = inlinePartialAndCommentRegex.exec(page.body)) { // Add inline partials from page.body starting with "layout-" - this.Handlebars.registerPartial( - inlinePartialMatch.groups.name, - this.Handlebars.compile(inlinePartialMatch.groups.body + '\n') - ); - // replaceWith = '{{!--removed ' + inlinePartialMatch.groups.name + '--}}'; - bodyText = bodyText.replace(inlinePartialMatch[0], replaceWith); + if (typeof inlinePartialMatch.groups.partial !== 'undefined') {//hasOwnProperty not working? + this.Handlebars.registerPartial( + inlinePartialMatch.groups.name, + this.Handlebars.compile(inlinePartialMatch.groups.body + '\n') + ); + bodyText = bodyText.replace(inlinePartialMatch.groups.partial, replaceWith); + } } - // bodyText = bodyText.replace(inlinePartialRegex, ''); page.body = bodyText;//amended body - removed inline partials starting with "layout-" //note: would not harm to use body with removed handlebars comments } From eb664ef0d46ba139abecedd42c2dba503413002e Mon Sep 17 00:00:00 2001 From: jaket-hub Date: Wed, 6 Nov 2024 20:26:49 +0000 Subject: [PATCH 4/4] add options inlinelayout - default value 'layout-' add options.inlinelayout to readme --- bin/panini.js | 2 ++ lib/helpMessage.js | 1 + lib/render.js | 10 +++++---- readme.md | 53 ++++++++++++++++++++++++++++++++++++++++++++++ test/test.js | 3 ++- 5 files changed, 64 insertions(+), 5 deletions(-) diff --git a/bin/panini.js b/bin/panini.js index 932a894..e2c707a 100755 --- a/bin/panini.js +++ b/bin/panini.js @@ -12,6 +12,7 @@ var options = { "root": String, "layouts": String, "partials": String, + "inlinelayout": String, "data": String, "helpers": String, "output": String, @@ -23,6 +24,7 @@ var shorthands = { "r": "--root", "l": "--layouts", "p": "--partials", + "i": "--inlinelayout", "d": "--data", "h": "--helpers", "o": "--output", diff --git a/lib/helpMessage.js b/lib/helpMessage.js index e00285b..8816573 100644 --- a/lib/helpMessage.js +++ b/lib/helpMessage.js @@ -7,6 +7,7 @@ module.exports = function() { ' --root (required) path to the root folder all pages live in\n' + ' --output (required) path to the folder compiled pages should get sent to\n' + ' --partials path to root folder for partials \n' + + ' --inlinelayout prefix of inline partial to look for \n' + ' --helpers path to folder for additional helpers \n' + ' --data path to folder for additional data \n' + '\n' + diff --git a/lib/render.js b/lib/render.js index 385c6e6..661c06b 100644 --- a/lib/render.js +++ b/lib/render.js @@ -38,17 +38,19 @@ function render(file, enc, cb) { } } - //remove partials starting with "layout-", assume added via inline to be used within layout.hbs - //possible panini.options to add in a layouts-partial param for "layout-" + // Determine which inline partial layout prefix to use + var inlineLayout = typeof page.attributes.inlinelayout !== "undefined" ? page.attributes.inlinelayout : typeof this.options.inlinelayout !== "undefined" ? this.options.inlinelayout : 'layout-'; + //remove Handlebars.partials starting with inlineLayout prefix var inlineLayoutPartialList = Object.keys(this.Handlebars.partials).filter( function (partial) { - return (partial.indexOf('layout-') === 0); + return (partial.indexOf(inlineLayout) === 0); } ); inlineLayoutPartialList.forEach(partial => delete this.Handlebars.partials[partial]); //add to Handlebars.partials any inline partials starting with "layout-" if (page.body.indexOf('{{#*inline') !== -1 || page.body.indexOf('{{~#*inline') !== -1) { - var inlinePartialAndCommentRegex = /{{!--(?[.\s\S]*?)--}}|(?{{#\*inline[\s]+\"(?layout-[0-9a-zA-Z_-]*)\"}}(?[.\s\S]*?){{\/inline}}[\s]*)/gm; + // var inlinePartialAndCommentRegex = /{{!--(?[.\s\S]*?)--}}|(?{{#\*inline[\s]+\"(?layout-[0-9a-zA-Z_-]*)\"}}(?[.\s\S]*?){{\/inline}}[\s]*)/gm; + var inlinePartialAndCommentRegex = new RegExp(String.raw`{{!--(?[.\s\S]*?)--}}|(?{{#\*inline[\s]+\"(?${inlineLayout}[0-9a-zA-Z_-]*)\"}}(?[.\s\S]*?){{\/inline}}[\s]*)`, "gm"); var replaceWith = ''; var bodyText = page.body; var inlinePartialMatch; diff --git a/readme.md b/readme.md index 83dd9b8..b173183 100644 --- a/readme.md +++ b/readme.md @@ -97,6 +97,59 @@ Path to a folder containing HTML partials. Partial files can have the extension {{> header}} ``` +### `inlinelayout` + +**Type:** `String` + +Inline partial name prefix, if not set - the default prefix will be `layout-`. Page Inline Partials to be used within layout pages. + +```html +{{#*inline "layout-inline-partial-bot"}} + +{{/inline}} +{{#*inline "layout-inline-partial-top"}} + +{{/inline}} + +``` + +The page inline partials with `inlinelayout` prefix can be used within the layouts. If there is not a corresponding inline partial on the page, then the default content will be displayed. Note: the default content can be made empty. + +```html +{{#> layout-inline-partial-top}} + +{{/layout-inline-partial-top}} +{{> body}} +{{#> layout-inline-partial-bot}} + +{{/layout-inline-partial-bot}} +``` + +To use an `inlinelayout` other than the default `layout-` prefix or `panini.options.inlinelayout` prefix on a specific page, override it in the Front Matter on that page. + +```html +--- +inlinelayout: alt-inlinelayout- +--- +{{#*inline "alt-inlinelayout-inline-partial-bot"}} + +{{/inline}} +{{#*inline "alt-inlinelayout-inline-partial-top"}} + +{{/inline}} + +``` +layout +```html +{{#> alt-inlinelayout-inline-partial-top}} + +{{/alt-inlinelayout-inline-partial-top}} +{{> body}} +{{#> alt-inlinelayout-inline-partial-bot}} + +{{/alt-inlinelayout-inline-partial-bot}} +``` + ### `helpers` **Type:** `String` diff --git a/test/test.js b/test/test.js index 59866d6..a375ecd 100644 --- a/test/test.js +++ b/test/test.js @@ -81,7 +81,8 @@ describe('Panini', () => { it('builds a page with inline partials used within layout', done => { var p = new Panini({ root: FIXTURES + 'partials-inline/pages/', - layouts: FIXTURES + 'partials-inline/layouts/' + layouts: FIXTURES + 'partials-inline/layouts/', + inlinelayout: 'layout-' }); p.refresh();