Skip to content

Commit

Permalink
refactor: refactor types
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Sketon committed Jan 9, 2024
1 parent 46c4809 commit a2432d6
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 14 deletions.
3 changes: 2 additions & 1 deletion lib/extend/console.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Promise from 'bluebird';
import abbrev from 'abbrev';
import type { NodeJSLikeCallback } from '../types';

type Option = Partial<{
usage: string;
Expand All @@ -19,7 +20,7 @@ interface Args {
_: string[];
[key: string]: string | boolean | string[];
}
type AnyFn = (args: Args) => any;
type AnyFn = (args: Args, callback?: NodeJSLikeCallback<any>) => any;
interface StoreFunction extends AnyFn {
desc?: string;
options?: Option;
Expand Down
3 changes: 2 additions & 1 deletion lib/extend/deployer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import Promise from 'bluebird';
import type { NodeJSLikeCallback } from '../types';

interface StoreFunction {
(deployArg: {
type: string;
[key: string]: any
}) : any;
}, callback?: NodeJSLikeCallback<any>) : any;
}
interface Store {
[key: string]: StoreFunction
Expand Down
4 changes: 2 additions & 2 deletions lib/extend/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Filter {
if (index !== -1) list.splice(index, 1);
}

exec(type: string, data: any[], options: FilterOptions = {}): Promise<any> {
exec(type: string, data: any, options: FilterOptions = {}): Promise<any> {
const filters = this.list(type);
if (filters.length === 0) return Promise.resolve(data);

Expand All @@ -91,7 +91,7 @@ class Filter {
})).then(() => args[0]);
}

execSync(type: string, data: any[], options: FilterOptions = {}) {
execSync(type: string, data: any, options: FilterOptions = {}) {
const filters = this.list(type);
const filtersLen = filters.length;
if (filtersLen === 0) return data;
Expand Down
3 changes: 2 additions & 1 deletion lib/extend/generator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Promise from 'bluebird';
import type { NodeJSLikeCallback } from '../types';

interface BaseObj {
path: string;
Expand All @@ -9,7 +10,7 @@ type ReturnType = BaseObj | BaseObj[];
type GeneratorReturnType = ReturnType | Promise<ReturnType>;

interface GeneratorFunction {
(locals: object): GeneratorReturnType;
(locals: object, callback?: NodeJSLikeCallback<any>): GeneratorReturnType;
}

type StoreFunctionReturn = Promise<ReturnType>;
Expand Down
4 changes: 3 additions & 1 deletion lib/extend/migrator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Promise from 'bluebird';
import type { NodeJSLikeCallback } from '../types';

interface StoreFunction {
(args: any): any
(args: any, callback?: NodeJSLikeCallback<any>): any
}

interface Store {
Expand Down
5 changes: 3 additions & 2 deletions lib/extend/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface StoreSyncFunction {
(
data: StoreFunctionData,
options: object,
// callback: NodeJSLikeCallback<string>
// callback?: NodeJSLikeCallback<string>
): any;
output?: string;
compile?: (local: object) => any;
Expand All @@ -31,6 +31,7 @@ export interface StoreFunction {
(
data: StoreFunctionData,
options: object,
callback?: NodeJSLikeCallback<any>
): Promise<any>;
(
data: StoreFunctionData,
Expand Down Expand Up @@ -58,7 +59,7 @@ class Renderer {
this.storeSync = {};
}

list(sync: boolean): Store | SyncStore {
list(sync?: boolean): Store | SyncStore {
return sync ? this.storeSync : this.store;
}

Expand Down
9 changes: 6 additions & 3 deletions lib/extend/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const rCodeTag = /<code[^<>]*>[\s\S]+?<\/code>/g;
const escapeSwigTag = (str: string) => str.replace(/{/g, '&#123;').replace(/}/g, '&#125;');

interface TagFunction {
(args: any[], content: string): string;
(args: any[], content: string, callback?: NodeJSLikeCallback<any>): string | PromiseLike<string>;
}
interface AsyncTagFunction {
(args: any[], content: string): Promise<string>;
Expand Down Expand Up @@ -253,14 +253,17 @@ class Tag {
if (env.hasExtension(name)) env.removeExtension(name);
}

render(str: string, options: { source?: string } = {}, callback?: NodeJSLikeCallback<any>): Promise<any> {
render(str: string): Promise<any>;
render(str: string, callback: NodeJSLikeCallback<any>): Promise<any>;
render(str: string, options: { source?: string, [key: string]: any }, callback?: NodeJSLikeCallback<any>): Promise<any>;
render(str: string, options: { source?: string, [key: string]: any } | NodeJSLikeCallback<any> = {}, callback?: NodeJSLikeCallback<any>): Promise<any> {
if (!callback && typeof options === 'function') {
callback = options;
options = {};
}

// Get path of post from source
const { source = '' } = options;
const { source = '' } = options as { source?: string };

return Promise.fromCallback(cb => {
this.env.renderString(
Expand Down
4 changes: 3 additions & 1 deletion lib/hexo/default_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ export = {
wrap: true,
exclude_languages: [],
language_attr: false,
hljs: false
hljs: false,
line_threshold: 0,
first_line_number: 'always1'
},
prismjs: {
preprocess: true,
Expand Down
18 changes: 16 additions & 2 deletions lib/plugins/highlight/highlight.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import type Hexo from '../../hexo';

interface Options {
line_threshold?: number;
line_number?: boolean;
lines_length?: number;
language_attr?: boolean;
caption?: string;
firstLine?: number;
lang?: string;
mark?: number[];
firstLineNumber?: number;
}

// Lazy require highlight.js
let highlight;

module.exports = function highlightFilter(code, options) {
const hljsCfg = this.config.highlight || {};
module.exports = function highlightFilter(this: Hexo, code: string, options: Options) {
const hljsCfg = this.config.highlight || {} as any;
const line_threshold = options.line_threshold || hljsCfg.line_threshold || 0;
const shouldUseLineNumbers = typeof options.line_number === 'undefined' ? hljsCfg.line_number : options.line_number;
const surpassesLineThreshold = options.lines_length > line_threshold;
Expand Down

0 comments on commit a2432d6

Please sign in to comment.