From ba56ce9ba0fa2c55eb34cf9fee7649409a26a859 Mon Sep 17 00:00:00 2001 From: Parajuli Kiran Date: Mon, 13 Nov 2023 17:22:54 +0545 Subject: [PATCH] - modify shrunk body of list - manage context for lexer index - remove static numbers from test function of code block Signed-off-by: Parajuli Kiran --- lib/lexer/index.js | 70 ++++++++++++++++++++------------------ lib/tokenizer/codeblock.js | 8 ++--- lib/tokenizer/list.js | 16 +++------ package.json | 2 +- 4 files changed, 46 insertions(+), 50 deletions(-) diff --git a/lib/lexer/index.js b/lib/lexer/index.js index 7dc3417..ed2760a 100644 --- a/lib/lexer/index.js +++ b/lib/lexer/index.js @@ -21,6 +21,7 @@ class Lexer { #frontMatter #config #indentObj + #context constructor(lines, { from = null, config= {} } = {}) { this.#cursor = 0 @@ -36,38 +37,60 @@ class Lexer { ) } - #runCurrLineLexer() { - const context = { + run() { + this.#skipFrontMatter() + this.#checkForLinkRefs() + + for (this.#cursor = 0; this.#cursor < this.#lines.length; this.#cursor++) { + this.#runPrep() + this.#runCurrLineLexer() + } + + return this.#lexerData + } + + #runPrep() { + this.#currLine = this.#lines[this.#cursor] + this.#nextLine = this.#lines[this.#cursor + 1] + this.#lexerLengthBefore = this.#lexerData.length + this.#lastLexerItem = this.#lexerData[this.#lexerLengthBefore - 1] || null + this.#currLineRawIndent = this.#indentObj.raw(this.#currLine) + this.#currLineIndent = this.#indentObj.calc(this.#currLine) + + this.#context = { line: this.#currLine, nextLine: this.#nextLine, lines: this.#lines, cursor: this.#cursor, indent: this.#currLineIndent, lastLexer: this.#lastLexerItem, - fromToken: this.#fromToken + fromToken: this.#fromToken, + indentObj: this.#indentObj } + } - if (Newline.test(context)) return this.#runNewLineLexer() + #runCurrLineLexer() { + if (Newline.test(this.#context)) return this.#runNewLineLexer() - if (Heading.test(context)) return this.#runHeadingLexer() + if (Heading.test(this.#context)) return this.#runHeadingLexer() - if (HrLine.test(context)) return this.#runHrLineLexer() + if (HrLine.test(this.#context)) return this.#runHrLineLexer() - if (Comment.test(context)) return this.#runCommentLexer() + if (Comment.test(this.#context)) return this.#runCommentLexer() - if (Image.test(context)) return this.#runImageLexer() + if (Image.test(this.#context)) return this.#runImageLexer() - if (Quote.test(context)) return this.#runQuoteLexer() + if (Quote.test(this.#context)) return this.#runQuoteLexer() - if (List.test(context)) return this.#runListLexer() + if (List.test(this.#context)) return this.#runListLexer() - if (Table.test({ ...context, indentObj: this.#indentObj })) { + if (Table.test(this.#context)) { return this.#runTableLexer() } - if (HTML.testBlock(context)) return this.#runHTMLLexer() + if (HTML.testBlock(this.#context)) return this.#runHTMLLexer() - if (CodeBlock.test(context)) return this.#runCodeBlockLexer() + if (CodeBlock.test(this.#context)) return this.#runCodeBlockLexer() // otherwise, it is a paragraph return this.#runParagraphLexer() @@ -274,15 +297,6 @@ class Lexer { } } - #runPrep() { - this.#currLine = this.#lines[this.#cursor] - this.#nextLine = this.#lines[this.#cursor + 1] - this.#lexerLengthBefore = this.#lexerData.length - this.#lastLexerItem = this.#lexerData[this.#lexerLengthBefore - 1] || null - this.#currLineRawIndent = this.#indentObj.raw(this.#currLine) - this.#currLineIndent = this.#indentObj.calc(this.#currLine) - } - #skipFrontMatter() { if (FrontMatter.test(this.#lines)) { this.#frontMatter = new FrontMatter(this.#lines) @@ -299,18 +313,6 @@ class Lexer { return {} } } - - run() { - this.#skipFrontMatter() - this.#checkForLinkRefs() - - for (this.#cursor = 0; this.#cursor < this.#lines.length; this.#cursor++) { - this.#runPrep() - this.#runCurrLineLexer() - } - - return this.#lexerData - } } export default Lexer diff --git a/lib/tokenizer/codeblock.js b/lib/tokenizer/codeblock.js index 50e3216..ea02bb6 100644 --- a/lib/tokenizer/codeblock.js +++ b/lib/tokenizer/codeblock.js @@ -22,10 +22,10 @@ class CodeBlock { * * @returns {boolean} */ - static test({ line, indent, fromToken, lastLexer }) { + static test({ line, indent, fromToken, lastLexer, indentObj }) { if ( !fromToken && - indent >= 4 && + indent >= indentObj.indentSize && (!lastLexer || lastLexer.type === TOKENS.NEW_LINE) ) { return true @@ -34,7 +34,7 @@ class CodeBlock { // under deep condition inside list item // cb needs to be indented at least twice if ( - fromToken === TOKENS.LIST_ITEM && indent >= 8 && + fromToken === TOKENS.LIST_ITEM && indent >= (indentObj.indentSize * 2) && (!lastLexer || lastLexer?.type === TOKENS.NEW_LINE) ) { return true @@ -45,7 +45,7 @@ class CodeBlock { // cb needs regular indentation if ( - fromToken === TOKENS.QUOTE && indent >= 4 && + fromToken === TOKENS.QUOTE && indent >= indentObj.indentSize && (!lastLexer || lastLexer?.type === TOKENS.NEW_LINE) ) { return true diff --git a/lib/tokenizer/list.js b/lib/tokenizer/list.js index f526d7a..eb99537 100644 --- a/lib/tokenizer/list.js +++ b/lib/tokenizer/list.js @@ -146,17 +146,11 @@ class List { * Shrinks raw body for the list item */ #shrinkBody() { - for (let index = 0; index < this.#body.length; index++) { - const line = this.#body[index] - if (index === 0) { - if (this.#isEmpty) { - this.#shrunkBody.push("") - } else { - this.#shrunkBody.push(this.#match.value) - } - } else { - this.#shrunkBody.push(line) - } + this.#shrunkBody = [...this.#body] + if (this.#isEmpty) { + this.#shrunkBody[0] = "" + } else { + this.#shrunkBody[0] = this.#match.value } } diff --git a/package.json b/package.json index d3cb742..af1ae36 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "htmlmark", - "version": "0.2.0", + "version": "0.2.1", "description": "Lightweight markdown parser", "main": "dist/mdp.cjs", "module": "dist/mdp.esm.js",