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 all 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
27 changes: 22 additions & 5 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 @@ -18,7 +19,7 @@ interface StoreFunction {
}

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

class Filter {
Expand All @@ -35,6 +36,20 @@ class Filter {
return this.store[type] || [];
}

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(fn: StoreFunction): void
register(fn: StoreFunction, priority: number): void
register(type: string, fn: StoreFunction): void
Expand Down Expand Up @@ -85,10 +100,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 @@ -10,13 +10,13 @@ type ReturnType = BaseObj | BaseObj[];
type GeneratorReturnType = ReturnType | Promise<ReturnType>;

interface GeneratorFunction {
(locals: object, callback?: NodeJSLikeCallback<any>): GeneratorReturnType;
(locals: Record<string, any>, callback?: NodeJSLikeCallback<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
2 changes: 1 addition & 1 deletion lib/extend/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Processor {
register(pattern: patternType | StoreFunction, fn?: StoreFunction): void {
if (!fn) {
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.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,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
16 changes: 8 additions & 8 deletions lib/extend/syntax_highlight.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import type Hexo from '../hexo';

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

// plugins/filter/before_post_render/backtick_code_block
firstLineNumber?: string | number
firstLineNumber?: string | number;
lang: string | undefined;
caption: string | undefined;
lines_length?: number | undefined;

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

}

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

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

class SyntaxHighlight {
Expand All @@ -52,7 +50,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
Loading