Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add autotitle cache context #483

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading