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 Aug 19, 2023
1 parent 683a32e commit eb8b08c
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 38 deletions.
6 changes: 2 additions & 4 deletions lib/box/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ class Box extends EventEmitter {
public File: any;
public ignore: any[];
public source: any;
public emit: any;
public ctx: any;

constructor(ctx: Hexo, base: string, options?: object) {
super();
Expand Down Expand Up @@ -65,13 +63,13 @@ class Box extends EventEmitter {
class _File extends File {
public box: Box;

render(options?: any) {
render(options?: object) {
return ctx.render.render({
path: this.source
}, options);
}

renderSync(options?: any) {
renderSync(options?: object) {
return ctx.render.renderSync({
path: this.source
}, options);
Expand Down
4 changes: 2 additions & 2 deletions lib/extend/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface StoreSyncFunction {
// callback: (err: Error, value: string) => any
): any;
output?: string;
compile?: (local: object) => string;
compile?: (local: object) => any;
}
export interface StoreFunction {
(
Expand All @@ -37,7 +37,7 @@ export interface StoreFunction {
callback: (err: Error, value: string) => any
): void;
output?: string;
compile?: (local: object) => string;
compile?: (local: object) => any;
disableNunjucks?: boolean;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/hexo/multi_config_path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import yml from 'js-yaml';
import { deepMerge } from 'hexo-util';
import type Hexo from './index';

export = (ctx: Hexo) => function multiConfigPath(base: string, configPaths: string, outputDir: any) {
export = (ctx: Hexo) => function multiConfigPath(base: string, configPaths: string, outputDir: string) {
const { log } = ctx;
const defaultPath = join(base, '_config.yml');

Expand Down
25 changes: 15 additions & 10 deletions lib/hexo/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class PostRenderEscape {
}
}

const prepareFrontMatter = (data: object, jsonMode: boolean) => {
const prepareFrontMatter = (data: any, jsonMode: boolean) => {
for (const [key, item] of Object.entries(data)) {
if (moment.isMoment(item)) {
data[key] = item.utc().format('YYYY-MM-DD HH:mm:ss');
Expand Down Expand Up @@ -232,19 +232,24 @@ interface Data {
source?: string;
}

interface PostData {
title?: string;
layout?: string;
slug?: string;
path?: string;
[prop: string]: any;
}

class Post {
public context: Hexo;
public config: any;
public tag: any;
public separator: string;

constructor(context: Hexo) {
this.context = context;
}

create(data: any, callback?: (...args: any[]) => any);
create(data: any, replace: boolean, callback?: (...args: any[]) => any);
create(data: any, replace: boolean | ((...args: any[]) => any), callback?: (...args: any[]) => any) {
create(data: PostData, callback?: (...args: any[]) => any);
create(data: PostData, replace: boolean, callback?: (...args: any[]) => any);
create(data: PostData, replace: boolean | ((...args: any[]) => any), callback?: (...args: any[]) => any) {
if (!callback && typeof replace === 'function') {
callback = replace;
replace = false;
Expand Down Expand Up @@ -288,7 +293,7 @@ class Post {
});
}

_renderScaffold(data: any) {
_renderScaffold(data: PostData) {
const { tag } = this.context.extend;
let splitted;

Expand Down Expand Up @@ -330,7 +335,7 @@ class Post {
});
}

publish(data: any, replace: boolean, callback: (...args: any[]) => any) {
publish(data: PostData, replace: boolean, callback?: (...args: any[]) => any) {
if (!callback && typeof replace === 'function') {
callback = replace;
replace = false;
Expand Down Expand Up @@ -383,7 +388,7 @@ class Post {
}).thenReturn(result).asCallback(callback);
}

render(source: string, data: Data = {}, callback: (...args: any[]) => any) {
render(source: string, data: Data = {}, callback?: (...args: any[]) => any) {
const ctx = this.context;
const { config } = ctx;
const { tag } = ctx.extend;
Expand Down
2 changes: 0 additions & 2 deletions lib/hexo/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class RouteStream extends Readable {
public _data: any;
public _ended: boolean;
public modified: any;
public push: any;
public emit: any;

constructor(data: Data) {
super({ objectMode: true });
Expand Down
6 changes: 3 additions & 3 deletions lib/plugins/tag/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ const rCaption = /\S[\S\s]*/;
function parseArgs(args: string[]): HighlightOptions {
const _else = [];
const len = args.length;
let lang, language_attr,
line_number, line_threshold, wrap;
let lang: string, language_attr: boolean,
line_number: boolean, line_threshold: number, wrap: boolean;
let firstLine = 1;
const mark = [];
for (let i = 0; i < len; i++) {
Expand Down Expand Up @@ -123,7 +123,7 @@ export = (ctx: Hexo) => function codeTag(args: string[], content: string) {
return `<pre><code>${escapeHTML(content)}</code></pre>`;
}

let index;
let index: number;
let enableHighlight = true;

if ((index = args.findIndex(item => item.startsWith('highlight:'))) !== -1) {
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/tag/post_path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type Hexo from '../../hexo';
* {% post_path slug | title %}
*/
export = (ctx: Hexo) => {
return function postPathTag(args) {
return function postPathTag(args: any[]) {
const slug = args.shift();
if (!slug) return;

Expand Down
1 change: 1 addition & 0 deletions lib/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Theme extends Box {
public views: any;
public i18n: I18n;
public View: any;
public processors: any[];

constructor(ctx: Hexo, options?) {
super(ctx, ctx.theme_dir, options);
Expand Down
8 changes: 5 additions & 3 deletions lib/theme/processors/config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Pattern } from 'hexo-util';
import type { _File } from '../../box';
import Theme from '..';

function process(file) {
function process(file: _File) {
if (file.type === 'delete') {
file.box.config = {};
(file.box as Theme).config = {};
return;
}

return file.render().then(result => {
file.box.config = result;
(file.box as Theme).config = result;
this.log.debug('Theme config loaded.');
}).catch(err => {
this.log.error('Theme config load failed.');
Expand Down
6 changes: 4 additions & 2 deletions lib/theme/processors/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Pattern } from 'hexo-util';
import { extname } from 'path';
import type { _File } from '../../box';
import type Theme from '..';

function process(file) {
function process(file: _File) {
const { path } = file.params;
const ext = extname(path);
const name = path.substring(0, path.length - ext.length);
const { i18n } = file.box;
const { i18n } = (file.box as Theme);

if (file.type === 'delete') {
i18n.remove(name);
Expand Down
3 changes: 2 additions & 1 deletion lib/theme/processors/source.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Pattern } from 'hexo-util';
import * as common from '../../plugins/processor/common';
import type { _File } from '../../box';

function process(file) {
function process(file: _File) {
const Asset = this.model('Asset');
const id = file.source.substring(this.base_dir.length).replace(/\\/g, '/');
const { path } = file.params;
Expand Down
8 changes: 5 additions & 3 deletions lib/theme/processors/view.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { Pattern } from 'hexo-util';
import type { _File } from '../../box';
import type Theme from '..';

function process(file) {
function process(file: _File) {
const { path } = file.params;

if (file.type === 'delete') {
file.box.removeView(path);
(file.box as Theme).removeView(path);
return;
}

return file.read().then(result => {
file.box.setView(path, result);
(file.box as Theme).setView(path, result);
});
}

Expand Down
12 changes: 6 additions & 6 deletions lib/theme/view.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { dirname, extname, join } from 'path';
import { parse as yfm } from 'hexo-front-matter';
import Promise from 'bluebird';
import type Theme from '.';
import type Render from '../hexo/render';

const assignIn = (target, ...sources) => {
const assignIn = (target: any, ...sources: any[]) => {
const length = sources.length;

if (length < 1 || target == null) return target;
Expand All @@ -23,14 +25,12 @@ class Options {
class View {
public path: string;
public source: any;
public _theme: any;
public _theme: Theme;
public data: any;
public _compiled: any;
public _compiledSync: any;
public _helper: any;
public _render: any;
public layout: any;
public _content: any;
public _render: Render;

constructor(path: string, data) {
this.path = path;
Expand Down Expand Up @@ -128,7 +128,7 @@ class View {
text: this.data._content
};

function buildFilterArguments(result) {
function buildFilterArguments(result: any): [string, any, any] {
const output = render.getOutput(ext) || ext;
return [
`after_render:${output}`,
Expand Down

0 comments on commit eb8b08c

Please sign in to comment.