From 7400b27a34000a7b1370694cabfc946c7c2adf71 Mon Sep 17 00:00:00 2001 From: Kevin Van Lierde Date: Tue, 4 Jul 2023 02:08:47 +0200 Subject: [PATCH] Adds Typescript types --- .gitignore | 5 +++-- lib/index.d.ts | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/index.js | 4 ++-- 3 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 lib/index.d.ts diff --git a/.gitignore b/.gitignore index f3897f7..cdb1430 100644 --- a/.gitignore +++ b/.gitignore @@ -6,5 +6,6 @@ coverage coverage.info test/fixtures/*/build node_modules -lib -!lib/*.d.ts \ No newline at end of file +lib/*.map +lib/*.cjs +lib/*.js \ No newline at end of file diff --git a/lib/index.d.ts b/lib/index.d.ts new file mode 100644 index 0000000..799ee3d --- /dev/null +++ b/lib/index.d.ts @@ -0,0 +1,52 @@ +import { Plugin } from 'metalsmith'; +export default initializeInPlace; +export type Render = (source: string, options: any, locals: any) => string; +export type RenderAsync = (source: string, options: any, locals: any, callback: Function) => Promise; +export type Compile = (source: string, options: any) => string; +export type CompileAsync = (source: string, options: any, callback: Function) => Promise; +export type JsTransformer = { + name: string; + inputFormats: string[]; + outputFormat: string; + render?: Render; + renderAsync?: RenderAsync; + compile?: Compile; + compileAsync?: CompileAsync; + [key]?: string; +}; +export type Options = { + /** + * Jstransformer to run: name of a node module or local JS module path (starting with `.`) whose default export is a jstransformer. As a shorthand for existing transformers you can remove the `jstransformer-` prefix: `marked` will be understood as `jstransformer-marked`. Or an actual jstransformer; an object with `name`, `inputFormats`,`outputFormat`, and at least one of the render methods `render`, `renderAsync`, `compile` or `compileAsync` described in the [jstransformer API docs](https://github.com/jstransformers/jstransformer#api) + */ + transform: string | JsTransformer; + /** + * One or more paths or glob patterns to limit the scope of the transform. + * @default '**\/*.' + */ + pattern?: string|string[]; + /** + * Pass options to the jstransformer templating engine that's rendering your files. + * @default {} + */ + engineOptions?: any; +}; +/** + * A metalsmith plugin for in-place templating + * @example + * import nunjucks from 'jstransformer-nunjucks' + * + * metalsmith + * .use(inPlace({ transform: 'jstransformer-nunjucks' })) // use jstransformer-nunjucks + * .use(inPlace({ transform: 'nunjucks' })) // shorthand for above + * .use(inPlace({ transform: nunjucks })) // equivalent to above + * .use(inPlace({ transform: './local/transform.js' })) // custom local transformer + * .use(inPlace({ transform: { // custom inline transformer + * name: 'prepend-hello', + * inputFormats: ['prepend-hello'], + * outputFormat: 'html', + * render(str, options, locals) => { + * return 'hello ' + str + * } + * }})) + */ +declare function initializeInPlace(options?: Options): Plugin; diff --git a/src/index.js b/src/index.js index a71c3eb..4f31496 100644 --- a/src/index.js +++ b/src/index.js @@ -173,8 +173,8 @@ function validate({ filename, files, transform }) { /** * @typedef {Object} Options - * @property {string|JsTransformer} transform Jstransformer to run - * @property {string} [pattern='**'] (*optional*) Limit the files to process by 1 or more glob patterns. Defaults to `'**'` (all) + * @property {string|JsTransformer} transform Jstransformer to run: name of a node module or local JS module path (starting with `.`) whose default export is a jstransformer. As a shorthand for existing transformers you can remove the `jstransformer-` prefix: `marked` will be understood as `jstransformer-marked`. Or an actual jstransformer; an object with `name`, `inputFormats`,`outputFormat`, and at least one of the render methods `render`, `renderAsync`, `compile` or `compileAsync` described in the [jstransformer API docs](https://github.com/jstransformers/jstransformer#api) + * @property {string} [pattern='**\/*.'] (*optional*) One or more paths or glob patterns to limit the scope of the transform. Defaults to `'**\/*.'` * @property {Object} [engineOptions={}] (*optional*) Pass options to the jstransformer templating engine that's rendering your files. The default is `{}` **/