Skip to content

Commit

Permalink
Adds Typescript types support
Browse files Browse the repository at this point in the history
  • Loading branch information
webketje committed Sep 26, 2022
1 parent 61430d4 commit 9f9b36c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
55 changes: 55 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import Metalsmith from "metalsmith";

export default initializeCollections;
export type CollectionConfig = {
/**
* - One or more glob patterns to match files to a collection
*/
pattern: string | string[];
/**
* - A key to sort by (e.g. `date`,`title`, ..) or a custom sort function
*/
sortBy: string | ((a: any, b: any) => 0 | 1 | -1);
/**
* - Limit the amount of items in a collection to `limit`
*/
limit: number;
/**
* - Adds `next` and `previous` keys to file metadata of matched files
*/
refer: boolean;
/**
* - Whether to invert the sorting function results (asc/descending)
*/
reverse: boolean;
/**
* - A function that gets a `Metalsmith.File` as first argument and returns `true` for every file to include in the collection
*/
filterBy: Function;
/**
* - An object with metadata to attach to the collection, or a `json`/`yaml`filepath string to load data from (relative to `Metalsmith.directory`)
*/
metadata: any | string;
};
/**
* Add `collections` of files to the global metadata as a sorted array.
* @example
* metalsmith.use(collections({
* posts: 'posts/*.md',
* portfolio: {
* pattern: 'portfolio/*.md',
* metadata: { title: 'My portfolio' },
* sortBy: 'order'
* }
* }))
*
* @param {Object.<string,CollectionConfig|string>} options
*/
declare function initializeCollections(options: {
[x: string]: CollectionConfig | string;
}): Metalsmith.Plugin;
declare namespace initializeCollections {
export { defaultOptions as defaults };
}

declare const defaultOptions: CollectionConfig;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"main": "lib/index.cjs",
"module": "lib/index.js",
"type": "module",
"types": "./lib/index.d.ts",
"exports": {
"import": "./lib/index.js",
"require": "./lib/index.cjs"
Expand Down Expand Up @@ -60,7 +61,7 @@
"lint:check": "eslint --fix-dry-run .",
"dev": "nodemon --exec 'npm test'",
"release": "release-it .",
"build": "microbundle --target node --no-sourcemap -f cjs,esm",
"build": "microbundle --target node --no-sourcemap -f cjs,esm --strict --generateTypes=false",
"pretest": "npm run build",
"test": "nyc mocha"
},
Expand Down
12 changes: 10 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,16 @@ function normalizeOptions(options) {
}

/**
* Metalsmith plugin that adds `collections` of files to the global
* metadata as a sorted array.
* Add `collections` of files to the global metadata as a sorted array.
* @example
* metalsmith.use(collections({
* posts: 'posts/*.md',
* portfolio: {
* pattern: 'portfolio/*.md',
* metadata: { title: 'My portfolio' },
* sortBy: 'order'
* }
* }))
*
* @param {Object.<string,CollectionConfig|string>} options
* @return {import('metalsmith').Plugin}
Expand Down
2 changes: 1 addition & 1 deletion test/index.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-env node, mocha */
const assert = require('assert')
const { it, describe } = require('mocha')
const Metalsmith = require('metalsmith')

/* eslint-disable-next-line node/no-missing-require */
Expand Down

0 comments on commit 9f9b36c

Please sign in to comment.