mirror of
https://github.com/microsoft/vscode.git
synced 2025-12-23 19:59:37 +00:00
Adopt prefix private with _ in markdown extension (#165088)
This commit is contained in:
@@ -46,37 +46,37 @@ const pluginSourceMap: MarkdownIt.PluginSimple = (md): void => {
|
||||
type MarkdownItConfig = Readonly<Required<Pick<MarkdownIt.Options, 'breaks' | 'linkify' | 'typographer'>>>;
|
||||
|
||||
class TokenCache {
|
||||
private cachedDocument?: {
|
||||
private _cachedDocument?: {
|
||||
readonly uri: vscode.Uri;
|
||||
readonly version: number;
|
||||
readonly config: MarkdownItConfig;
|
||||
};
|
||||
private tokens?: Token[];
|
||||
private _tokens?: Token[];
|
||||
|
||||
public tryGetCached(document: ITextDocument, config: MarkdownItConfig): Token[] | undefined {
|
||||
if (this.cachedDocument
|
||||
&& this.cachedDocument.uri.toString() === document.uri.toString()
|
||||
&& this.cachedDocument.version === document.version
|
||||
&& this.cachedDocument.config.breaks === config.breaks
|
||||
&& this.cachedDocument.config.linkify === config.linkify
|
||||
if (this._cachedDocument
|
||||
&& this._cachedDocument.uri.toString() === document.uri.toString()
|
||||
&& this._cachedDocument.version === document.version
|
||||
&& this._cachedDocument.config.breaks === config.breaks
|
||||
&& this._cachedDocument.config.linkify === config.linkify
|
||||
) {
|
||||
return this.tokens;
|
||||
return this._tokens;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
public update(document: ITextDocument, config: MarkdownItConfig, tokens: Token[]) {
|
||||
this.cachedDocument = {
|
||||
this._cachedDocument = {
|
||||
uri: document.uri,
|
||||
version: document.version,
|
||||
config,
|
||||
};
|
||||
this.tokens = tokens;
|
||||
this._tokens = tokens;
|
||||
}
|
||||
|
||||
public clean(): void {
|
||||
this.cachedDocument = undefined;
|
||||
this.tokens = undefined;
|
||||
this._cachedDocument = undefined;
|
||||
this._tokens = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ export interface IMdParser {
|
||||
|
||||
export class MarkdownItEngine implements IMdParser {
|
||||
|
||||
private md?: Promise<MarkdownIt>;
|
||||
private _md?: Promise<MarkdownIt>;
|
||||
|
||||
private _slugCount = new Map<string, number>();
|
||||
private _tokenCache = new TokenCache();
|
||||
@@ -107,26 +107,26 @@ export class MarkdownItEngine implements IMdParser {
|
||||
public readonly slugifier: Slugifier;
|
||||
|
||||
public constructor(
|
||||
private readonly contributionProvider: MarkdownContributionProvider,
|
||||
private readonly _contributionProvider: MarkdownContributionProvider,
|
||||
slugifier: Slugifier,
|
||||
private readonly logger: ILogger,
|
||||
private readonly _logger: ILogger,
|
||||
) {
|
||||
this.slugifier = slugifier;
|
||||
|
||||
contributionProvider.onContributionsChanged(() => {
|
||||
_contributionProvider.onContributionsChanged(() => {
|
||||
// Markdown plugin contributions may have changed
|
||||
this.md = undefined;
|
||||
this._md = undefined;
|
||||
});
|
||||
}
|
||||
|
||||
private async getEngine(config: MarkdownItConfig): Promise<MarkdownIt> {
|
||||
if (!this.md) {
|
||||
this.md = (async () => {
|
||||
private async _getEngine(config: MarkdownItConfig): Promise<MarkdownIt> {
|
||||
if (!this._md) {
|
||||
this._md = (async () => {
|
||||
const markdownIt = await import('markdown-it');
|
||||
let md: MarkdownIt = markdownIt(await getMarkdownOptions(() => md));
|
||||
md.linkify.set({ fuzzyLink: false });
|
||||
|
||||
for (const plugin of this.contributionProvider.contributions.markdownItPlugins.values()) {
|
||||
for (const plugin of this._contributionProvider.contributions.markdownItPlugins.values()) {
|
||||
try {
|
||||
md = (await plugin)(md);
|
||||
} catch (e) {
|
||||
@@ -149,60 +149,60 @@ export class MarkdownItEngine implements IMdParser {
|
||||
alt: ['paragraph', 'reference', 'blockquote', 'list']
|
||||
});
|
||||
|
||||
this.addImageRenderer(md);
|
||||
this.addFencedRenderer(md);
|
||||
this.addLinkNormalizer(md);
|
||||
this.addLinkValidator(md);
|
||||
this.addNamedHeaders(md);
|
||||
this.addLinkRenderer(md);
|
||||
this._addImageRenderer(md);
|
||||
this._addFencedRenderer(md);
|
||||
this._addLinkNormalizer(md);
|
||||
this._addLinkValidator(md);
|
||||
this._addNamedHeaders(md);
|
||||
this._addLinkRenderer(md);
|
||||
md.use(pluginSourceMap);
|
||||
return md;
|
||||
})();
|
||||
}
|
||||
|
||||
const md = await this.md!;
|
||||
const md = await this._md!;
|
||||
md.set(config);
|
||||
return md;
|
||||
}
|
||||
|
||||
public reloadPlugins() {
|
||||
this.md = undefined;
|
||||
this._md = undefined;
|
||||
}
|
||||
|
||||
private tokenizeDocument(
|
||||
private _tokenizeDocument(
|
||||
document: ITextDocument,
|
||||
config: MarkdownItConfig,
|
||||
engine: MarkdownIt
|
||||
): Token[] {
|
||||
const cached = this._tokenCache.tryGetCached(document, config);
|
||||
if (cached) {
|
||||
this.resetSlugCount();
|
||||
this._resetSlugCount();
|
||||
return cached;
|
||||
}
|
||||
|
||||
this.logger.verbose('MarkdownItEngine', `tokenizeDocument - ${document.uri}`);
|
||||
const tokens = this.tokenizeString(document.getText(), engine);
|
||||
this._logger.verbose('MarkdownItEngine', `tokenizeDocument - ${document.uri}`);
|
||||
const tokens = this._tokenizeString(document.getText(), engine);
|
||||
this._tokenCache.update(document, config, tokens);
|
||||
return tokens;
|
||||
}
|
||||
|
||||
private tokenizeString(text: string, engine: MarkdownIt) {
|
||||
this.resetSlugCount();
|
||||
private _tokenizeString(text: string, engine: MarkdownIt) {
|
||||
this._resetSlugCount();
|
||||
|
||||
return engine.parse(text.replace(UNICODE_NEWLINE_REGEX, ''), {});
|
||||
}
|
||||
|
||||
private resetSlugCount(): void {
|
||||
private _resetSlugCount(): void {
|
||||
this._slugCount = new Map<string, number>();
|
||||
}
|
||||
|
||||
public async render(input: ITextDocument | string, resourceProvider?: WebviewResourceProvider): Promise<RenderOutput> {
|
||||
const config = this.getConfig(typeof input === 'string' ? undefined : input.uri);
|
||||
const engine = await this.getEngine(config);
|
||||
const config = this._getConfig(typeof input === 'string' ? undefined : input.uri);
|
||||
const engine = await this._getEngine(config);
|
||||
|
||||
const tokens = typeof input === 'string'
|
||||
? this.tokenizeString(input, engine)
|
||||
: this.tokenizeDocument(input, config, engine);
|
||||
? this._tokenizeString(input, engine)
|
||||
: this._tokenizeDocument(input, config, engine);
|
||||
|
||||
const env: RenderEnv = {
|
||||
containingImages: new Set<string>(),
|
||||
@@ -222,16 +222,16 @@ export class MarkdownItEngine implements IMdParser {
|
||||
}
|
||||
|
||||
public async tokenize(document: ITextDocument): Promise<Token[]> {
|
||||
const config = this.getConfig(document.uri);
|
||||
const engine = await this.getEngine(config);
|
||||
return this.tokenizeDocument(document, config, engine);
|
||||
const config = this._getConfig(document.uri);
|
||||
const engine = await this._getEngine(config);
|
||||
return this._tokenizeDocument(document, config, engine);
|
||||
}
|
||||
|
||||
public cleanCache(): void {
|
||||
this._tokenCache.clean();
|
||||
}
|
||||
|
||||
private getConfig(resource?: vscode.Uri): MarkdownItConfig {
|
||||
private _getConfig(resource?: vscode.Uri): MarkdownItConfig {
|
||||
const config = vscode.workspace.getConfiguration('markdown', resource ?? null);
|
||||
return {
|
||||
breaks: config.get<boolean>('preview.breaks', false),
|
||||
@@ -240,7 +240,7 @@ export class MarkdownItEngine implements IMdParser {
|
||||
};
|
||||
}
|
||||
|
||||
private addImageRenderer(md: MarkdownIt): void {
|
||||
private _addImageRenderer(md: MarkdownIt): void {
|
||||
const original = md.renderer.rules.image;
|
||||
md.renderer.rules.image = (tokens: Token[], idx: number, options, env: RenderEnv, self) => {
|
||||
const token = tokens[idx];
|
||||
@@ -249,7 +249,7 @@ export class MarkdownItEngine implements IMdParser {
|
||||
env.containingImages?.add(src);
|
||||
|
||||
if (!token.attrGet('data-src')) {
|
||||
token.attrSet('src', this.toResourceUri(src, env.currentDocument, env.resourceProvider));
|
||||
token.attrSet('src', this._toResourceUri(src, env.currentDocument, env.resourceProvider));
|
||||
token.attrSet('data-src', src);
|
||||
}
|
||||
}
|
||||
@@ -262,11 +262,11 @@ export class MarkdownItEngine implements IMdParser {
|
||||
};
|
||||
}
|
||||
|
||||
private addFencedRenderer(md: MarkdownIt): void {
|
||||
private _addFencedRenderer(md: MarkdownIt): void {
|
||||
const original = md.renderer.rules['fenced'];
|
||||
md.renderer.rules['fenced'] = (tokens: Token[], idx: number, options, env, self) => {
|
||||
const token = tokens[idx];
|
||||
if (token.map && token.map.length) {
|
||||
if (token.map?.length) {
|
||||
token.attrJoin('class', 'hljs');
|
||||
}
|
||||
|
||||
@@ -278,7 +278,7 @@ export class MarkdownItEngine implements IMdParser {
|
||||
};
|
||||
}
|
||||
|
||||
private addLinkNormalizer(md: MarkdownIt): void {
|
||||
private _addLinkNormalizer(md: MarkdownIt): void {
|
||||
const normalizeLink = md.normalizeLink;
|
||||
md.normalizeLink = (link: string) => {
|
||||
try {
|
||||
@@ -294,7 +294,7 @@ export class MarkdownItEngine implements IMdParser {
|
||||
};
|
||||
}
|
||||
|
||||
private addLinkValidator(md: MarkdownIt): void {
|
||||
private _addLinkValidator(md: MarkdownIt): void {
|
||||
const validateLink = md.validateLink;
|
||||
md.validateLink = (link: string) => {
|
||||
return validateLink(link)
|
||||
@@ -304,7 +304,7 @@ export class MarkdownItEngine implements IMdParser {
|
||||
};
|
||||
}
|
||||
|
||||
private addNamedHeaders(md: MarkdownIt): void {
|
||||
private _addNamedHeaders(md: MarkdownIt): void {
|
||||
const original = md.renderer.rules.heading_open;
|
||||
md.renderer.rules.heading_open = (tokens: Token[], idx: number, options, env, self) => {
|
||||
const title = tokens[idx + 1].children!.reduce<string>((acc, t) => acc + t.content, '');
|
||||
@@ -328,7 +328,7 @@ export class MarkdownItEngine implements IMdParser {
|
||||
};
|
||||
}
|
||||
|
||||
private addLinkRenderer(md: MarkdownIt): void {
|
||||
private _addLinkRenderer(md: MarkdownIt): void {
|
||||
const original = md.renderer.rules.link_open;
|
||||
|
||||
md.renderer.rules.link_open = (tokens: Token[], idx: number, options, env, self) => {
|
||||
@@ -346,7 +346,7 @@ export class MarkdownItEngine implements IMdParser {
|
||||
};
|
||||
}
|
||||
|
||||
private toResourceUri(href: string, currentDocument: vscode.Uri | undefined, resourceProvider: WebviewResourceProvider | undefined): string {
|
||||
private _toResourceUri(href: string, currentDocument: vscode.Uri | undefined, resourceProvider: WebviewResourceProvider | undefined): string {
|
||||
try {
|
||||
// Support file:// links
|
||||
if (isOfScheme(Schemes.file, href)) {
|
||||
|
||||
Reference in New Issue
Block a user