Skip to content

Commit

Permalink
chore: moved interfaces to renderer-d.ts using ES Export style
Browse files Browse the repository at this point in the history
to easily import between source files
chore(Renderer.register): add overload method types
docs: add JSDoc
  • Loading branch information
dimaslanjaka committed May 18, 2023
1 parent 1ab1d58 commit b33d7e7
Showing 1 changed file with 41 additions and 40 deletions.
81 changes: 41 additions & 40 deletions lib/extend/renderer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { extname } from 'path';
import Promise from 'bluebird';
import { Store, SyncStore, StoreSyncFunction, StoreFunction } from './renderer-d';

const getExtname = (str: string) => {
if (typeof str !== 'string') return '';
Expand All @@ -8,46 +9,6 @@ const getExtname = (str: string) => {
return ext.startsWith('.') ? ext.slice(1) : ext;
};

interface StoreSyncFunction {
(
data: {
path?: string;
text: string;
},
options: object,
// callback: (err: Error, value: string) => any
): any;
output?: string;
compile?: (local: object) => string;
}
interface StoreFunction {
(
data: {
path?: string;
text: string;
},
options: object,
): Promise<any>;
(
data: {
path?: string;
text: string;
},
options: object,
callback: (err: Error, value: string) => any
): void;
output?: string;
compile?: (local: object) => string;
disableNunjucks?: boolean;
}

interface SyncStore {
[key: string]: StoreSyncFunction;
}
interface Store {
[key: string]: StoreFunction;
}

class Renderer {
public store: Store;
public storeSync: SyncStore;
Expand Down Expand Up @@ -80,10 +41,50 @@ class Renderer {
return renderer ? renderer.output : '';
}

/**
* register renderer engine
* - [hexo-renderer-nunjucks example](https://github.com/hexojs/hexo-renderer-nunjucks/blob/c71a1979535c47c3949ff6bf3a85691641841e12/lib/renderer.js#L55-L56)
* - [typescript example](https://github.com/dimaslanjaka/hexo-renderers/blob/feed801e90920bea8a5e7000b275b912e4ef6c43/src/renderer-sass.ts#L37-L38)
* @param name input extension name. ex: ejs
* @param output output extension name. ex: html
* @param fn renderer function
*/
register(name: string, output: string, fn: StoreFunction): void;

/**
* register renderer engine asynchronous
* @param name input extension name. ex: ejs
* @param output output extension name. ex: html
* @param fn renderer asynchronous function
* @param sync is synchronous?
*/
register(name: string, output: string, fn: StoreFunction, sync: false): void;

/**
* register renderer engine
* @param name input extension name. ex: ejs
* @param output output extension name. ex: html
* @param fn renderer function
* @param sync is synchronous?
*/
register(name: string, output: string, fn: StoreSyncFunction, sync: true): void;

/**
* register renderer engine
* @param name input extension name. ex: ejs
* @param output output extension name. ex: html
* @param fn renderer function
* @param sync is synchronous?
*/
register(name: string, output: string, fn: StoreFunction | StoreSyncFunction, sync: boolean): void;

/**
* register renderer engine
* @param name input extension name. ex: ejs
* @param output output extension name. ex: html
* @param fn renderer function
* @param sync is synchronous?
*/
register(name: string, output: string, fn: StoreFunction | StoreSyncFunction, sync?: boolean) {
if (!name) throw new TypeError('name is required');
if (!output) throw new TypeError('output is required');
Expand Down

0 comments on commit b33d7e7

Please sign in to comment.