From 9f9b36ca569153ad84be89facdc6a2630e86a6af Mon Sep 17 00:00:00 2001 From: Kevin Van Lierde Date: Mon, 26 Sep 2022 23:13:21 +0200 Subject: [PATCH] Adds Typescript types support --- lib/index.d.ts | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 3 ++- src/index.js | 12 +++++++++-- test/index.cjs | 2 +- 4 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 lib/index.d.ts diff --git a/lib/index.d.ts b/lib/index.d.ts new file mode 100644 index 0000000..8230c4b --- /dev/null +++ b/lib/index.d.ts @@ -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.} options + */ +declare function initializeCollections(options: { + [x: string]: CollectionConfig | string; +}): Metalsmith.Plugin; +declare namespace initializeCollections { + export { defaultOptions as defaults }; +} + +declare const defaultOptions: CollectionConfig; diff --git a/package.json b/package.json index 213698c..0d0136b 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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" }, diff --git a/src/index.js b/src/index.js index c6adfb5..ef36a39 100755 --- a/src/index.js +++ b/src/index.js @@ -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.} options * @return {import('metalsmith').Plugin} diff --git a/test/index.cjs b/test/index.cjs index 00dcbe9..f05292e 100755 --- a/test/index.cjs +++ b/test/index.cjs @@ -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 */