* Lexing/Compiling
(src: string, tokens: Token[] = [])
| 301 | * Lexing/Compiling |
| 302 | */ |
| 303 | inlineTokens(src: string, tokens: Token[] = []): Token[] { |
| 304 | this.tokenizer.lexer = this; |
| 305 | // String with links masked to avoid interference with em and strong |
| 306 | let maskedSrc = src; |
| 307 | let match: RegExpExecArray | null = null; |
| 308 | |
| 309 | // Mask out reflinks |
| 310 | if (this.tokens.links) { |
| 311 | const links = Object.keys(this.tokens.links); |
| 312 | if (links.length > 0) { |
| 313 | while ((match = this.tokenizer.rules.inline.reflinkSearch.exec(maskedSrc)) !== null) { |
| 314 | if (links.includes(match[0].slice(match[0].lastIndexOf('[') + 1, -1))) { |
| 315 | maskedSrc = maskedSrc.slice(0, match.index) |
| 316 | + '[' + 'a'.repeat(match[0].length - 2) + ']' |
| 317 | + maskedSrc.slice(this.tokenizer.rules.inline.reflinkSearch.lastIndex); |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | // Mask out escaped characters |
| 324 | while ((match = this.tokenizer.rules.inline.anyPunctuation.exec(maskedSrc)) !== null) { |
| 325 | maskedSrc = maskedSrc.slice(0, match.index) + '++' + maskedSrc.slice(this.tokenizer.rules.inline.anyPunctuation.lastIndex); |
| 326 | } |
| 327 | |
| 328 | // Mask out other blocks |
| 329 | let offset; |
| 330 | while ((match = this.tokenizer.rules.inline.blockSkip.exec(maskedSrc)) !== null) { |
| 331 | offset = match[2] ? match[2].length : 0; |
| 332 | maskedSrc = maskedSrc.slice(0, match.index + offset) + '[' + 'a'.repeat(match[0].length - offset - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.blockSkip.lastIndex); |
| 333 | } |
| 334 | |
| 335 | // Mask out blocks from extensions |
| 336 | maskedSrc = this.options.hooks?.emStrongMask?.call({ lexer: this }, maskedSrc) ?? maskedSrc; |
| 337 | |
| 338 | let keepPrevChar = false; |
| 339 | let prevChar = ''; |
| 340 | let srcLength = Infinity; |
| 341 | while (src) { |
| 342 | if (src.length < srcLength) { |
| 343 | srcLength = src.length; |
| 344 | } else { |
| 345 | this.infiniteLoopError(src.charCodeAt(0)); |
| 346 | break; |
| 347 | } |
| 348 | |
| 349 | if (!keepPrevChar) { |
| 350 | prevChar = ''; |
| 351 | } |
| 352 | keepPrevChar = false; |
| 353 | |
| 354 | let token: Tokens.Generic | undefined; |
| 355 | |
| 356 | // extensions |
| 357 | if (this.options.extensions?.inline?.some((extTokenizer) => { |
| 358 | if (token = extTokenizer.call({ lexer: this }, src, tokens)) { |
| 359 | src = src.substring(token.raw.length); |
| 360 | tokens.push(token); |