Skip to content
Closed
Changes from 1 commit
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
31 changes: 23 additions & 8 deletions src/Lexer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { _Tokenizer } from './Tokenizer.ts';
import { _defaults } from './defaults.ts';
import { other, block, inline } from './rules.ts';
import type { Token, TokensList, Tokens } from './Tokens.ts';
import type { Links, Token, TokensList, Tokens } from './Tokens.ts';
import type { MarkedOptions } from './MarkedOptions.ts';

/**
Expand All @@ -19,7 +19,7 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {
public inlineQueue: { src: string, tokens: Token[] }[];

private tokenizer: _Tokenizer<ParserOutput, RendererOutput>;

private linksForInline: Links | null = null;
Comment thread
UziTech marked this conversation as resolved.
Outdated
constructor(options?: MarkedOptions<ParserOutput, RendererOutput>) {
// TokenList cannot be created in one go
this.tokens = [] as unknown as TokensList;
Expand Down Expand Up @@ -90,9 +90,16 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {

this.blockTokens(src, this.tokens);

for (let i = 0; i < this.inlineQueue.length; i++) {
const next = this.inlineQueue[i];
this.inlineTokens(next.src, next.tokens);
if (Object.keys(this.tokens.links).length > 0) {
this.linksForInline = this.tokens.links;
}
try {
for (let i = 0; i < this.inlineQueue.length; i++) {
const next = this.inlineQueue[i];
this.inlineTokens(next.src, next.tokens);
}
} finally {
this.linksForInline = null;
}
this.inlineQueue = [];

Expand Down Expand Up @@ -307,12 +314,20 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {

// Mask out reflinks
if (this.tokens.links) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should still check if (this.tokens.links && Object.entries(this.tokens.links).length > 0) to prevent checking if we don't need to

const links = Object.keys(this.tokens.links);
if (links.length > 0) {
const linksForInline = this.linksForInline;
if (linksForInline) {
maskedSrc = maskedSrc.replace(this.tokenizer.rules.inline.reflinkSearch, match0 =>
links.includes(match0.slice(match0.lastIndexOf('[') + 1, -1))
Object.hasOwn(linksForInline, match0.slice(match0.lastIndexOf('[') + 1, -1))
? '[' + 'a'.repeat(match0.length - 2) + ']'
: match0);
} else {
const links = Object.keys(this.tokens.links);
Comment thread
UziTech marked this conversation as resolved.
Outdated
if (links.length > 0) {
maskedSrc = maskedSrc.replace(this.tokenizer.rules.inline.reflinkSearch, match0 =>
links.includes(match0.slice(match0.lastIndexOf('[') + 1, -1))
? '[' + 'a'.repeat(match0.length - 2) + ']'
: match0);
}
}
}

Expand Down