From 536c6a5aa88db870121f64621a9d829dd65d5918 Mon Sep 17 00:00:00 2001 From: HerrHase Date: Sun, 15 Jan 2023 22:13:37 +0100 Subject: [PATCH] adding #12 --- example/site/post2.md | 2 +- package-lock.json | 23 +++++++- package.json | 1 + src/queries/pages.js | 124 +++++------------------------------------- 4 files changed, 37 insertions(+), 113 deletions(-) diff --git a/example/site/post2.md b/example/site/post2.md index 562c2b8..ad342e2 100644 --- a/example/site/post2.md +++ b/example/site/post2.md @@ -6,7 +6,7 @@ excerpt: "ddd" meta: description: "DSA yes plz hot chicken green juice" tags: - - f + - xxx --- ## Normcore cold-pressed ramps DSA diff --git a/package-lock.json b/package-lock.json index 35360ea..3fe6644 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,13 +1,14 @@ { "name": "@helpers/siteomat-webpack-plugin", - "version": "0.3.0", + "version": "0.4.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@helpers/siteomat-webpack-plugin", - "version": "0.3.0", + "version": "0.4.0", "dependencies": { + "@helpers/siteomat-query": "^0.1.0", "assign-deep": "^1.0.1", "crypto": "^1.0.1", "dayjs": "^1.11.6", @@ -28,6 +29,15 @@ "mocha": "^10.2.0" } }, + "node_modules/@helpers/siteomat-query": { + "version": "0.1.0", + "resolved": "https://gitea.node001.net/api/packages/HerrHase/npm/%40helpers%2Fsiteomat-query/-/0.1.0/siteomat-query-0.1.0.tgz", + "integrity": "sha512-5I5XrfZKCJ+RQ4phO/oI+0Wjb54jwRX3npbmDtZZ7sVHkLfYyBwKR+Zs3//MAKBOOUC44jeFp6RO3Si+IroZvg==", + "dependencies": { + "assign-deep": "^1.0.1", + "lodash.orderby": "^4.6.0" + } + }, "node_modules/a-sync-waterfall": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/a-sync-waterfall/-/a-sync-waterfall-1.0.1.tgz", @@ -1673,6 +1683,15 @@ } }, "dependencies": { + "@helpers/siteomat-query": { + "version": "0.1.0", + "resolved": "https://gitea.node001.net/api/packages/HerrHase/npm/%40helpers%2Fsiteomat-query/-/0.1.0/siteomat-query-0.1.0.tgz", + "integrity": "sha512-5I5XrfZKCJ+RQ4phO/oI+0Wjb54jwRX3npbmDtZZ7sVHkLfYyBwKR+Zs3//MAKBOOUC44jeFp6RO3Si+IroZvg==", + "requires": { + "assign-deep": "^1.0.1", + "lodash.orderby": "^4.6.0" + } + }, "a-sync-waterfall": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/a-sync-waterfall/-/a-sync-waterfall-1.0.1.tgz", diff --git a/package.json b/package.json index d8516f7..e7272a6 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ }, "description": "A Webpack Plugin for generating a Website as Html-Files from a Markdown File Structure", "dependencies": { + "@helpers/siteomat-query": "^0.1.0", "assign-deep": "^1.0.1", "crypto": "^1.0.1", "dayjs": "^1.11.6", diff --git a/src/queries/pages.js b/src/queries/pages.js index c5cff98..f674ca6 100644 --- a/src/queries/pages.js +++ b/src/queries/pages.js @@ -1,6 +1,8 @@ const fs = require('fs') const path = require('path') -const orderBy = require('lodash.orderby') +const { + Filter, orderBy, reduce +} = require('@helpers/siteomat-query') const PageFactory = require('./../factories/page.js') const BlocksQuery = require('./../queries/blocks.js') @@ -51,116 +53,23 @@ class Pages { this._count = 0 options = Object.assign({}, this._options, options) + + if (options.filter) { + this._filter = new Filter(options.filter) + } + this._findFiles(this._dirPath, options) // if (options.orderBy && options.orderBy.length > 0) { - this._orderBy(options) - } - - 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)) { - - // equal - if (value['_eq'] && result[key] !== value['_eq']) { - isValid = false - } - - // not equal - if (value['_neq'] && result[key] === value['_neq']) { - isValid = false - } - - // in - if (value['_in'] && Array.isArray(value['_in'])) { - - // if result no exists - if (!result[key]) { - isValid = false - } - - if (Array.isArray(result[key])) { - - let found = false - - result[key].forEach((v, index) => { - if (value['_in'].indexOf(v) !== -1) { - found = true - } - }) - - if (!found) { - isValid = false - } - - } else { - if (value['_in'].indexOf(result[key]) === -1) { - isValid = false - } - } - } - - if (value['_lt'] && result[key] < value['_lt']) { - isValid = false - } - - if (value['_lte'] && result[key] <= value['_lte']) { - isValid = false - } - - if (value['_gt'] && result[key] > value['_gt']) { - isValid = false - } - - if (value['_gte'] && result[key] >= value['_gte']) { - isValid = false - } - - if (value['_null'] && result[key]) { - isValid = false - } - - if (value['_nnull'] && !result[key]) { - isValid = false - } + this.results = orderBy(options.orderBy, this._results) } - return isValid - } + if (options.limit || options.offset) { + this._results = reduce(options, this._results) + } - /** - * - * - * @param {array} options - * - */ - _orderBy(options) { - options.orderBy.forEach((key, index) => { - - let direction = 'asc' - - if (key.charAt(0) === '-') { - key.slice(0, 1) - direction = 'desc' - } - - this._results = orderBy(this._results, key, direction) - }) + return this._results } /** @@ -225,12 +134,7 @@ class Pages { 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) { + if (this._filter && !this._filter.validate(page.get())) { return; }