From 790e234f3428b4fb9b0fff6aac7d85b4e81ed88e Mon Sep 17 00:00:00 2001 From: dimaslanjaka Date: Sat, 5 Oct 2024 10:54:33 +0700 Subject: [PATCH 1/6] feat: bind hexo context to helper function callback --- lib/extend/helper.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/extend/helper.ts b/lib/extend/helper.ts index f167282371..9fe13e9fed 100644 --- a/lib/extend/helper.ts +++ b/lib/extend/helper.ts @@ -1,12 +1,19 @@ +import Hexo from '../hexo'; +import { PageSchema } from '../types'; + +interface HexoContext extends Hexo { + // https://github.com/dimaslanjaka/hexo-renderers/blob/147340f6d03a8d3103e9589ddf86778ed7f9019b/src/helper/related-posts.ts#L106-L113 + page?: PageSchema; +} + interface StoreFunction { - (...args: any[]): string; + (this: HexoContext, ...args: any[]): string; } interface Store { - [key: string]: StoreFunction + [key: string]: StoreFunction; } - class Helper { public store: Store; From c089dfec9029c920032c66f72bf92733521d0498 Mon Sep 17 00:00:00 2001 From: dimaslanjaka Date: Sat, 5 Oct 2024 11:00:49 +0700 Subject: [PATCH 2/6] fix(TS2339): Property 'path' does not exist on type 'true | RegExpMatchArray | Record'. --- lib/plugins/filter/template_locals/i18n.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/plugins/filter/template_locals/i18n.ts b/lib/plugins/filter/template_locals/i18n.ts index 87c505280b..8528474007 100644 --- a/lib/plugins/filter/template_locals/i18n.ts +++ b/lib/plugins/filter/template_locals/i18n.ts @@ -13,9 +13,9 @@ function i18nLocalsFilter(this: Hexo, locals: LocalsType): void { if (!lang) { const pattern = new Pattern(`${i18nDir}/*path`); - const data = pattern.match(locals.path); + const data = pattern.match(locals.path) as Record; - if (data && data.lang && i18nLanguages.includes(data.lang)) { + if (data && typeof data.lang === 'string' && i18nLanguages.includes(data.lang)) { lang = data.lang; page.canonical_path = data.path; } else { From 85b66d4fe4f7a58969d3b4004fee51d95b10046a Mon Sep 17 00:00:00 2001 From: dimaslanjaka Date: Sat, 5 Oct 2024 11:02:21 +0700 Subject: [PATCH 3/6] fix(TS2322): Type '((str: string) => PatternMatchResult) | StoreFunction' is not assignable to type 'StoreFunction'. --- lib/extend/processor.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/extend/processor.ts b/lib/extend/processor.ts index ce73da1031..f38d3ba1cb 100644 --- a/lib/extend/processor.ts +++ b/lib/extend/processor.ts @@ -3,15 +3,15 @@ import { Pattern } from 'hexo-util'; import type File from '../box/file'; interface StoreFunction { - (file: File): any + (file: File | string): any; } type Store = { - pattern: Pattern; - process: StoreFunction - }[]; + pattern: Pattern; + process: StoreFunction; +}[]; -type patternType = Exclude[0], ((str: string) => string)>; +type patternType = Exclude[0], (str: string) => string>; class Processor { public store: Store; From dda472f10bf92fb2c6ec9fb387215f801565c698 Mon Sep 17 00:00:00 2001 From: dimaslanjaka Date: Sat, 5 Oct 2024 11:39:50 +0700 Subject: [PATCH 4/6] feat: shim hexo-util refer to doc https://hexo.io/api/helper#How-do-I-use-another-registered-helper-in-my-custom-helper --- lib/extend/helper.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lib/extend/helper.ts b/lib/extend/helper.ts index 9fe13e9fed..37ddb5ae0e 100644 --- a/lib/extend/helper.ts +++ b/lib/extend/helper.ts @@ -1,9 +1,39 @@ import Hexo from '../hexo'; import { PageSchema } from '../types'; +import * as hutil from 'hexo-util'; interface HexoContext extends Hexo { + // get current page information // https://github.com/dimaslanjaka/hexo-renderers/blob/147340f6d03a8d3103e9589ddf86778ed7f9019b/src/helper/related-posts.ts#L106-L113 page?: PageSchema; + + // hexo-util shims + url_for: typeof hutil.url_for; + full_url_for: typeof hutil.full_url_for; + relative_url: typeof hutil.relative_url; + slugize: typeof hutil.slugize; + escapeDiacritic: typeof hutil.escapeDiacritic; + escapeHTML: typeof hutil.escapeHTML; + unescapeHTML: typeof hutil.unescapeHTML; + encodeURL: typeof hutil.encodeURL; + decodeURL: typeof hutil.decodeURL; + escapeRegExp: typeof hutil.escapeRegExp; + stripHTML: typeof hutil.stripHTML; + stripIndent: typeof hutil.stripIndent; + hash: typeof hutil.hash; + createSha1Hash: typeof hutil.createSha1Hash; + highlight: typeof hutil.highlight; + prismHighlight: typeof hutil.prismHighlight; + tocObj: typeof hutil.tocObj; + wordWrap: typeof hutil.wordWrap; + prettyUrls: typeof hutil.prettyUrls; + isExternalLink: typeof hutil.isExternalLink; + gravatar: typeof hutil.gravatar; + htmlTag: typeof hutil.htmlTag; + truncate: typeof hutil.truncate; + spawn: typeof hutil.spawn; + camelCaseKeys: typeof hutil.camelCaseKeys; + deepMerge: typeof hutil.deepMerge; } interface StoreFunction { From 61f224aea843432fd7407059cc2456420f0e997b Mon Sep 17 00:00:00 2001 From: dimaslanjaka Date: Sat, 5 Oct 2024 12:26:06 +0700 Subject: [PATCH 5/6] fix(StoreFunction): generalize return type --- lib/extend/helper.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/extend/helper.ts b/lib/extend/helper.ts index 37ddb5ae0e..6bfe45be4d 100644 --- a/lib/extend/helper.ts +++ b/lib/extend/helper.ts @@ -37,7 +37,7 @@ interface HexoContext extends Hexo { } interface StoreFunction { - (this: HexoContext, ...args: any[]): string; + (this: HexoContext, ...args: any[]): any; } interface Store { From a35ed543570c4b2bda3cf752273aec6996d28bfc Mon Sep 17 00:00:00 2001 From: dimaslanjaka Date: Sun, 6 Oct 2024 07:30:36 +0700 Subject: [PATCH 6/6] chore(i18nLocalsFilter): cast `data` to any --- lib/plugins/filter/template_locals/i18n.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/plugins/filter/template_locals/i18n.ts b/lib/plugins/filter/template_locals/i18n.ts index 8528474007..53f030ce55 100644 --- a/lib/plugins/filter/template_locals/i18n.ts +++ b/lib/plugins/filter/template_locals/i18n.ts @@ -13,9 +13,9 @@ function i18nLocalsFilter(this: Hexo, locals: LocalsType): void { if (!lang) { const pattern = new Pattern(`${i18nDir}/*path`); - const data = pattern.match(locals.path) as Record; + const data = pattern.match(locals.path) as any; - if (data && typeof data.lang === 'string' && i18nLanguages.includes(data.lang)) { + if (data && 'lang' in data && i18nLanguages.includes(data.lang)) { lang = data.lang; page.canonical_path = data.path; } else {