Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix related categorizeBy #27

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ISC License

Copyright 2019-2024 Bryan Robinson and other contributors.

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
29 changes: 21 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ This plugin is a series of shortcodes and filters that aim to help you write and

Available on [npm](https://www.npmjs.com/package/eleventy-plugin-blog-tools).

```
```sh
npm install eleventy-plugin-blog-tools --save
```

Open up your Eleventy config file (probably `.eleventy.js`) and add the plugin:

```
```js
const blogTools = require("eleventy-plugin-blog-tools");
module.exports = function(eleventyConfig) {
eleventyConfig.addPlugin(blogTools);
Expand All @@ -31,7 +31,7 @@ This custom tag creates an excerpt from a page with front matter and/or standard
Main Usage:

```html
{% excerpt post %}
{% excerpt post %}
```

Where `post` is an instance of a markdown file being pulled in via an 11ty template engine.
Expand All @@ -46,22 +46,22 @@ The Excerpt is built from one of three options:

The YouTube shortcode takes a YouTube video ID and creates the markup for a fluidly-responsive YouTube embed.

```
```html
{% youtube "idstring" %}
```
### Vimeo

The Vimeo shortcode takes a Vimeo video ID and creates the markup for a fluidly-responsive Vimeo embed.

```
```html
{% vimeo "idstring" %}
```


### CodePen

The CodePen shortcode takes multiple values to customize your embed.
```
```html
{% codepen "URL", "codepen tabs string", "unitlessHeight", "theme ID" %}

{% codepen "https://codepen.io/url/path" %}
Expand All @@ -85,12 +85,25 @@ The related filter will pull items from a list based on parameters passed to the

The basic usage is to filter a collection based on an array of items and a threshold.

Syntax: `{{ collections.posts | related(<sort-field-key>, <sort-field-data>, <threshold-integer Defaults to 1>, <URL-to-Exclude-optional>)}}`
Syntax: `{{ collections.posts | related(<sort-field-key>, <sort-field-data>, <threshold-integer Defaults to 1>, <fileSlug-to-Exclude-optional>)}}`

The threshold integer is meant to force a number of array items in common. Defaults to 1.

```
Nunjucks:
```html
{% for post in collections.posts | related("sortField", sortField, 1) %}
{{ post.data.title }}
{% endfor %}
```

Liquid:
```html
{% assign filtered = related: "sortField", sortField, 1, page.fileSlug %}
{% for post in collections.posts %}
{{ post.data.title }}
{% endfor %}
```

# License

[ISC](LICENSE) (c) 2019-2024 Bryan Robinson and other contributors.
20 changes: 11 additions & 9 deletions src/related.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
module.exports = function(items, categorizeBy, categoryArray, threshold = 1, excludedSlug = "") {
let posts = Array.from(items).filter(item => {
return item.fileSlug != excludedSlug // removes "excluded item" aka the current item
});
module.exports = function(items, categorizeBy, categoryArray = [], threshold = 1, excludedSlug = "") {
let posts = Array.from(items).filter(item => {
return item.fileSlug != excludedSlug // removes "excluded item" aka the current item
});

let filteredPosts = posts.filter(item => {
let matchedCount = 0; // Counter to match our threshold
let filteredPosts = posts.filter(item => {
let matchedCount = 0; // Counter to match our threshold
if (categorizeBy in item.data) {
item.data[categorizeBy].forEach(cat => {
if (categoryArray.includes(cat)) {
return matchedCount++
}
});
return (matchedCount >= threshold);
})
return filteredPosts
}
return (matchedCount >= threshold);
})
return filteredPosts
}