Skip to content

Commit

Permalink
Improve parsing inline code
Browse files Browse the repository at this point in the history
  • Loading branch information
thetarnav committed Feb 28, 2024
1 parent 2f74f99 commit 2444d89
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 41 deletions.
75 changes: 36 additions & 39 deletions mds/mds.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,18 @@ export function parser_write(p, chunk) {
continue
}

/* `Code Inline` */
if ('`' === p.pending &&
"\n"!== char &&
'`' !== char
) {
parser_add_token(p, PARAGRAPH)
parser_add_text(p)
parser_add_token(p, CODE_INLINE)
p.text = char
continue
}

parser_add_token(p, PARAGRAPH)
p.text = p.pending
p.pending = char
Expand Down Expand Up @@ -242,38 +254,17 @@ export function parser_write(p, chunk) {
}
}
case CODE_INLINE: {
if ("\n" === p.pending) {
parser_add_text(p)

switch (char) {
case '\n':
while (p.len > 0) parser_end_token(p)
continue
case '`':
p.renderer.add_text('\n', p.renderer.data)
parser_end_token(p)
continue
default:
p.renderer.add_text('\n', p.renderer.data)
continue
}
}

switch (char) {
case '\n':
p.pending = "\n"
if ('\n' === char) {
p.pending = char
continue
case '`':
}
if ("`" === char) {
p.text += p.pending
p.pending = ""
parser_add_text(p)
parser_end_token(p)
continue
default:
p.text += p.pending + char
p.pending = ""
continue
}
break
}
case STRONG_AST:
case STRONG_UND: {
Expand Down Expand Up @@ -377,6 +368,24 @@ export function parser_write(p, chunk) {
break
}

/* Newline */
if ('\n' === p.pending[0]) {
parser_add_text(p)
if ('\n' === char) {
while (p.len > 0) parser_end_token(p)
} else {
p.renderer.add_text('\n', p.renderer.data)
p.pending = char
}
continue
}

if (in_token === CODE_INLINE) {
p.text += p.pending + char
p.pending = ""
continue
}

/*
Escape character
*/
Expand All @@ -396,18 +405,6 @@ export function parser_write(p, chunk) {
continue
}

/* Newline */
if ('\n' === p.pending[0]) {
parser_add_text(p)
if ('\n' === char) {
while (p.len > 0) parser_end_token(p)
} else {
p.renderer.add_text('\n', p.renderer.data)
p.pending = char
}
continue
}

if (in_token === IMAGE) {
p.text += p.pending
p.pending = char
Expand All @@ -421,7 +418,7 @@ export function parser_write(p, chunk) {
) {
parser_add_text(p)
parser_add_token(p, CODE_INLINE)
p.pending = char
p.text = char
continue
}

Expand Down
26 changes: 24 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,12 @@ test_single_write("Line Breaks",
)

test_single_write("Line Breaks with Italic",
"*" + content_1 + "\n" + content_2 + "*",
"*a\nb*",
[{
type : mds.Token_Type.Paragraph,
children: [{
type : mds.Token_Type.Italic_Ast,
children: [content_1, "\n", content_2]
children: ["a", "\n", "b"]
}],
}]
)
Expand Down Expand Up @@ -185,6 +185,28 @@ test_single_write("Paragraph with Italic",
}]
)

test_single_write("Code Inline",
"`a`",
[{
type : mds.Token_Type.Paragraph,
children: [{
type : mds.Token_Type.Code_Inline,
children: ["a"]
}],
}]
)

test_single_write("Code with line break",
"`a\nb`",
[{
type : mds.Token_Type.Paragraph,
children: [{
type : mds.Token_Type.Code_Inline,
children: ["a", "\n", "b"]
}],
}]
)

test_single_write("Empty Code_Block",
"```\n```",
[{
Expand Down

0 comments on commit 2444d89

Please sign in to comment.