Skip to content

Commit

Permalink
feat: add autotitle cache context (#483)
Browse files Browse the repository at this point in the history
  • Loading branch information
makamekm authored Aug 16, 2024
1 parent ae07df8 commit 3ce51f7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
23 changes: 17 additions & 6 deletions src/transform/plugins/links/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -180,6 +190,7 @@ function processLink(state: StateCore, tokens: Token[], idx: number, opts: ProcO
href,
currentPath,
log,
cache,
});
}

Expand Down
3 changes: 2 additions & 1 deletion src/transform/plugins/typings.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -8,6 +8,7 @@ export interface MarkdownItPluginOpts {
root: string;
rootPublicPath: string;
isLintRun: boolean;
cache?: CacheContext;
}

export type MarkdownItPluginCb<T extends {} = {}> = {
Expand Down
7 changes: 7 additions & 0 deletions src/transform/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, LanguageFn>;

export type Heading = {
Expand Down Expand Up @@ -49,6 +55,7 @@ export interface OptionsType {
transformLink?: (href: string) => string;
getPublicPath?: (options: OptionsType, href?: string) => string;
renderInline?: boolean;
cache?: CacheContext;
[x: string]: unknown;
}

Expand Down

0 comments on commit 3ce51f7

Please sign in to comment.