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 19, 2024
1 parent c1c5aaa commit ce3229c
Show file tree
Hide file tree
Showing 26 changed files with 280 additions and 68 deletions.
4 changes: 3 additions & 1 deletion lib/hexo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import multiConfigPath from './multi_config_path';
import { deepMerge, full_url_for } from 'hexo-util';
import type Box from '../box';
import type { AssetGenerator, LocalsType, NodeJSLikeCallback, NormalPageGenerator, NormalPostGenerator, PageGenerator, PostGenerator, SiteLocals } from '../types';
import type { AddSchemaTypeOptions } from 'warehouse/dist/types';
import type Schema from 'warehouse/dist/schema';

let resolveSync; // = require('resolve');

Expand Down Expand Up @@ -440,7 +442,7 @@ class Hexo extends EventEmitter {
return Promise.reject(new Error(`Console \`${name}\` has not been registered yet!`));
}

model(name: string, schema?: any) {
model(name: string, schema?: Schema | Record<string, AddSchemaTypeOptions>) {
return this.database.model(name, schema);
}

Expand Down
3 changes: 2 additions & 1 deletion lib/plugins/generator/post.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { PostGenerator, PostSchema, SiteLocals } from '../../types';
import type Document from 'warehouse/dist/document';

function postGenerator(locals: SiteLocals): PostGenerator[] {
const posts = locals.posts.sort('-date').toArray();
const { length } = posts;

return posts.map((post: PostSchema, i: number) => {
return posts.map((post: Document<PostSchema>, i: number) => {
const { path, layout } = post;

if (!layout || layout === 'false') {
Expand Down
3 changes: 2 additions & 1 deletion lib/plugins/helper/css.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { htmlTag, url_for } from 'hexo-util';
import moize from 'moize';
import type { LocalsType } from '../../types';

let relative_link = true;
function cssHelper(...args: any[]) {
function cssHelper(this: LocalsType, ...args: any[]) {
let result = '\n';

relative_link = this.config.relative_link;
Expand Down
13 changes: 6 additions & 7 deletions lib/plugins/helper/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ function toISOString(date: string | number | Date | moment.Moment) {
return new Date(date as (string | number)).toISOString();
}

function dateHelper(date: moment.Moment | moment.MomentInput, format: string) {
function dateHelper(this: LocalsType, date: moment.Moment | moment.MomentInput, format?: string) {
const { config } = this;
const moment = getMoment(date, getLanguage(this), config.timezone);
return moment.format(format || config.date_format);
}

function timeHelper(date: moment.Moment | moment.MomentInput, format: string) {
function timeHelper(this: LocalsType, date: moment.Moment | moment.MomentInput, format?: string) {
const { config } = this;
const moment = getMoment(date, getLanguage(this), config.timezone);
return moment.format(format || config.time_format);
}

function fullDateHelper(date: moment.Moment | moment.MomentInput, format: string) {
function fullDateHelper(this: LocalsType, date: moment.Moment | moment.MomentInput, format: string) {
if (format) {
const moment = getMoment(date, getLanguage(this), this.config.timezone);
return moment.format(format);
Expand All @@ -50,15 +50,14 @@ function fullDateHelper(date: moment.Moment | moment.MomentInput, format: string
return `${this.date(date)} ${this.time(date)}`;
}

function relativeDateHelper(date: moment.Moment | moment.MomentInput) {
function relativeDateHelper(this: LocalsType, date: moment.Moment | moment.MomentInput) {
const { config } = this;
const moment = getMoment(date, getLanguage(this), config.timezone);
return moment.fromNow();
}

function timeTagHelper(date: string | number | Date | moment.Moment, format: string) {
const { config } = this;
return `<time datetime="${toISOString(date)}">${this.date(date, format, getLanguage(this), config.timezone)}</time>`;
function timeTagHelper(this: LocalsType, date: string | number | Date | moment.Moment, format: string) {
return `<time datetime="${toISOString(date)}">${this.date(date, format)}</time>`;
}

function getLanguage(ctx: LocalsType) {
Expand Down
3 changes: 2 additions & 1 deletion lib/plugins/helper/favicon_tag.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { url_for } from 'hexo-util';
import type { LocalsType } from '../../types';

function faviconTagHelper(path: string) {
function faviconTagHelper(this: LocalsType, path: string) {
return `<link rel="shortcut icon" href="${url_for.call(this, path)}">`;
}

Expand Down
7 changes: 4 additions & 3 deletions lib/plugins/helper/feed_tag.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { url_for } from 'hexo-util';
import moize from 'moize';
import type { LocalsType } from '../../types';

const feedFn = (str = '') => {
if (str) return str.replace(/2$/, '');
Expand All @@ -11,7 +12,7 @@ interface Options {
type?: string;
}

function makeFeedTag(path: string, options: Options = {}, configFeed?: any, configTitle?: string) {
function makeFeedTag(this: LocalsType, path: string, options: Options = {}, configFeed?: any, configTitle?: string) {
const title = options.title || configTitle;

if (path) {
Expand Down Expand Up @@ -46,9 +47,9 @@ function makeFeedTag(path: string, options: Options = {}, configFeed?: any, conf
return '';
}

function feedTagHelper(path: string, options = {}) {
function feedTagHelper(this: LocalsType, path: string, options: Options = {}) {
const { config } = this;
return moize.deep(makeFeedTag.bind(this))(path, options, config.feed, config.title);
return moize.deep(makeFeedTag.bind(this))(path, options, (config as any).feed, config.title);
}

export = feedTagHelper;
2 changes: 1 addition & 1 deletion lib/plugins/helper/fragment_cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export = (ctx: Hexo) => {
// reset cache for watch mode
ctx.on('generateBefore', () => { cache.flush(); });

return function fragmentCache(id, fn) {
return function fragmentCache(id: string, fn: () => any) {
if (this.cache) return cache.apply(id, fn);

const result = fn();
Expand Down
3 changes: 2 additions & 1 deletion lib/plugins/helper/full_url_for.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import { full_url_for } from 'hexo-util';
import type { LocalsType } from '../../types';

export = function(path: string) {
export = function(this: LocalsType, path: string) {
return full_url_for.call(this, path);
}
3 changes: 2 additions & 1 deletion lib/plugins/helper/image_tag.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { htmlTag, url_for } from 'hexo-util';
import type { LocalsType } from '../../types';

interface Options {
src?: string;
Expand All @@ -11,7 +12,7 @@ interface Attrs {
[key: string]: string | undefined;
}

function imageTagHelper(path: string, options: Options = {}) {
function imageTagHelper(this: LocalsType, path: string, options: Options = {}) {
const attrs = Object.assign({
src: url_for.call(this, path) as string
}, options);
Expand Down
12 changes: 7 additions & 5 deletions lib/plugins/helper/is.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
function isCurrentHelper(path = '/', strict: boolean) {
import type { LocalsType } from '../../types';

function isCurrentHelper(this: LocalsType, path = '/', strict: boolean) {
const currentPath = this.path.replace(/^[^/].*/, '/$&');

if (strict) {
Expand Down Expand Up @@ -37,7 +39,7 @@ function isArchiveHelper() {
return Boolean(this.page.archive);
}

function isYearHelper(year) {
function isYearHelper(year?) {
const { page } = this;
if (!page.archive) return false;

Expand All @@ -48,7 +50,7 @@ function isYearHelper(year) {
return Boolean(page.year);
}

function isMonthHelper(year, month) {
function isMonthHelper(year?, month?) {
const { page } = this;
if (!page.archive) return false;

Expand All @@ -63,15 +65,15 @@ function isMonthHelper(year, month) {
return Boolean(page.year && page.month);
}

function isCategoryHelper(category) {
function isCategoryHelper(category?) {
if (category) {
return this.page.category === category;
}

return Boolean(this.page.category);
}

function isTagHelper(tag) {
function isTagHelper(tag?) {
if (tag) {
return this.page.tag === tag;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/plugins/helper/js.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { htmlTag, url_for } from 'hexo-util';
import moize from 'moize';
import type { LocalsType } from '../../types';

let relative_link = true;
function jsHelper(...args: any[]) {
function jsHelper(this: LocalsType, ...args: any[]) {
let result = '\n';

relative_link = this.config.relative_link;
Expand Down
3 changes: 2 additions & 1 deletion lib/plugins/helper/link_to.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { htmlTag, url_for } from 'hexo-util';
import type { LocalsType } from '../../types';

interface Options {
href?: string;
Expand All @@ -19,7 +20,7 @@ interface Attrs {
[key: string]: string | boolean | null | undefined;
}

function linkToHelper(path: string, text: string, options: Options | boolean = {}) {
function linkToHelper(this: LocalsType, path: string, text: string, options: Options | boolean = {}) {
if (typeof options === 'boolean') options = {external: options};

if (!text) text = path.replace(/^https?:\/\/|\/$/g, '');
Expand Down
3 changes: 2 additions & 1 deletion lib/plugins/helper/list_archives.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { LocalsType } from '../../types';
import { toMomentLocale } from './date';
import { url_for } from 'hexo-util';

Expand All @@ -19,7 +20,7 @@ interface Data {
count: number;
}

function listArchivesHelper(options: Options = {}) {
function listArchivesHelper(this: LocalsType, options: Options = {}) {
const { config } = this;
const archiveDir = config.archive_dir;
const { timezone } = config;
Expand Down
29 changes: 24 additions & 5 deletions lib/plugins/helper/list_categories.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
import { url_for } from 'hexo-util';
import type { CategorySchema, LocalsType } from '../../types';
import type Query from 'warehouse/dist/query';

interface Options {
style?: string;
class?: string;
depth?: number | string;
orderby?: string;
order?: number;
show_count?: boolean;
show_current?: boolean;
transform?: (name: string) => string;
separator?: string;
suffix?: string;
children_indicator?: boolean;
}

function listCategoriesHelper(categories, options) {
function listCategoriesHelper(this: LocalsType, options?: Options): string;
function listCategoriesHelper(this: LocalsType, categories: Query<CategorySchema>, options?: Options): string;
function listCategoriesHelper(this: LocalsType, categories?: Query<CategorySchema> | Options, options?: Options) {
if (!options && (!categories || !Object.prototype.hasOwnProperty.call(categories, 'length'))) {
options = categories;
options = categories as Options;
categories = this.site.categories;
}
categories = categories as Query<CategorySchema>;

if (!categories || !categories.length) return '';
options = options || {};

const { style = 'list', transform, separator = ', ', suffix = '' } = options;
const showCount = Object.prototype.hasOwnProperty.call(options, 'show_count') ? options.show_count : true;
const className = options.class || 'category';
const depth = options.depth ? parseInt(options.depth, 10) : 0;
const depth = options.depth ? parseInt(String(options.depth), 10) : 0;
const orderby = options.orderby || 'name';
const order = options.order || 1;
const showCurrent = options.show_current || false;
Expand All @@ -27,13 +46,13 @@ function listCategoriesHelper(categories, options) {
query.parent = {$exists: false};
}

return categories.find(query).sort(orderby, order);
return (categories as Query<CategorySchema>).find(query).sort(orderby, order);
};

const hierarchicalList = (level: number, parent?: any) => {
let result = '';

prepareQuery(parent).forEach(cat => {
prepareQuery(parent).forEach((cat: CategorySchema) => {
let child;
if (!depth || level + 1 < depth) {
child = hierarchicalList(level + 1, cat._id);
Expand Down
20 changes: 18 additions & 2 deletions lib/plugins/helper/list_posts.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import { url_for } from 'hexo-util';
import type { LocalsType, PostSchema } from '../../types';
import type Query from 'warehouse/dist/query';

interface Options {
style?: string;
class?: string;
amount?: number;
orderby?: string;
order?: number;
transform?: (name: string) => string;
separator?: string;
}

function listPostsHelper(posts, options) {
function listPostsHelper(this: LocalsType, options?: Options): string;
function listPostsHelper(this: LocalsType, posts: Query<PostSchema>, options?: Options): string;
function listPostsHelper(this: LocalsType, posts?: Query<PostSchema> | Options, options?: Options) {
if (!options && (!posts || !Object.prototype.hasOwnProperty.call(posts, 'length'))) {
options = posts;
options = posts as Options;
posts = this.site.posts;
}

posts = posts as Query<PostSchema>;

options = options || {};

const { style = 'list', transform, separator = ', ' } = options;
Expand Down
29 changes: 24 additions & 5 deletions lib/plugins/helper/list_tags.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
import { url_for, escapeHTML } from 'hexo-util';
import moize from 'moize';
import type { LocalsType, TagSchema } from '../../types';
import type Query from 'warehouse/dist/query';

interface Options {
style?: string;
class?: any;
amount?: number;
orderby?: string;
order?: number;
transform?: (name: string) => string;
separator?: string;
show_count?: boolean;
suffix?: string;
}

function listTagsHelper(tags, options?) {
function listTagsHelper(this: LocalsType, options?: Options): string;
function listTagsHelper(this: LocalsType, tags: Query<TagSchema>, options?: Options): string;
function listTagsHelper(this: LocalsType, tags?: Query<TagSchema> | Options, options?: Options) {
if (!options && (!tags || !Object.prototype.hasOwnProperty.call(tags, 'length'))) {
options = tags;
options = tags as Options;
tags = this.site.tags;
}
tags = tags as Query<TagSchema>;

if (!tags || !tags.length) return '';
options = options || {};
Expand Down Expand Up @@ -87,13 +104,15 @@ function listTagsHelper(tags, options?) {

return result;
}

function listTagsHelperFactory(tags, options) {
function listTagsHelperFactory(options?: Options): string;
function listTagsHelperFactory(tags: Query<TagSchema>, options?: Options): string;
function listTagsHelperFactory(tags?: Query<TagSchema> | Options, options?: Options) {
const transformArgs = () => {
if (!options && (!tags || !Object.prototype.hasOwnProperty.call(tags, 'length'))) {
options = tags;
options = tags as Options;
tags = this.site.tags;
}
tags = tags as Query<TagSchema>;

return [tags.toArray(), options];
};
Expand Down
4 changes: 3 additions & 1 deletion lib/plugins/helper/markdown.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
function markdownHelper(text: string, options) {
import type { LocalsType } from '../../types';

function markdownHelper(this: LocalsType, text: string, options: any) {
return this.render(text, 'markdown', options);
}

Expand Down
4 changes: 3 additions & 1 deletion lib/plugins/helper/meta_generator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
function metaGeneratorHelper() {
import type { LocalsType } from '../../types';

function metaGeneratorHelper(this: LocalsType) {
return `<meta name="generator" content="Hexo ${this.env.version}">`;
}

Expand Down
Loading

0 comments on commit ce3229c

Please sign in to comment.