From edffe6d168d91739e4735aba802865650c9338ba Mon Sep 17 00:00:00 2001 From: D-Sketon <2055272094@qq.com> Date: Mon, 4 Sep 2023 13:59:19 +0800 Subject: [PATCH] update type --- lib/box/file.ts | 9 +++--- lib/box/index.ts | 32 +++++++++---------- lib/extend/console.ts | 4 +-- lib/extend/deployer.ts | 6 ++-- lib/extend/filter.ts | 6 ++-- lib/extend/generator.ts | 6 ++-- lib/extend/helper.ts | 2 +- lib/extend/injector.ts | 14 ++++---- lib/extend/migrator.ts | 6 ++-- lib/extend/processor.ts | 4 +-- lib/extend/renderer.ts | 10 +++--- lib/extend/syntax_highlight.ts | 4 +-- lib/extend/tag.ts | 6 ++-- lib/hexo/index.ts | 2 +- lib/plugins/console/clean.ts | 6 ++-- lib/plugins/console/config.ts | 9 +++--- lib/plugins/console/deploy.ts | 3 +- lib/plugins/console/generate.ts | 8 ++--- lib/plugins/console/list/category.ts | 2 +- lib/plugins/console/list/common.ts | 2 +- lib/plugins/console/list/index.ts | 3 +- lib/plugins/console/list/page.ts | 2 +- lib/plugins/console/list/post.ts | 2 +- lib/plugins/console/list/route.ts | 2 +- lib/plugins/console/list/tag.ts | 2 +- lib/plugins/console/migrate.ts | 3 +- lib/plugins/console/new.ts | 3 +- lib/plugins/console/publish.ts | 3 +- lib/plugins/console/render.ts | 3 +- .../filter/after_post_render/excerpt.ts | 2 +- .../filter/after_post_render/external_link.ts | 5 +-- lib/plugins/filter/after_post_render/index.ts | 4 ++- .../filter/after_render/external_link.ts | 5 +-- lib/plugins/filter/after_render/index.ts | 4 ++- .../filter/after_render/meta_generator.ts | 4 ++- lib/plugins/filter/before_exit/index.ts | 4 ++- .../filter/before_exit/save_database.ts | 4 ++- lib/plugins/filter/before_generate/index.ts | 4 ++- .../filter/before_generate/render_post.ts | 3 +- .../before_post_render/backtick_code_block.ts | 6 ++-- .../filter/before_post_render/index.ts | 4 ++- .../filter/before_post_render/titlecase.ts | 2 +- lib/plugins/filter/new_post_path.ts | 3 +- lib/plugins/filter/post_permalink.ts | 3 +- lib/plugins/filter/template_locals/i18n.ts | 3 +- lib/plugins/filter/template_locals/index.ts | 4 ++- lib/plugins/generator/asset.ts | 2 +- lib/types.ts | 2 +- 48 files changed, 132 insertions(+), 100 deletions(-) diff --git a/lib/box/file.ts b/lib/box/file.ts index 6b5e134704..70bccf1739 100644 --- a/lib/box/file.ts +++ b/lib/box/file.ts @@ -1,3 +1,4 @@ +import type Promise from 'bluebird'; import { readFile, readFileSync, stat, statSync, type ReadFileOptions } from 'hexo-fs'; class File { @@ -17,19 +18,19 @@ class File { this.type = type; } - read(options?: ReadFileOptions) { + read(options?: ReadFileOptions): Promise { return readFile(this.source, options); } - readSync(options?: ReadFileOptions) { + readSync(options?: ReadFileOptions): string | Buffer { return readFileSync(this.source, options); } - stat() { + stat(): any { return stat(this.source); } - statSync() { + statSync():any { return statSync(this.source); } } diff --git a/lib/box/index.ts b/lib/box/index.ts index 996594f8e8..3222b55880 100644 --- a/lib/box/index.ts +++ b/lib/box/index.ts @@ -81,9 +81,9 @@ class Box extends EventEmitter { return _File; } - addProcessor(pattern: (...args: any[]) => any); - addProcessor(pattern: string | RegExp | Pattern | ((...args: any[]) => any), fn: (...args: any[]) => any); - addProcessor(pattern: string | RegExp | Pattern | ((...args: any[]) => any), fn?: (...args: any[]) => any) { + addProcessor(pattern: (...args: any[]) => any): void; + addProcessor(pattern: string | RegExp | Pattern | ((...args: any[]) => any), fn: (...args: any[]) => any): void; + addProcessor(pattern: string | RegExp | Pattern | ((...args: any[]) => any), fn?: (...args: any[]) => any): void { if (!fn && typeof pattern === 'function') { fn = pattern; pattern = defaultPattern; @@ -98,7 +98,7 @@ class Box extends EventEmitter { }); } - _readDir(base: string, prefix = '') { + _readDir(base: string, prefix = ''): BlueBirdPromise { const { context: ctx } = this; const results = []; return readDirWalker(ctx, base, results, this.ignore, prefix) @@ -121,7 +121,7 @@ class Box extends EventEmitter { })); } - process(callback?: NodeJSLikeCallback) { + process(callback?: NodeJSLikeCallback): BlueBirdPromise { const { base, Cache, context: ctx } = this; return stat(base).then(stats => { @@ -133,14 +133,14 @@ class Box extends EventEmitter { // Handle deleted files return this._readDir(base) - .then(files => cacheFiles.filter(path => !files.includes(path))) - .map(path => this._processFile(File.TYPE_DELETE, path)); + .then((files: string[]) => cacheFiles.filter((path: string) => !files.includes(path))) + .map((path: string) => this._processFile(File.TYPE_DELETE, path) as PromiseLike); }).catch(err => { if (err && err.code !== 'ENOENT') throw err; }).asCallback(callback); } - _processFile(type: string, path: string) { + _processFile(type: string, path: string): BlueBirdPromise | BlueBirdPromise { if (this._processingFiles[path]) { return BlueBirdPromise.resolve(); } @@ -183,7 +183,7 @@ class Box extends EventEmitter { }).thenReturn(path); } - watch(callback?: NodeJSLikeCallback) { + watch(callback?: NodeJSLikeCallback): BlueBirdPromise { if (this.isWatching()) { return BlueBirdPromise.reject(new Error('Watcher has already started.')).asCallback(callback); } @@ -218,24 +218,24 @@ class Box extends EventEmitter { }).asCallback(callback); } - unwatch() { + unwatch(): void { if (!this.isWatching()) return; this.watcher.close(); this.watcher = null; } - isWatching() { + isWatching(): boolean { return Boolean(this.watcher); } } -function escapeBackslash(path: string) { +function escapeBackslash(path: string): string { // Replace backslashes on Windows return path.replace(/\\/g, '/'); } -function getHash(path: string) { +function getHash(path: string): BlueBirdPromise { const src = createReadStream(path); const hasher = createSha1Hash(); @@ -249,7 +249,7 @@ function getHash(path: string) { return finishedPromise.then(() => hasher.digest('hex')); } -function toRegExp(ctx: Hexo, arg: string) { +function toRegExp(ctx: Hexo, arg: string): RegExp | null { if (!arg) return null; if (typeof arg !== 'string') { ctx.log.warn('A value of "ignore:" section in "_config.yml" is not invalid (not a string)'); @@ -263,11 +263,11 @@ function toRegExp(ctx: Hexo, arg: string) { return result; } -function isIgnoreMatch(path: string, ignore: string | any[]) { +function isIgnoreMatch(path: string, ignore: string | any[]): boolean { return path && ignore && ignore.length && isMatch(path, ignore); } -function readDirWalker(ctx: Hexo, base: string, results: any[], ignore: any, prefix: string) { +function readDirWalker(ctx: Hexo, base: string, results: any[], ignore: any, prefix: string): BlueBirdPromise { if (isIgnoreMatch(base, ignore)) return BlueBirdPromise.resolve(); return BlueBirdPromise.map(readdir(base).catch(err => { diff --git a/lib/extend/console.ts b/lib/extend/console.ts index c4af6322d7..a5b7868af9 100644 --- a/lib/extend/console.ts +++ b/lib/extend/console.ts @@ -51,7 +51,7 @@ class Console { return this.store[this.alias[name]]; } - list() { + list(): Store { return this.store; } @@ -66,7 +66,7 @@ class Console { register(name: string, desc: string, fn: AnyFn): void register(name: string, options: Option, fn: AnyFn): void register(name: string, desc: string, options: Option, fn: AnyFn): void - register(name: string, desc: string | Option | AnyFn, options?: Option | AnyFn, fn?: AnyFn) { + register(name: string, desc: string | Option | AnyFn, options?: Option | AnyFn, fn?: AnyFn): void { if (!name) throw new TypeError('name is required'); if (!fn) { diff --git a/lib/extend/deployer.ts b/lib/extend/deployer.ts index d42d3d854c..ae88664cdc 100644 --- a/lib/extend/deployer.ts +++ b/lib/extend/deployer.ts @@ -17,15 +17,15 @@ class Deployer { this.store = {}; } - list() { + list(): Store { return this.store; } - get(name: string) { + get(name: string): StoreFunction { return this.store[name]; } - register(name: string, fn: StoreFunction) { + register(name: string, fn: StoreFunction): void { if (!name) throw new TypeError('name is required'); if (typeof fn !== 'function') throw new TypeError('fn must be a function'); diff --git a/lib/extend/filter.ts b/lib/extend/filter.ts index 78113d01c2..2af06fae87 100644 --- a/lib/extend/filter.ts +++ b/lib/extend/filter.ts @@ -38,7 +38,7 @@ class Filter { 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(type: string | StoreFunction, fn?: StoreFunction | number, priority?: number): void { if (!priority) { if (typeof type === 'function') { priority = fn as number; @@ -61,7 +61,7 @@ class Filter { store.sort((a, b) => a.priority - b.priority); } - unregister(type: string, fn: StoreFunction) { + unregister(type: string, fn: StoreFunction): void { if (!type) throw new TypeError('type is required'); if (typeof fn !== 'function') throw new TypeError('fn must be a function'); @@ -75,7 +75,7 @@ class Filter { if (index !== -1) list.splice(index, 1); } - exec(type: string, data: any[], options: FilterOptions = {}) { + exec(type: string, data: any[], options: FilterOptions = {}): Promise { const filters = this.list(type); if (filters.length === 0) return Promise.resolve(data); diff --git a/lib/extend/generator.ts b/lib/extend/generator.ts index 6360d1eb2c..2854f8f869 100644 --- a/lib/extend/generator.ts +++ b/lib/extend/generator.ts @@ -31,17 +31,17 @@ class Generator { this.store = {}; } - list() { + list(): Store { return this.store; } - get(name: string) { + get(name: string): StoreFunction { return this.store[name]; } register(fn: GeneratorFunction): void register(name: string, fn: GeneratorFunction): void - register(name: string | GeneratorFunction, fn?: GeneratorFunction) { + register(name: string | GeneratorFunction, fn?: GeneratorFunction): void { if (!fn) { if (typeof name === 'function') { // fn fn = name; diff --git a/lib/extend/helper.ts b/lib/extend/helper.ts index 7903557c97..f167282371 100644 --- a/lib/extend/helper.ts +++ b/lib/extend/helper.ts @@ -35,7 +35,7 @@ class Helper { * @param {String} name - The name of the helper plugin * @param {StoreFunction} fn - The helper plugin function */ - register(name: string, fn: StoreFunction) { + register(name: string, fn: StoreFunction): void { if (!name) throw new TypeError('name is required'); if (typeof fn !== 'function') throw new TypeError('fn must be a function'); diff --git a/lib/extend/injector.ts b/lib/extend/injector.ts index fd1d09acdd..edb4a45094 100644 --- a/lib/extend/injector.ts +++ b/lib/extend/injector.ts @@ -24,15 +24,15 @@ class Injector { this.cache = new Cache(); } - list() { + list(): Store { return this.store; } - get(entry: Entry, to = 'default') { + get(entry: Entry, to = 'default'): any[] { return Array.from(this.store[entry][to] || []); } - getText(entry: Entry, to = 'default') { + getText(entry: Entry, to = 'default'): string { const arr = this.get(entry, to); if (!arr || !arr.length) return ''; return arr.join(''); @@ -42,7 +42,7 @@ class Injector { return this.cache.apply(`${entry}-size`, Object.keys(this.store[entry]).length); } - register(entry: Entry, value: string | (() => string), to = 'default') { + register(entry: Entry, value: string | (() => string), to = 'default'): void { if (!entry) throw new TypeError('entry is required'); if (typeof value === 'function') value = value(); @@ -52,7 +52,7 @@ class Injector { entryMap[to] = valueSet; } - _getPageType(pageLocals) { + _getPageType(pageLocals): string { let currentType = 'default'; if (pageLocals.__index) currentType = 'home'; if (pageLocals.__post) currentType = 'post'; @@ -65,7 +65,7 @@ class Injector { return currentType; } - _injector(input, pattern, flag, isBegin = true, currentType) { + _injector(input: string, pattern: string | RegExp, flag: Entry, isBegin = true, currentType: string): string { if (input.includes(`hexo injector ${flag}`)) return input; const code = this.cache.apply(`${flag}-${currentType}-code`, () => { @@ -81,7 +81,7 @@ class Injector { return input.replace(pattern, str => { return isBegin ? str + code : code + str; }); } - exec(data, locals = { page: {} }) { + exec(data: string, locals = { page: {} }): string { const { page } = locals; const currentType = this._getPageType(page); diff --git a/lib/extend/migrator.ts b/lib/extend/migrator.ts index 2a149f191a..5ab0f7063e 100644 --- a/lib/extend/migrator.ts +++ b/lib/extend/migrator.ts @@ -14,15 +14,15 @@ class Migrator { this.store = {}; } - list() { + list(): Store { return this.store; } - get(name: string) { + get(name: string): StoreFunction { return this.store[name]; } - register(name: string, fn: StoreFunction) { + register(name: string, fn: StoreFunction): void { if (!name) throw new TypeError('name is required'); if (typeof fn !== 'function') throw new TypeError('fn must be a function'); diff --git a/lib/extend/processor.ts b/lib/extend/processor.ts index 0af6998494..ce73da1031 100644 --- a/lib/extend/processor.ts +++ b/lib/extend/processor.ts @@ -19,13 +19,13 @@ class Processor { this.store = []; } - list() { + list(): Store { return this.store; } register(fn: StoreFunction): void; register(pattern: patternType, fn: StoreFunction): void; - register(pattern: patternType | StoreFunction, fn?: StoreFunction) { + register(pattern: patternType | StoreFunction, fn?: StoreFunction): void { if (!fn) { if (typeof pattern === 'function') { fn = pattern; diff --git a/lib/extend/renderer.ts b/lib/extend/renderer.ts index 3dd0b42e84..15f90c6de6 100644 --- a/lib/extend/renderer.ts +++ b/lib/extend/renderer.ts @@ -57,25 +57,25 @@ class Renderer { this.storeSync = {}; } - list(sync: boolean) { + list(sync: boolean): Store | SyncStore { return sync ? this.storeSync : this.store; } - get(name: string, sync?: boolean) { + get(name: string, sync?: boolean): StoreSyncFunction | StoreFunction { const store = this[sync ? 'storeSync' : 'store']; return store[getExtname(name)] || store[name]; } - isRenderable(path: string) { + isRenderable(path: string): boolean { return Boolean(this.get(path)); } - isRenderableSync(path: string) { + isRenderableSync(path: string): boolean { return Boolean(this.get(path, true)); } - getOutput(path: string) { + getOutput(path: string): string { const renderer = this.get(path); return renderer ? renderer.output : ''; } diff --git a/lib/extend/syntax_highlight.ts b/lib/extend/syntax_highlight.ts index 096eb4c04c..c5cf28a95f 100644 --- a/lib/extend/syntax_highlight.ts +++ b/lib/extend/syntax_highlight.ts @@ -39,13 +39,13 @@ class SyntaxHighlight { this.store = {}; } - register(name: string, fn: StoreFunction) { + register(name: string, fn: StoreFunction): void { if (typeof fn !== 'function') throw new TypeError('fn must be a function'); this.store[name] = fn; } - query(name: string) { + query(name: string): StoreFunction { return name && this.store[name]; } diff --git a/lib/extend/tag.ts b/lib/extend/tag.ts index 7e8426840f..d44b5dd993 100644 --- a/lib/extend/tag.ts +++ b/lib/extend/tag.ts @@ -211,7 +211,7 @@ class Tag { register(name: string, fn: TagFunction): void register(name: string, fn: TagFunction, ends: boolean): void register(name: string, fn: TagFunction, options: RegisterOptions): void - register(name: string, fn: TagFunction, options?: RegisterOptions | boolean) { + register(name: string, fn: TagFunction, options?: RegisterOptions | boolean):void { if (!name) throw new TypeError('name is required'); if (typeof fn !== 'function') throw new TypeError('fn must be a function'); @@ -243,7 +243,7 @@ class Tag { this.env.addExtension(name, tag); } - unregister(name: string) { + unregister(name: string): void { if (!name) throw new TypeError('name is required'); const { env } = this; @@ -251,7 +251,7 @@ class Tag { if (env.hasExtension(name)) env.removeExtension(name); } - render(str: string, options: { source?: string } = {}, callback?: NodeJSLikeCallback) { + render(str: string, options: { source?: string } = {}, callback?: NodeJSLikeCallback): Promise { if (!callback && typeof options === 'function') { callback = options; options = {}; diff --git a/lib/hexo/index.ts b/lib/hexo/index.ts index 392407fb5d..d0ef00334e 100644 --- a/lib/hexo/index.ts +++ b/lib/hexo/index.ts @@ -401,7 +401,7 @@ class Hexo extends EventEmitter { return args.draft || args.drafts || this.config.render_drafts; } - load(callback: NodeJSLikeCallback) { + load(callback?: NodeJSLikeCallback) { return loadDatabase(this).then(() => { this.log.info('Start processing'); diff --git a/lib/plugins/console/clean.ts b/lib/plugins/console/clean.ts index 19061ee0fb..eea456a757 100644 --- a/lib/plugins/console/clean.ts +++ b/lib/plugins/console/clean.ts @@ -2,7 +2,7 @@ import Promise from 'bluebird'; import { exists, unlink, rmdir } from 'hexo-fs'; import type Hexo from '../../hexo'; -function cleanConsole() { +function cleanConsole(this: Hexo): Promise<[void, void, any]> { return Promise.all([ deleteDatabase(this), deletePublicDir(this), @@ -10,7 +10,7 @@ function cleanConsole() { ]); } -function deleteDatabase(ctx: Hexo) { +function deleteDatabase(ctx: Hexo): Promise { const dbPath = ctx.database.options.path; return exists(dbPath).then(exist => { @@ -22,7 +22,7 @@ function deleteDatabase(ctx: Hexo) { }); } -function deletePublicDir(ctx: Hexo) { +function deletePublicDir(ctx: Hexo): Promise { const publicDir = ctx.public_dir; return exists(publicDir).then(exist => { diff --git a/lib/plugins/console/config.ts b/lib/plugins/console/config.ts index dc546d961e..777bc4e1d0 100644 --- a/lib/plugins/console/config.ts +++ b/lib/plugins/console/config.ts @@ -2,8 +2,9 @@ import yaml from 'js-yaml'; import { exists, writeFile } from 'hexo-fs'; import { extname } from 'path'; import Promise from 'bluebird'; +import type Hexo from '../../hexo'; -function configConsole(args) { +function configConsole(this: Hexo, args): Promise { const key = args._[0]; let value = args._[1]; @@ -35,7 +36,7 @@ function configConsole(args) { }); } -function getProperty(obj: object, key: string) { +function getProperty(obj: object, key: string): any { const split = key.split('.'); let result = obj[split[0]]; @@ -46,7 +47,7 @@ function getProperty(obj: object, key: string) { return result; } -function setProperty(obj: object, key: string, value: any) { +function setProperty(obj: object, key: string, value: any): void { const split = key.split('.'); let cursor = obj; const lastKey = split.pop(); @@ -60,7 +61,7 @@ function setProperty(obj: object, key: string, value: any) { cursor[lastKey] = value; } -function castValue(value: string) { +function castValue(value: string): any { switch (value) { case 'true': return true; diff --git a/lib/plugins/console/deploy.ts b/lib/plugins/console/deploy.ts index 6da32c2e76..1da54819d4 100644 --- a/lib/plugins/console/deploy.ts +++ b/lib/plugins/console/deploy.ts @@ -1,7 +1,8 @@ import { exists } from 'hexo-fs'; import { underline, magenta } from 'picocolors'; +import type Hexo from '../../hexo'; -function deployConsole(args) { +function deployConsole(this: Hexo, args) { let config = this.config.deploy; const deployers = this.extend.deployer.list(); diff --git a/lib/plugins/console/generate.ts b/lib/plugins/console/generate.ts index 70836695d7..2b17697b49 100644 --- a/lib/plugins/console/generate.ts +++ b/lib/plugins/console/generate.ts @@ -59,7 +59,7 @@ class Generater { generatingFiles.delete(path); }); } - writeFile(path: string, force?: boolean) { + writeFile(path: string, force?: boolean): Promise { const { route, log } = this.context; const publicDir = this.context.public_dir; const Cache = this.context.model('Cache'); @@ -100,7 +100,7 @@ class Generater { }); }); } - deleteFile(path: string) { + deleteFile(path: string): Promise { const { log } = this.context; const publicDir = this.context.public_dir; const dest = join(publicDir, path); @@ -127,7 +127,7 @@ class Generater { return dataStream.pipe(new PassThrough()); } - firstGenerate() { + firstGenerate(): Promise { const { concurrency } = this; const { route, log } = this.context; const publicDir = this.context.public_dir; @@ -172,7 +172,7 @@ class Generater { log.info('%d files generated in %s', count, cyan(interval)); }); } - execWatch() { + execWatch(): Promise { const { route, log } = this.context; return this.context.watch().then(() => this.firstGenerate()).then(() => { log.info('Hexo is watching for file changes. Press Ctrl+C to exit.'); diff --git a/lib/plugins/console/list/category.ts b/lib/plugins/console/list/category.ts index 0044ec3a0e..819c6221dd 100644 --- a/lib/plugins/console/list/category.ts +++ b/lib/plugins/console/list/category.ts @@ -2,7 +2,7 @@ import { underline } from 'picocolors'; import table from 'text-table'; import { stringLength } from './common'; -function listCategory() { +function listCategory(): void { const categories = this.model('Category'); const data = categories.sort({name: 1}).map(cate => [cate.name, String(cate.length)]); diff --git a/lib/plugins/console/list/common.ts b/lib/plugins/console/list/common.ts index c79c390089..cc18685f5c 100644 --- a/lib/plugins/console/list/common.ts +++ b/lib/plugins/console/list/common.ts @@ -1,6 +1,6 @@ import strip from 'strip-ansi'; -export function stringLength(str: string) { +export function stringLength(str: string): number { str = strip(str); const len = str.length; diff --git a/lib/plugins/console/list/index.ts b/lib/plugins/console/list/index.ts index 7c3f084138..cd091ca272 100644 --- a/lib/plugins/console/list/index.ts +++ b/lib/plugins/console/list/index.ts @@ -4,6 +4,7 @@ import post from './post'; import route from './route'; import tag from './tag'; import category from './category'; +import type Hexo from '../../../hexo'; const store = { page, post, route, tag, category @@ -11,7 +12,7 @@ const store = { const alias = abbrev(Object.keys(store)); -function listConsole(args) { +function listConsole(this: Hexo, args) { const type = args._.shift(); // Display help message if user didn't input any arguments diff --git a/lib/plugins/console/list/page.ts b/lib/plugins/console/list/page.ts index d132231450..aa6582b599 100644 --- a/lib/plugins/console/list/page.ts +++ b/lib/plugins/console/list/page.ts @@ -2,7 +2,7 @@ import { magenta, underline, gray } from 'picocolors'; import table from 'text-table'; import { stringLength } from './common'; -function listPage() { +function listPage(): void { const Page = this.model('Page'); const data = Page.sort({date: 1}).map(page => { diff --git a/lib/plugins/console/list/post.ts b/lib/plugins/console/list/post.ts index e4dca1a578..d893eeac04 100644 --- a/lib/plugins/console/list/post.ts +++ b/lib/plugins/console/list/post.ts @@ -6,7 +6,7 @@ function mapName(item) { return item.name; } -function listPost() { +function listPost(): void { const Post = this.model('Post'); const data = Post.sort({published: -1, date: 1}).map(post => { diff --git a/lib/plugins/console/list/route.ts b/lib/plugins/console/list/route.ts index a29e4e4c54..f55cec3255 100644 --- a/lib/plugins/console/list/route.ts +++ b/lib/plugins/console/list/route.ts @@ -1,6 +1,6 @@ import archy from 'archy'; -function listRoute() { +function listRoute(): void { const routes = this.route.list().sort(); const tree = buildTree(routes); const nodes = buildNodes(tree); diff --git a/lib/plugins/console/list/tag.ts b/lib/plugins/console/list/tag.ts index 09c0a148df..258a0fd219 100644 --- a/lib/plugins/console/list/tag.ts +++ b/lib/plugins/console/list/tag.ts @@ -2,7 +2,7 @@ import { magenta, underline } from 'picocolors'; import table from 'text-table'; import { stringLength } from './common'; -function listTag() { +function listTag(): void { const Tag = this.model('Tag'); const data = Tag.sort({name: 1}).map(tag => [tag.name, String(tag.length), magenta(tag.path)]); diff --git a/lib/plugins/console/migrate.ts b/lib/plugins/console/migrate.ts index df341f6ed4..afe5dfb537 100644 --- a/lib/plugins/console/migrate.ts +++ b/lib/plugins/console/migrate.ts @@ -1,6 +1,7 @@ import { underline, magenta } from 'picocolors'; +import type Hexo from '../../hexo'; -function migrateConsole(args) { +function migrateConsole(this: Hexo, args) { // Display help message if user didn't input any arguments if (!args._.length) { return this.call('help', {_: ['migrate']}); diff --git a/lib/plugins/console/new.ts b/lib/plugins/console/new.ts index becbe1c625..64e3d83bd1 100644 --- a/lib/plugins/console/new.ts +++ b/lib/plugins/console/new.ts @@ -1,6 +1,7 @@ import tildify from 'tildify'; import { magenta } from 'picocolors'; import { basename } from 'path'; +import Hexo from '../../hexo'; const reservedKeys = { _: true, @@ -19,7 +20,7 @@ const reservedKeys = { silent: true }; -function newConsole(args) { +function newConsole(this: Hexo, args) { const path = args.p || args.path; let title; if (args._.length) { diff --git a/lib/plugins/console/publish.ts b/lib/plugins/console/publish.ts index f73d87ec55..6f97f9d175 100644 --- a/lib/plugins/console/publish.ts +++ b/lib/plugins/console/publish.ts @@ -1,7 +1,8 @@ import tildify from 'tildify'; import { magenta } from 'picocolors'; +import type Hexo from '../../hexo'; -function publishConsole(args) { +function publishConsole(this: Hexo, args) { // Display help message if user didn't input any arguments if (!args._.length) { return this.call('help', {_: ['publish']}); diff --git a/lib/plugins/console/render.ts b/lib/plugins/console/render.ts index 01cb57c581..c40230f728 100644 --- a/lib/plugins/console/render.ts +++ b/lib/plugins/console/render.ts @@ -3,8 +3,9 @@ import tildify from 'tildify'; import prettyHrtime from 'pretty-hrtime'; import { writeFile } from 'hexo-fs'; import { cyan, magenta } from 'picocolors'; +import type Hexo from '../../hexo'; -function renderConsole(args) { +function renderConsole(this: Hexo, args) { // Display help message if user didn't input any arguments if (!args._.length) { return this.call('help', {_: 'render'}); diff --git a/lib/plugins/filter/after_post_render/excerpt.ts b/lib/plugins/filter/after_post_render/excerpt.ts index ef1746a352..5694ce5dec 100644 --- a/lib/plugins/filter/after_post_render/excerpt.ts +++ b/lib/plugins/filter/after_post_render/excerpt.ts @@ -1,6 +1,6 @@ const rExcerpt = //i; -function excerptFilter(data) { +function excerptFilter(data): void { const { content } = data; if (typeof data.excerpt !== 'undefined') { diff --git a/lib/plugins/filter/after_post_render/external_link.ts b/lib/plugins/filter/after_post_render/external_link.ts index 0b116de8b4..dab35b3a9d 100644 --- a/lib/plugins/filter/after_post_render/external_link.ts +++ b/lib/plugins/filter/after_post_render/external_link.ts @@ -1,11 +1,12 @@ import { isExternalLink } from 'hexo-util'; +import type Hexo from '../../../hexo'; let EXTERNAL_LINK_POST_ENABLED = true; const rATag = /]+?\s+?)href=["']((?:https?:|\/\/)[^<>"']+)["'][^<>]*>/gi; const rTargetAttr = /target=/i; const rRelAttr = /rel=/i; const rRelStrAttr = /rel=["']([^<>"']*)["']/i; -function externalLinkFilter(data) { +function externalLinkFilter(this: Hexo, data): void { if (!EXTERNAL_LINK_POST_ENABLED) return; const { external_link, url } = this.config; @@ -16,7 +17,7 @@ function externalLinkFilter(data) { } data.content = data.content.replace(rATag, (str, href) => { - if (!isExternalLink(href, url, external_link.exclude) || rTargetAttr.test(str)) return str; + if (!isExternalLink(href, url, external_link.exclude as any) || rTargetAttr.test(str)) return str; if (rRelAttr.test(str)) { str = str.replace(rRelStrAttr, (relStr, rel) => { diff --git a/lib/plugins/filter/after_post_render/index.ts b/lib/plugins/filter/after_post_render/index.ts index 87bb8b433c..014eb0b6d0 100644 --- a/lib/plugins/filter/after_post_render/index.ts +++ b/lib/plugins/filter/after_post_render/index.ts @@ -1,4 +1,6 @@ -export = ctx => { +import type Hexo from '../../../hexo'; + +export = (ctx: Hexo) => { const { filter } = ctx.extend; filter.register('after_post_render', require('./external_link')); diff --git a/lib/plugins/filter/after_render/external_link.ts b/lib/plugins/filter/after_render/external_link.ts index a5d7218133..7a0168d842 100644 --- a/lib/plugins/filter/after_render/external_link.ts +++ b/lib/plugins/filter/after_render/external_link.ts @@ -1,4 +1,5 @@ import { isExternalLink } from 'hexo-util'; +import Hexo from '../../../hexo'; let EXTERNAL_LINK_SITE_ENABLED = true; const rATag = /]+?\s+?)href=["']((?:https?:|\/\/)[^<>"']+)["'][^<>]*>/gi; @@ -6,7 +7,7 @@ const rTargetAttr = /target=/i; const rRelAttr = /rel=/i; const rRelStrAttr = /rel=["']([^<>"']*)["']/i; -function externalLinkFilter(data) { +function externalLinkFilter(this: Hexo, data: string): string { if (!EXTERNAL_LINK_SITE_ENABLED) return; const { external_link, url } = this.config; @@ -17,7 +18,7 @@ function externalLinkFilter(data) { } return data.replace(rATag, (str, href) => { - if (!isExternalLink(href, url, external_link.exclude) || rTargetAttr.test(str)) return str; + if (!isExternalLink(href, url, external_link.exclude as any) || rTargetAttr.test(str)) return str; if (rRelAttr.test(str)) { str = str.replace(rRelStrAttr, (relStr, rel) => { diff --git a/lib/plugins/filter/after_render/index.ts b/lib/plugins/filter/after_render/index.ts index 509e95a0f8..a1eb862d5a 100644 --- a/lib/plugins/filter/after_render/index.ts +++ b/lib/plugins/filter/after_render/index.ts @@ -1,4 +1,6 @@ -export = ctx => { +import type Hexo from '../../../hexo'; + +export = (ctx: Hexo) => { const { filter } = ctx.extend; filter.register('after_render:html', require('./external_link')); diff --git a/lib/plugins/filter/after_render/meta_generator.ts b/lib/plugins/filter/after_render/meta_generator.ts index 4c04daee3a..7848f435ee 100644 --- a/lib/plugins/filter/after_render/meta_generator.ts +++ b/lib/plugins/filter/after_render/meta_generator.ts @@ -1,7 +1,9 @@ +import type Hexo from '../../../hexo'; + let NEED_INJECT = true; let META_GENERATOR_TAG; -function hexoMetaGeneratorInject(data) { +function hexoMetaGeneratorInject(this: Hexo, data: string): string { if (!NEED_INJECT) return; if (!this.config.meta_generator diff --git a/lib/plugins/filter/before_exit/index.ts b/lib/plugins/filter/before_exit/index.ts index ba3312749e..1443eed5cb 100644 --- a/lib/plugins/filter/before_exit/index.ts +++ b/lib/plugins/filter/before_exit/index.ts @@ -1,4 +1,6 @@ -export = ctx => { +import type Hexo from '../../../hexo'; + +export = (ctx: Hexo) => { const { filter } = ctx.extend; filter.register('before_exit', require('./save_database')); diff --git a/lib/plugins/filter/before_exit/save_database.ts b/lib/plugins/filter/before_exit/save_database.ts index 9aadce2ee8..9a853eb93e 100644 --- a/lib/plugins/filter/before_exit/save_database.ts +++ b/lib/plugins/filter/before_exit/save_database.ts @@ -1,4 +1,6 @@ -function saveDatabaseFilter() { +import type Hexo from '../../../hexo'; + +function saveDatabaseFilter(this: Hexo) { if (!this.env.init || !this._dbLoaded) return; return this.database.save().then(() => { diff --git a/lib/plugins/filter/before_generate/index.ts b/lib/plugins/filter/before_generate/index.ts index 288bb1651a..9c9565b9f4 100644 --- a/lib/plugins/filter/before_generate/index.ts +++ b/lib/plugins/filter/before_generate/index.ts @@ -1,4 +1,6 @@ -export = ctx => { +import type Hexo from '../../../hexo'; + +export = (ctx: Hexo) => { const { filter } = ctx.extend; filter.register('before_generate', require('./render_post')); diff --git a/lib/plugins/filter/before_generate/render_post.ts b/lib/plugins/filter/before_generate/render_post.ts index ae202e9477..1042def601 100644 --- a/lib/plugins/filter/before_generate/render_post.ts +++ b/lib/plugins/filter/before_generate/render_post.ts @@ -1,6 +1,7 @@ import Promise from 'bluebird'; +import type Hexo from '../../../hexo'; -function renderPostFilter(data) { +function renderPostFilter(this: Hexo, data): Promise<[any[], any[]]> { const renderPosts = model => { const posts = model.toArray().filter(post => post.content == null); diff --git a/lib/plugins/filter/before_post_render/backtick_code_block.ts b/lib/plugins/filter/before_post_render/backtick_code_block.ts index b9df3e60f2..7952169edd 100644 --- a/lib/plugins/filter/before_post_render/backtick_code_block.ts +++ b/lib/plugins/filter/before_post_render/backtick_code_block.ts @@ -1,3 +1,5 @@ +import type Hexo from '../../../hexo'; + const rBacktick = /^((?:[^\S\r\n]*>){0,3}[^\S\r\n]*)(`{3,}|~{3,})[^\S\r\n]*((?:.*?[^`\s])?)[^\S\r\n]*\n((?:[\s\S]*?\n)?)(?:(?:[^\S\r\n]*>){0,3}[^\S\r\n]*)\2[^\S\r\n]?(\n+|$)/gm; const rAllOptions = /([^\s]+)\s+(.+?)\s+(https?:\/\/\S+|\/\S+)\s*(.+)?/; const rLangCaption = /([^\s]+)\s*(.+)?/; @@ -11,8 +13,8 @@ interface Options { firstLineNumber?: string | number } -export = ctx => { - return function backtickCodeBlock(data) { +export = (ctx: Hexo) => { + return function backtickCodeBlock(data): void { const dataContent = data.content; if ((!dataContent.includes('```') && !dataContent.includes('~~~')) || !ctx.extend.highlight.query(ctx.config.syntax_highlighter)) return; diff --git a/lib/plugins/filter/before_post_render/index.ts b/lib/plugins/filter/before_post_render/index.ts index 07bde83da3..501b483df7 100644 --- a/lib/plugins/filter/before_post_render/index.ts +++ b/lib/plugins/filter/before_post_render/index.ts @@ -1,4 +1,6 @@ -export = ctx => { +import type Hexo from '../../../hexo'; + +export = (ctx: Hexo) => { const { filter } = ctx.extend; filter.register('before_post_render', require('./backtick_code_block')(ctx)); diff --git a/lib/plugins/filter/before_post_render/titlecase.ts b/lib/plugins/filter/before_post_render/titlecase.ts index 24e596ace8..bd149d3823 100644 --- a/lib/plugins/filter/before_post_render/titlecase.ts +++ b/lib/plugins/filter/before_post_render/titlecase.ts @@ -1,6 +1,6 @@ let titlecase; -function titlecaseFilter(data) { +function titlecaseFilter(data): void { if (!(typeof data.titlecase !== 'undefined' ? data.titlecase : this.config.titlecase) || !data.title) return; if (!titlecase) titlecase = require('titlecase'); diff --git a/lib/plugins/filter/new_post_path.ts b/lib/plugins/filter/new_post_path.ts index c06b946230..1eaa58c9f2 100644 --- a/lib/plugins/filter/new_post_path.ts +++ b/lib/plugins/filter/new_post_path.ts @@ -3,6 +3,7 @@ import moment from 'moment'; import Promise from 'bluebird'; import { createSha1Hash, Permalink } from 'hexo-util'; import { ensurePath } from 'hexo-fs'; +import type Hexo from '../../hexo'; let permalink; const reservedKeys = { @@ -22,7 +23,7 @@ interface Data { date?: Date; } -function newPostPathFilter(data: Data = {}, replace) { +function newPostPathFilter(this: Hexo, data: Data = {}, replace): Promise { const sourceDir = this.source_dir; const draftDir = join(sourceDir, '_drafts'); const postDir = join(sourceDir, '_posts'); diff --git a/lib/plugins/filter/post_permalink.ts b/lib/plugins/filter/post_permalink.ts index c715e67b5d..f066421330 100644 --- a/lib/plugins/filter/post_permalink.ts +++ b/lib/plugins/filter/post_permalink.ts @@ -1,8 +1,9 @@ import { createSha1Hash, Permalink, slugize } from 'hexo-util'; import { basename } from 'path'; +import type Hexo from '../../hexo'; let permalink; -function postPermalinkFilter(data) { +function postPermalinkFilter(this: Hexo, data) { const { config } = this; const { id, _id, slug, title, date, __permalink } = data; diff --git a/lib/plugins/filter/template_locals/i18n.ts b/lib/plugins/filter/template_locals/i18n.ts index 73367d8df0..a709fe8934 100644 --- a/lib/plugins/filter/template_locals/i18n.ts +++ b/lib/plugins/filter/template_locals/i18n.ts @@ -1,6 +1,7 @@ import { Pattern } from 'hexo-util'; +import type Hexo from '../../../hexo'; -function i18nLocalsFilter(locals) { +function i18nLocalsFilter(this: Hexo, locals): void { const { i18n } = this.theme; const { config } = this; const i18nDir = config.i18n_dir; diff --git a/lib/plugins/filter/template_locals/index.ts b/lib/plugins/filter/template_locals/index.ts index 663cb8ab3f..27dbf0caa4 100644 --- a/lib/plugins/filter/template_locals/index.ts +++ b/lib/plugins/filter/template_locals/index.ts @@ -1,4 +1,6 @@ -export = ctx => { +import type Hexo from '../../../hexo'; + +export = (ctx: Hexo) => { const { filter } = ctx.extend; filter.register('template_locals', require('./i18n')); diff --git a/lib/plugins/generator/asset.ts b/lib/plugins/generator/asset.ts index 5f869b3048..074f497620 100644 --- a/lib/plugins/generator/asset.ts +++ b/lib/plugins/generator/asset.ts @@ -47,7 +47,7 @@ const process = (name: string, ctx: Hexo) => { }); }; -function assetGenerator() { +function assetGenerator(this: Hexo): Promise { return Promise.all([ process('Asset', this), process('PostAsset', this) diff --git a/lib/types.ts b/lib/types.ts index caa69deadb..ea6313c253 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -1 +1 @@ -type NodeJSLikeCallback = (err: E, result?: R) => void \ No newline at end of file +type NodeJSLikeCallback = (err: E, result?: R) => void