From ebe92ba3966d5bd8860d719a32cc932a26ea7f4a Mon Sep 17 00:00:00 2001 From: Dimas Lanjaka Date: Wed, 11 Dec 2024 06:26:49 +0000 Subject: [PATCH] feat: bind hexo context to helper function callback (#5555) Co-authored-by: Uiolee <22849383+uiolee@users.noreply.github.com> --- lib/extend/helper.ts | 43 ++++++++++++++++++++-- lib/extend/processor.ts | 10 ++--- lib/plugins/filter/template_locals/i18n.ts | 2 +- 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/lib/extend/helper.ts b/lib/extend/helper.ts index f167282371..6bfe45be4d 100644 --- a/lib/extend/helper.ts +++ b/lib/extend/helper.ts @@ -1,12 +1,49 @@ +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 { - (...args: any[]): string; + (this: HexoContext, ...args: any[]): any; } interface Store { - [key: string]: StoreFunction + [key: string]: StoreFunction; } - class Helper { public store: Store; 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; diff --git a/lib/plugins/filter/template_locals/i18n.ts b/lib/plugins/filter/template_locals/i18n.ts index 87c505280b..7411a94b23 100644 --- a/lib/plugins/filter/template_locals/i18n.ts +++ b/lib/plugins/filter/template_locals/i18n.ts @@ -15,7 +15,7 @@ function i18nLocalsFilter(this: Hexo, locals: LocalsType): void { const pattern = new Pattern(`${i18nDir}/*path`); const data = pattern.match(locals.path); - if (data && data.lang && i18nLanguages.includes(data.lang)) { + if (data && 'lang' in data && i18nLanguages.includes(data.lang)) { lang = data.lang; page.canonical_path = data.path; } else {