Skip to content

Commit a6d96b0

Browse files
authored
metalsmith-collections-related: add filter options (#244)
1 parent ad24e04 commit a6d96b0

11 files changed

Lines changed: 47 additions & 8 deletions

File tree

package-lock.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/metalsmith-collections-related/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## v2.1.0 / 2025-11-27
4+
5+
- Added the `filter` function option.
6+
- Added filtering of the `related_ignore` file metadata key.
7+
38
## v2.0.0 / 2023-11-06
49

510
- Converted to TypeScript, including type definitions.

packages/metalsmith-collections-related/README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,20 @@ which can be used with templating engines, such as with [`handlebars`](https://w
8080
{{/each}}
8181
```
8282

83-
## Options
83+
## Plugin options
8484

8585
### `pattern` (optional)
8686

8787
Type: `string` Default: `"**/*"`
8888

8989
A [`micromatch`](https://www.npmjs.com/package/micromatch) glob pattern to find input files.
9090

91+
### `filter` (optional)
92+
93+
Type: `(basePage: Metalsmith.File, relatedPage: Metalsmith.File, idx: number) => boolean`
94+
95+
A function to filter out pages from a "base" page's list of related pages.
96+
9197
### `maxRelated` (optional)
9298

9399
Type: `number` Default: `3`
@@ -131,6 +137,23 @@ Type: `object` Default:
131137

132138
An object of [`sanitize-html`](https://www.npmjs.com/package/sanitize-html) options.
133139

140+
## Metadata options
141+
142+
### `related_ignore` (optional)
143+
144+
Type: `boolean` Default: `false`
145+
146+
If a file has a truthy `related_ignore` metadata value, it will be excluded from other files' related lists. These files will still have a related list calculated for them.
147+
148+
Example:
149+
150+
```markdown
151+
---
152+
related_ignore: true
153+
---
154+
This Markdown file won't appear in any other file's "related" list.
155+
```
156+
134157
## Changelog
135158

136159
[Changelog](./CHANGELOG.md)

packages/metalsmith-collections-related/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const { TfIdf } = natural;
88

99
export interface Options {
1010
pattern?: string;
11+
filter?: (basePage: Metalsmith.File, relatedPage: Metalsmith.File, idx: number) => boolean;
1112
maxRelated?: number;
1213
natural?: {
1314
minTfIdf?: number;
@@ -117,6 +118,12 @@ export default (options: Options = {}): Metalsmith.Plugin => {
117118
// Sort by filename ascending second
118119
return a.filename > b.filename ? 1 : -1;
119120
})
121+
.filter((related) => !files[related.filename].related_ignore)
122+
.filter((related, idx) =>
123+
options.filter !== undefined
124+
? options.filter(files[filename], files[related.filename], idx)
125+
: true,
126+
)
120127
.slice(0, defaultedOptions.maxRelated)
121128
.map((related) => files[related.filename]);
122129

packages/metalsmith-collections-related/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "metalsmith-collections-related",
3-
"version": "2.0.16",
3+
"version": "2.1.0",
44
"description": "A Metalsmith plugin to find related files within collections.",
55
"keywords": [
66
"metalsmith",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
A coppersmith, also known as a brazier, is a person who makes artifacts from copper and brass. Brass is an alloy of copper and zinc. The term redsmith is used for a tinsmith that uses tinsmithing tools and techniques to make copper items.
22

3-
/wikipedia/blacksmith.md
43
/wikipedia/goldsmith.md
54
/wikipedia/metalsmith.md
5+
/wikipedia/silversmith.md

packages/metalsmith-collections-related/test/fixtures/basic/expected/wikipedia/goldsmith.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ Goldsmiths must be skilled in forming metal through filing, soldering, sawing, f
55
At least in Europe, goldsmiths increasingly performed many of the functions now regarded as part of banking, especially providing custody of valuable items and currency exchange, though they were usually restrained from lending at interest, which was regarded as usury.
66

77
/wikipedia/metalsmith.md
8-
/wikipedia/blacksmith.md
98
/wikipedia/tinsmith.md
9+
/wikipedia/coppersmith.md

packages/metalsmith-collections-related/test/fixtures/basic/expected/wikipedia/metalsmith.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ The prevalence of metalworking in the culture of recent centuries has led Smith
44

55
/wikipedia/goldsmith.md
66
/wikipedia/tinsmith.md
7-
/wikipedia/blacksmith.md
7+
/wikipedia/coppersmith.md
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
A silversmith is a metalworker who crafts objects from silver. The terms silversmith and goldsmith are not exactly synonyms as the techniques, training, history, and guilds are or were largely the same but the end product may vary greatly as may the scale of objects created.
22

3-
/wikipedia/blacksmith.md
43
/wikipedia/goldsmith.md
54
/wikipedia/coppersmith.md
5+
/wikipedia/metalsmith.md

packages/metalsmith-collections-related/test/fixtures/basic/expected/wikipedia/tinsmith.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ A tinsmith, sometimes known as a tinner, tinker, tinman, or tinplate worker is a
22

33
Unlike blacksmiths (who work mostly with hot metals), tinsmiths do the majority of their work on cold metal (although they might use a hearth to heat and help shape their raw materials). Tinsmiths fabricate items such as water pitchers, forks, spoons, and candle holders.
44

5-
/wikipedia/blacksmith.md
65
/wikipedia/coppersmith.md
76
/wikipedia/goldsmith.md
7+
/wikipedia/metalsmith.md

0 commit comments

Comments
 (0)