From 74f5d6be1b2e756e01b9254d4afd8c4ff30022eb Mon Sep 17 00:00:00 2001 From: Maxim Karpov Date: Wed, 14 Aug 2024 15:40:38 +0300 Subject: [PATCH] feat: add autotitle cache context --- src/transform/plugins/links/index.ts | 23 +++++++++++++++++------ src/transform/plugins/typings.ts | 3 ++- src/transform/typings.ts | 7 +++++++ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/transform/plugins/links/index.ts b/src/transform/plugins/links/index.ts index 42daa765..4501762f 100644 --- a/src/transform/plugins/links/index.ts +++ b/src/transform/plugins/links/index.ts @@ -11,9 +11,9 @@ import { import {getFileTokens, isFileExists} from '../../utilsFS'; import Token from 'markdown-it/lib/token'; import {Logger} from 'src/transform/log'; +import {CacheContext, StateCore} from '../../typings'; import {MarkdownItPluginCb, MarkdownItPluginOpts} from '../typings'; import path, {isAbsolute, parse, relative, resolve} from 'path'; -import {StateCore} from 'src/transform/typings'; function getTitleFromTokens(tokens: Token[]) { let title = ''; @@ -46,13 +46,12 @@ type Options = { href: string; currentPath: string; log: Logger; + cache?: CacheContext; }; -const addTitle = (options: Options) => { - const {hash, file, state, opts, isEmptyLink, tokens, idx, nextToken, href, currentPath, log} = - options; +const getTitle = (id: string | null, options: Options) => { + const {file, state, opts} = options; - const id = hash && hash.slice(1); const fileTokens = getFileTokens(file, state, { ...opts, disableLint: true, @@ -61,7 +60,17 @@ const addTitle = (options: Options) => { inheritVars: false, }); const sourceTokens = id ? findBlockTokens(fileTokens, id) : fileTokens; - const title = getTitleFromTokens(sourceTokens); + return getTitleFromTokens(sourceTokens); +}; + +const addTitle = (options: Options) => { + const {hash, state, isEmptyLink, tokens, idx, nextToken, href, currentPath, log, cache} = + options; + + const id = hash && hash.slice(1); + const key = [id, path].join('::'); + const title = cache?.get(key) ?? getTitle(id, options); + cache?.set(key, title); if (title) { let textToken; @@ -109,6 +118,7 @@ function processLink(state: StateCore, tokens: Token[], idx: number, opts: ProcO needSkipLinkFn, log, getPublicPath = getDefaultPublicPath, + cache, } = opts; const currentPath = state.env.path || startPath; @@ -180,6 +190,7 @@ function processLink(state: StateCore, tokens: Token[], idx: number, opts: ProcO href, currentPath, log, + cache, }); } diff --git a/src/transform/plugins/typings.ts b/src/transform/plugins/typings.ts index 94c6f833..1c323c89 100644 --- a/src/transform/plugins/typings.ts +++ b/src/transform/plugins/typings.ts @@ -1,5 +1,5 @@ import type {Logger} from '../log'; -import type {MarkdownIt} from '../typings'; +import type {CacheContext, MarkdownIt} from '../typings'; export interface MarkdownItPluginOpts { path: string; @@ -8,6 +8,7 @@ export interface MarkdownItPluginOpts { root: string; rootPublicPath: string; isLintRun: boolean; + cache?: CacheContext; } export type MarkdownItPluginCb = { diff --git a/src/transform/typings.ts b/src/transform/typings.ts index 79ac76db..d6359866 100644 --- a/src/transform/typings.ts +++ b/src/transform/typings.ts @@ -10,10 +10,16 @@ export interface MarkdownIt extends DefaultMarkdownIt { assets?: string[]; meta?: string[]; } + export interface StateCore extends DefaultStateCore { md: MarkdownIt; } +export interface CacheContext { + get(key: string): string | null | undefined; + set(key: string, value: string | null | undefined): void; +} + export type HighlightLangMap = Record; export type Heading = { @@ -49,6 +55,7 @@ export interface OptionsType { transformLink?: (href: string) => string; getPublicPath?: (options: OptionsType, href?: string) => string; renderInline?: boolean; + cache?: CacheContext; [x: string]: unknown; }