Skip to content

Commit

Permalink
- modify shrunk body of list
Browse files Browse the repository at this point in the history
- manage context for lexer index
- remove static numbers from test function of code block

Signed-off-by: Parajuli Kiran <[email protected]>
  • Loading branch information
kiranparajuli589 committed Nov 13, 2023
1 parent e75e23a commit ba56ce9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 50 deletions.
70 changes: 36 additions & 34 deletions lib/lexer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Lexer {
#frontMatter
#config
#indentObj
#context

constructor(lines, { from = null, config= {} } = {}) {
this.#cursor = 0
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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
8 changes: 4 additions & 4 deletions lib/tokenizer/codeblock.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
16 changes: 5 additions & 11 deletions lib/tokenizer/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit ba56ce9

Please sign in to comment.