From cf2440575b8dd7c1918603402927365ab11b39b9 Mon Sep 17 00:00:00 2001 From: straker Date: Wed, 31 Jul 2019 22:19:26 -0600 Subject: [PATCH 1/3] feat: add option to make every section its own page --- examples/basic/index.js | 6 +++++- examples/multiple-pages/index.js | 8 +++++++- examples/multiple-pages/styles.css | 2 +- index.js | 25 +++++++++++++++++++++---- template/template.hbs | 17 +++++++++++++++++ 5 files changed, 51 insertions(+), 7 deletions(-) diff --git a/examples/basic/index.js b/examples/basic/index.js index 15d4acf..5ed36ba 100644 --- a/examples/basic/index.js +++ b/examples/basic/index.js @@ -1,3 +1,7 @@ var livingcss = require('../../index'); -livingcss('buttons.css'); \ No newline at end of file +livingcss('buttons.css', { + preprocess: function(context) { + console.log(JSON.stringify(context, null, 2)); + } +}); \ No newline at end of file diff --git a/examples/multiple-pages/index.js b/examples/multiple-pages/index.js index c00442a..b001cfb 100644 --- a/examples/multiple-pages/index.js +++ b/examples/multiple-pages/index.js @@ -1,3 +1,9 @@ var livingcss = require('../../index'); -livingcss('styles.css'); \ No newline at end of file +livingcss('styles.css', { + allSectionPages: true, + preprocess: function(context) { + console.log('\n\n'); + console.log(JSON.stringify(context.singleSection, null, 2)); + } +}); \ No newline at end of file diff --git a/examples/multiple-pages/styles.css b/examples/multiple-pages/styles.css index f848b47..5c7c544 100644 --- a/examples/multiple-pages/styles.css +++ b/examples/multiple-pages/styles.css @@ -78,7 +78,7 @@ body { * A primary button * * @section - * @sectionof Buttons + * @sectionof Buttons.Buttons * * @example * diff --git a/index.js b/index.js index dc0d1d5..49ffd6d 100644 --- a/index.js +++ b/index.js @@ -84,6 +84,7 @@ function livingcss(source, dest, options) { options.tags = options.tags || []; options.minify = (typeof options.minify === 'undefined' ? false : options.minify); options.loadcss = (typeof options.loadcss === 'undefined' ? true : options.loadcss); + options.allSectionPages = (typeof options.allSectionPages === 'undefined' ? false : options.allSectionPages); // add custom tags for (var tag in options.tags) { @@ -158,10 +159,26 @@ function livingcss(source, dest, options) { pageContext.navbar[index].selected = true; } - // values[0] = handlebars template - promises.push( - generate(path.join(dest, page.id + '.html'), values[0], pageContext, options) - ); + if (options.allSectionPages) { + pageContext.allSectionPages = true; + + page.sections.forEach(function(section) { + // deep copy context for each page + pageContext = JSON.parse(JSON.stringify(pageContext)); + pageContext.singleSection = section; + + // values[0] = handlebars template + promises.push( + generate(path.join(dest, section.id + '.html'), values[0], pageContext, options) + ); + }); + } + else { + // values[0] = handlebars template + promises.push( + generate(path.join(dest, page.id + '.html'), values[0], pageContext, options) + ); + } }); // wait until all promises have returned (either rejected or resolved) before diff --git a/template/template.hbs b/template/template.hbs index 9e9c72d..138a8ad 100644 --- a/template/template.hbs +++ b/template/template.hbs @@ -48,6 +48,18 @@
+ {{#if singleSection}} + {{#with singleSection}} +
+

{{name}}

+ {{~> section}} + + {{#children}} + {{~> childSection}} + {{/children}} +
+ {{/with}} + {{else}} {{#each sections}}

{{name}}

@@ -58,13 +70,18 @@ {{/children}}
{{/each}} + {{/if}}
From 35a795a1b395941ffd2599affaa5a0cbac773c43 Mon Sep 17 00:00:00 2001 From: straker Date: Sat, 23 Nov 2019 20:47:08 -0700 Subject: [PATCH 2/3] finish --- examples/multiple-pages/index.js | 8 +------ index.js | 37 +++++++++++++++++++++++++++++--- template/partials/TOC.hbs | 10 +++++++++ template/partials/childTOC.hbs | 10 +++++++++ template/template.hbs | 16 +++++++++----- 5 files changed, 66 insertions(+), 15 deletions(-) create mode 100644 template/partials/TOC.hbs create mode 100644 template/partials/childTOC.hbs diff --git a/examples/multiple-pages/index.js b/examples/multiple-pages/index.js index b001cfb..c00442a 100644 --- a/examples/multiple-pages/index.js +++ b/examples/multiple-pages/index.js @@ -1,9 +1,3 @@ var livingcss = require('../../index'); -livingcss('styles.css', { - allSectionPages: true, - preprocess: function(context) { - console.log('\n\n'); - console.log(JSON.stringify(context.singleSection, null, 2)); - } -}); \ No newline at end of file +livingcss('styles.css'); \ No newline at end of file diff --git a/index.js b/index.js index 49ffd6d..6e34deb 100644 --- a/index.js +++ b/index.js @@ -159,19 +159,50 @@ function livingcss(source, dest, options) { pageContext.navbar[index].selected = true; } + // generate a single page for each top level section of the @page if (options.allSectionPages) { pageContext.allSectionPages = true; + // reset urls to correct path + if (context.navbar) { + pageContext.navbar = context.pages.map(function(page) { + return { + name: page.name, + url: path.join('/', page.id, 'index.html') + }; + }); + pageContext.navbar[index].selected = true; + } + page.sections.forEach(function(section) { // deep copy context for each page - pageContext = JSON.parse(JSON.stringify(pageContext)); - pageContext.singleSection = section; + var singleContext = JSON.parse(JSON.stringify(pageContext)); + singleContext.singleSection = section; + + // add selected for side nav + singleContext.sections.forEach(function(sec) { + if (section.id === sec.id) { + sec.selected = true; + } + }) // values[0] = handlebars template promises.push( - generate(path.join(dest, section.id + '.html'), values[0], pageContext, options) + generate(path.join(dest, page.id, section.id + '.html'), values[0], singleContext, options) ); }); + + // generate a TOC for the top level page + singleContext = JSON.parse(JSON.stringify(pageContext)); + singleContext.TOC = true; + + // add urls for TOC list + singleContext.sections.forEach(function(sec) { + sec.url = path.join(page.id, sec.id + '.html'); + }); + promises.push( + generate(path.join(dest, page.id, 'index.html'), values[0], singleContext, options) + ); } else { // values[0] = handlebars template diff --git a/template/partials/TOC.hbs b/template/partials/TOC.hbs new file mode 100644 index 0000000..fe9b790 --- /dev/null +++ b/template/partials/TOC.hbs @@ -0,0 +1,10 @@ +
  • + {{name}} + {{#if children}} +
      + {{#children}} + {{~> childTOC}} + {{/children}} +
    + {{/if}} +
  • \ No newline at end of file diff --git a/template/partials/childTOC.hbs b/template/partials/childTOC.hbs new file mode 100644 index 0000000..1001e64 --- /dev/null +++ b/template/partials/childTOC.hbs @@ -0,0 +1,10 @@ +
  • +
    {{name}}
    + {{#if children}} +
      + {{#children}} + {{~> childTOC}} + {{/children}} +
    + {{/if}} +
  • \ No newline at end of file diff --git a/template/template.hbs b/template/template.hbs index 138a8ad..a9350fa 100644 --- a/template/template.hbs +++ b/template/template.hbs @@ -48,9 +48,16 @@
    - {{#if singleSection}} - {{#with singleSection}}
    + {{#if TOC}} +

    Table of Contents

    +
      + {{#each sections}} + {{~> TOC}} + {{/each}} +
    + {{else if singleSection}} + {{#with singleSection}}

    {{name}}

    {{~> section}} @@ -61,16 +68,15 @@ {{/with}} {{else}} {{#each sections}} -

    {{name}}

    {{~> section}} {{#children}} {{~> childSection}} {{/children}} -
    {{/each}} {{/if}} +
    @@ -78,7 +84,7 @@
    {{#each sections}} {{#if ../allSectionPages}} - {{name}} + {{name}} {{else}} {{name}} {{/if}} From 97764a8cac7ddee1fc177501e8634a51a61e3ea9 Mon Sep 17 00:00:00 2001 From: straker Date: Sat, 23 Nov 2019 20:48:33 -0700 Subject: [PATCH 3/3] revert examples --- examples/basic/index.js | 6 +----- examples/multiple-pages/styles.css | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/examples/basic/index.js b/examples/basic/index.js index 5ed36ba..15d4acf 100644 --- a/examples/basic/index.js +++ b/examples/basic/index.js @@ -1,7 +1,3 @@ var livingcss = require('../../index'); -livingcss('buttons.css', { - preprocess: function(context) { - console.log(JSON.stringify(context, null, 2)); - } -}); \ No newline at end of file +livingcss('buttons.css'); \ No newline at end of file diff --git a/examples/multiple-pages/styles.css b/examples/multiple-pages/styles.css index 5c7c544..f848b47 100644 --- a/examples/multiple-pages/styles.css +++ b/examples/multiple-pages/styles.css @@ -78,7 +78,7 @@ body { * A primary button * * @section - * @sectionof Buttons.Buttons + * @sectionof Buttons * * @example *