Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improvement hexo.extend #5209

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
8f6e1a6
chore: moved interfaces and using ES Export style
dimaslanjaka May 18, 2023
cc9518b
feat: add data type `before_post_render`
dimaslanjaka May 18, 2023
1a11ad5
chore: change old type `object` to `Record<string, any>
dimaslanjaka May 18, 2023
6aee1f3
chore: add missing type param `ctx`
dimaslanjaka May 18, 2023
4a61ca5
fix: cast `pattern` as StoreFunction
dimaslanjaka May 18, 2023
83aa298
chore(lint): eslint --fix
dimaslanjaka May 18, 2023
31ef314
chore: convert interface `TagFunction` to overload type
dimaslanjaka May 18, 2023
f2e17aa
chore(StoreFunction): add explicit this
dimaslanjaka May 18, 2023
1ab1d58
feat(Filter.register): add overload method types
dimaslanjaka May 18, 2023
b33d7e7
chore: moved interfaces to `renderer-d.ts` using ES Export style
dimaslanjaka May 18, 2023
a5aef69
fix: conditional of data is actually an `object`
dimaslanjaka May 18, 2023
244be2f
fix(TS2345): Argument of type 'string | Buffer' is not assignable to …
dimaslanjaka May 18, 2023
b1312fc
chore(hexo.loadPlugin): make `callback` as optional
dimaslanjaka May 19, 2023
cfd7acb
feat(loadPlugin): add JSDoc
dimaslanjaka May 19, 2023
1e572c8
feat: add context interface `ExtendedTagProperty`
dimaslanjaka May 19, 2023
28a2e1f
chore: export as type
dimaslanjaka May 19, 2023
eeaacce
chore: update context type import
dimaslanjaka May 19, 2023
3c5ee1f
chore: merge from `hexojs/hexo` master
dimaslanjaka Feb 14, 2024
e1fd7d7
fix(TS2322): Type 'unknown' is not assignable to type 'string'.
dimaslanjaka Feb 14, 2024
e36d024
feat(deps-dev): install `@types/connect` for middleware `hexo-server`…
dimaslanjaka Feb 14, 2024
233dcb7
chore: import hexo from parent folder
dimaslanjaka Feb 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions lib/extend/filter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Promise from 'bluebird';
import { extend_filter_before_post_render_data } from '../plugins/filter/before_post_render/dataType';

const typeAlias = {
pre: 'before_post_render',
Expand All @@ -17,7 +18,7 @@ interface StoreFunction {
}

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

class Filter {
Expand All @@ -34,11 +35,27 @@ class Filter {
return this.store[type] || [];
}

register(fn: StoreFunction): void
register(fn: StoreFunction, priority: number): void
register(type: string, fn: StoreFunction): void
register(type: string, fn: StoreFunction, priority: number): void
register(type: string | StoreFunction, fn?: StoreFunction | number, priority?: number) {
register(fn: StoreFunction): void;
register(fn: StoreFunction, priority: number): void;
register(type: string, fn: StoreFunction): void;
register(
type: 'server_middleware',
fn: (app: import('connect').Server) => void
): void;
register(
type: 'before_post_render',
fn: (data: extend_filter_before_post_render_data) => void
): void;
register(
type: 'before_post_render',
fn: (data: extend_filter_before_post_render_data) => Promise<void>
): void;
register(type: string, fn: StoreFunction, priority: number): void;
register(
type: string | StoreFunction,
fn?: StoreFunction | number,
priority?: number
) {
if (!priority) {
if (typeof type === 'function') {
priority = fn as number;
Expand Down Expand Up @@ -84,10 +101,12 @@ class Filter {

args.unshift(data);

return Promise.each(filters, filter => Reflect.apply(Promise.method(filter), ctx, args).then(result => {
args[0] = result == null ? args[0] : result;
return args[0];
})).then(() => args[0]);
return Promise.each(filters, filter =>
Reflect.apply(Promise.method(filter), ctx, args).then(result => {
args[0] = result == null ? args[0] : result;
return args[0];
})
).then(() => args[0]);
}

execSync(type: string, data: any[], options: FilterOptions = {}) {
Expand Down
4 changes: 2 additions & 2 deletions lib/extend/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ type ReturnType = BaseObj | BaseObj[];
type GeneratorReturnType = ReturnType | Promise<ReturnType>;

interface GeneratorFunction {
(locals: object): GeneratorReturnType;
(locals: Record<string, any>): GeneratorReturnType;
}

type StoreFunctionReturn = Promise<ReturnType>;

interface StoreFunction {
(locals: object): StoreFunctionReturn;
(locals: Record<string, any>): StoreFunctionReturn;
}

interface Store {
Expand Down
5 changes: 2 additions & 3 deletions lib/extend/helper.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
interface StoreFunction {
(...args: any[]): string;
(this: import('../hexo'), ...args: any[]): any;
}

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


class Helper {
public store: Store;

Expand Down
4 changes: 2 additions & 2 deletions lib/extend/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class Processor {
register(fn: StoreFunction): void;
register(pattern: patternType, fn: StoreFunction): void;
register(pattern: patternType | StoreFunction, fn?: StoreFunction) {
if (!fn) {
if (typeof fn !== 'function') {
if (typeof pattern === 'function') {
fn = pattern;
fn = pattern as StoreFunction;
pattern = /(.*)/;
} else {
throw new TypeError('fn must be a function');
Expand Down
40 changes: 40 additions & 0 deletions lib/extend/renderer-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* to cast `data` type look example below
* @example
* type rendererData = Parameters<Parameters<typeof hexo.extend.renderer.register>[2]>[0];
* // or
* type rendererData = Parameters<Parameters<import('hexo')['extend']['renderer']['register']>[2]>[0];
* // or
* type rendererData = import('hexo/extend/renderer-d.ts').StoreFunctionData;
*/
export interface StoreFunctionData {
[key: string]: any;
path?: string;
text: string;
}

export interface StoreSyncFunction {
(
data: StoreFunctionData,
options: Record<string, any>
// callback: (err: Error, value: string) => any
): any;
output?: string;
compile?: (local: Record<string, any>) => string;
disableNunjucks?: boolean;
}

export interface StoreFunction {
(data: StoreFunctionData, options: Record<string, any>): Promise<any>;
(data: StoreFunctionData, options: Record<string, any>, callback: (err: Error, value: string) => any): void;
output?: string;
compile?: (local: Record<string, any>) => string;
disableNunjucks?: boolean;
}

export interface SyncStore {
[key: string]: StoreSyncFunction;
}
export interface Store {
[key: string]: StoreFunction;
}
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';
dimaslanjaka marked this conversation as resolved.
Show resolved Hide resolved

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
*/
Comment on lines +85 to +92
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to reduce/dedupe the comment here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to reduce/dedupe the comment here?

yes, or should we using shortlink from http://bit.ly to reduce comment length ?

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
17 changes: 9 additions & 8 deletions lib/extend/syntax_highlight.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type Hexo from '../hexo';

export interface HighlightOptions {
lang: string | undefined,
caption: string | undefined,
lines_length: number,
lang: string | undefined;
caption: string | undefined;
lines_length: number;

// plulgins/filter/before_post_render/backtick_code_block
firstLineNumber?: string | number
// plugins/filter/before_post_render/backtick_code_block
firstLineNumber?: string | number;

// plugins/tag/code.ts
language_attr?: boolean | undefined;
Expand All @@ -15,7 +15,6 @@ export interface HighlightOptions {
line_threshold?: number | undefined;
mark?: number[];
wrap?: boolean | undefined;

}

interface HighlightExecArgs {
Expand All @@ -29,7 +28,7 @@ interface StoreFunction {
}

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

class SyntaxHighlight {
Expand All @@ -52,7 +51,9 @@ class SyntaxHighlight {
exec(name: string, options: HighlightExecArgs): string {
const fn = this.store[name];

if (!fn) throw new TypeError(`syntax highlighter ${name} is not registered`);
if (!fn) {
throw new TypeError(`syntax highlighter ${name} is not registered`);
}
const ctx = options.context;
const args = options.args || [];

Expand Down
Loading