diff --git a/README.md b/README.md index 7dc3e2c..963eb78 100644 --- a/README.md +++ b/README.md @@ -143,7 +143,7 @@ can be used like this, ## Queries -Queries can be used in Templates to get +Queries can be used in Templates to get Pages. ### Pages @@ -160,6 +160,24 @@ or with options, | parent | {String} | / | Directory for start query | | deep | {Integer} | -1 | Deep of Recursive | | orderBy | {Array} | null | Name of field sorting, a "-" in front of the. Nested fields are also possible. | +| limit | {Integer} | null | Limit results | +| filter | {Object} | null | Filtering results by Fields in yaml | + +#### Filter + +Basic Usage: + +``` +{ + : { + : + } +} +``` + +| Name | Description | +|-------------|-------------| +| _eq | Equal Value | ## Sitemap diff --git a/example/site/blog.md b/example/site/blog.md new file mode 100644 index 0000000..cc2c927 --- /dev/null +++ b/example/site/blog.md @@ -0,0 +1,6 @@ +--- +title: "health goth DIY tattooed" +view: "blog.njk" +meta: + description: "La" +--- \ No newline at end of file diff --git a/example/site/post.md b/example/site/post.md new file mode 100644 index 0000000..82b706f --- /dev/null +++ b/example/site/post.md @@ -0,0 +1,9 @@ +--- +title: "health goth DIY tattooed" +view: "post.njk" +meta: + description: "DSA yes plz hot chicken green juice" +--- +## Normcore cold-pressed ramps DSA + +Normcore cold-pressed ramps DSA yes plz hot chicken green juice succulents leggings messenger bag truffaut iceland pabst ethical godard. Semiotics air plant marfa, drinking vinegar authentic iceland pug fit cloud bread cronut kickstarter glossier crucifix tumeric. Chicharrones polaroid flexitarian, seitan lumbersexual viral fam master cleanse four dollar toast scenester. Succulents poutine vegan keffiyeh meh, health goth DIY tattooed. Praxis roof party celiac chartreuse banjo butcher you probably haven't heard of them schlitz beard. Ethical tattooed kinfolk, cliche vegan messenger bag mukbang dreamcatcher cloud bread farm-to-table gatekeep trust fund. \ No newline at end of file diff --git a/example/site/post2.md b/example/site/post2.md new file mode 100644 index 0000000..82b706f --- /dev/null +++ b/example/site/post2.md @@ -0,0 +1,9 @@ +--- +title: "health goth DIY tattooed" +view: "post.njk" +meta: + description: "DSA yes plz hot chicken green juice" +--- +## Normcore cold-pressed ramps DSA + +Normcore cold-pressed ramps DSA yes plz hot chicken green juice succulents leggings messenger bag truffaut iceland pabst ethical godard. Semiotics air plant marfa, drinking vinegar authentic iceland pug fit cloud bread cronut kickstarter glossier crucifix tumeric. Chicharrones polaroid flexitarian, seitan lumbersexual viral fam master cleanse four dollar toast scenester. Succulents poutine vegan keffiyeh meh, health goth DIY tattooed. Praxis roof party celiac chartreuse banjo butcher you probably haven't heard of them schlitz beard. Ethical tattooed kinfolk, cliche vegan messenger bag mukbang dreamcatcher cloud bread farm-to-table gatekeep trust fund. \ No newline at end of file diff --git a/example/views/blog.njk b/example/views/blog.njk index 3363219..9db9c0b 100644 --- a/example/views/blog.njk +++ b/example/views/blog.njk @@ -1 +1,15 @@ -{% extends('layout.njk') %} \ No newline at end of file +{% extends('layout.njk') %} + +{% block main %} + +{% set posts = pageQuery.find({ orderBy: [ '-date_published' ], limit: 1, filter: { view: { _eq: 'post.njk' } } }) %} + +{% for post in posts %} + +

+ {{ post.title }} +

+
+{% endfor %} + +{% endblock %} \ No newline at end of file diff --git a/example/views/post.njk b/example/views/post.njk new file mode 100644 index 0000000..e3728e2 --- /dev/null +++ b/example/views/post.njk @@ -0,0 +1,5 @@ +{% extends('layout.njk') %} + +{% block main %} +{{ page.content | safe }} +{% endblock %} \ No newline at end of file diff --git a/package.json b/package.json index b728f08..bd976b8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@helpers/siteomat-webpack-plugin", - "version": "0.1.0", + "version": "0.2.0", "build": "webpack", "author": "Björn Hase ", "main": "index.js", diff --git a/src/queries/pages.js b/src/queries/pages.js index 5af8098..dcf4ae4 100644 --- a/src/queries/pages.js +++ b/src/queries/pages.js @@ -48,6 +48,7 @@ class Pages { */ find(options = {}) { this._results = [] + this._count = 0 options = Object.assign({}, this._options, options) this._findFiles(this._dirPath, options) @@ -60,6 +61,28 @@ class Pages { return this._results } + /** + * filtering single results from query + * if filter is set check result + * + * @param {array} result + * @param {array} options + * @return {boolean} + * + */ + _filter(result, options) { + + let isValid = true + + for (const [key, value] of Object.entries(options.filter)) { + if (value['_eq'] && result[key] !== value['_eq']) { + isValid = false + } + } + + return isValid + } + /** * * @@ -140,6 +163,17 @@ class Pages { // create page object and add to page const page = new PageFactory(file, options.parent, content, blocks) + + // check for filters and skip + if (options.filter && !this._filter(page.get(), options)) { + return; + } + + // check for filters and skip + if (options.limit && options.limit <= this._results.length) { + return; + } + this._results.push(page.get()) }) } diff --git a/test.js b/test.js index 5b0c5ec..2cb7df6 100644 --- a/test.js +++ b/test.js @@ -1,7 +1,8 @@ const Siteomat = require('./src/siteomat.js') const siteomat = new Siteomat('./example/site', './example/views', { - 'destination': './public' + 'destination': './public', + 'minifyHtml': false }) siteomat.run() \ No newline at end of file