Skip to content

Commit

Permalink
More switches
Browse files Browse the repository at this point in the history
  • Loading branch information
thetarnav committed Feb 28, 2024
1 parent 2131a9e commit ca2e98d
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 111 deletions.
155 changes: 77 additions & 78 deletions mds/mds.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,9 @@ export function parser_write(p, chunk) {
p.pending = ""
}
continue
case 0: /* cannot end */
case 0: /* can't end */
console.assert(p.pending.length === 0, "Has pending text but cannot end")

if ('\n' === char) {
p.code_block = 1
p.pending = char
Expand Down Expand Up @@ -368,28 +368,27 @@ export function parser_write(p, chunk) {
break
}

/*
Common checks
*/
switch (p.pending) {
/* Newline */
if ('\n' === p.pending[0]) {
case "\n":
parser_add_text(p)
/* Paragraph */
if ('\n' === char) {
while (p.len > 0) parser_end_token(p)
} else {
}
/* Line break */
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 */
case "\\":
if (in_token === CODE_INLINE) break

/*
Escape character
*/
if ("\\" === p.pending) {
if ('\n' === char) {
// Escaped newline has the same affect as unescaped one
p.pending = char
Expand All @@ -403,83 +402,83 @@ export function parser_write(p, chunk) {
: char
}
continue
}

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

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

if (in_token ^ ASTERISK) {
if ("*" === p.pending) {
/* **Strong** */
if ('*' === char) {
parser_add_text(p)
parser_add_token(p, STRONG_AST)
}
/* *Em* */
else {
parser_add_text(p)
parser_add_token(p, ITALIC_AST)
p.pending = char
}
continue
}
}

if (in_token ^ UNDERSCORE) {
if ("_" === p.pending) {
/* __Strong__ */
if ('_' === char) {
parser_add_text(p)
parser_add_token(p, STRONG_UND)
}
/* _Em_ */
else {
parser_add_text(p)
parser_add_token(p, ITALIC_UND)
p.pending = char
}
case "`":
if (!(in_token & (CODE_INLINE | IMAGE)) &&
"\n"!== char &&
'`' !== char
) {
parser_add_text(p)
parser_add_token(p, CODE_INLINE)
p.text = char
continue
}
}
break
case "*":
if (in_token & (CODE_INLINE | ASTERISK | IMAGE)) break

/* [Link](url) */
if (in_token !== LINK &&
"[" === p.pending &&
"\n"!== char &&
"]" !== char
) {
parser_add_text(p)
parser_add_token(p, LINK)
p.pending = char
/* **Strong** */
if ('*' === char) {
parser_add_token(p, STRONG_AST)
}
/* *Em* */
else {
parser_add_token(p, ITALIC_AST)
p.pending = char
}
continue
}
case "_":
if (in_token & (CODE_INLINE | UNDERSCORE | IMAGE)) break

/* ![Image](url) */
if ("![" === pending_with_char) {
parser_add_text(p)
parser_add_token(p, IMAGE)
/* __Strong__ */
if ('_' === char) {
parser_add_token(p, STRONG_UND)
}
/* _Em_ */
else {
parser_add_token(p, ITALIC_UND)
p.pending = char
}
continue
/* [Image](url) */
case "[":
if (!(in_token & (CODE_INLINE | IMAGE | LINK)) &&
"\n"!== char &&
"]" !== char
) {
parser_add_text(p)
parser_add_token(p, LINK)
p.pending = char
continue
}
break
/* ![Image](url) */
case "!":
if (!(in_token & (CODE_INLINE | IMAGE)) &&
"[" === char
) {
parser_add_text(p)
parser_add_token(p, IMAGE)
continue
}
break
}

/*
No check hit
*/
p.text += p.pending
p.pending = char
switch (in_token) {
case CODE_INLINE:
p.text += p.pending + char
p.pending = ""
break
default:
p.text += p.pending
p.pending = char
break
}
}

parser_add_text(p)
Expand Down
63 changes: 30 additions & 33 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,6 @@ function test_single_write(title, markdown, expected_children) {
})
}

const content_1 = "Hello, World!"
const content_2 = "Goodbye, World!"

for (let level = 1; level <= 6; level += 1) {

/** @type {mds.Token_Type} */
Expand All @@ -117,30 +114,30 @@ for (let level = 1; level <= 6; level += 1) {
}

test_single_write(`Heading_${level}`,
"#".repeat(level) + " " + content_1,
"#".repeat(level) + " " + "foo",
[{
type : heading_type,
children: [content_1]
children: ["foo"]
}]
)

test_single_write(`Heading_${level} with Line Italic`,
"#".repeat(level) + " " + content_1 + " *" + content_2 + "*",
"#".repeat(level) + " foo *bar*",
[{
type : heading_type,
children: [content_1 + " ", {
children: ["foo ", {
type : mds.Token_Type.Italic_Ast,
children: [content_2]
children: ["bar"]
}]
}]
)
}

test_single_write("Line Breaks",
content_1 + "\n" + content_2,
"foo\nbar",
[{
type : mds.Token_Type.Paragraph,
children: [content_1, "\n", content_2],
children: ["foo", "\n", "bar"],
}]
)

Expand All @@ -164,23 +161,23 @@ test_single_write("Escaped Line Breaks",
)

test_single_write("Paragraphs",
content_1 + "\n" + "\n" + content_2,
"foo\n\nbar",
[{
type : mds.Token_Type.Paragraph,
children: [content_1],
children: ["foo"],
}, {
type : mds.Token_Type.Paragraph,
children: [content_2],
children: ["bar"],
}]
)

test_single_write("Paragraph with Italic",
"*" + content_1 + "*",
"*foo*",
[{
type : mds.Token_Type.Paragraph,
children: [{
type : mds.Token_Type.Italic_Ast,
children: [content_1]
children: ["foo"]
}],
}]
)
Expand Down Expand Up @@ -230,18 +227,18 @@ test_single_write("Empty Code_Block",
)

test_single_write("Code_Block",
"```\n" + content_1 + "\n```",
"```\nfoo\n```",
[{
type : mds.Token_Type.Code_Block,
children: [content_1]
children: ["foo"]
}]
)

test_single_write("Code_Block with language",
"```js\n" + content_1 + "\n```",
"```js\nfoo\n```",
[{
type : mds.Token_Type.Code_Block,
children: [content_1]
children: ["foo"]
}]
)

Expand Down Expand Up @@ -280,38 +277,38 @@ for (const token of [
}

test_single_write(`Escape ${mds.token_type_to_string(token)} Begin`,
escaped + content_1,
escaped + "foo",
[{
type : mds.Token_Type.Paragraph,
children: [char + content_1]
children: [char + "foo"]
}]
)

test_single_write(`Escape ${mds.token_type_to_string(token)} End`,
char + content_1 + escaped,
char + "foo" + escaped,
[{
type : mds.Token_Type.Paragraph,
children: [{
type : token,
children: [content_1 + char]
children: ["foo" + char]
}]
}]
)
}

test_single_write("Escape Backtick",
"\\`" + content_1,
"\\`" + "foo",
[{
type : mds.Token_Type.Paragraph,
children: ["`" + content_1]
children: ["`" + "foo"]
}]
)

test_single_write("Escape Backslash",
"\\\\" + content_1,
"\\\\" + "foo",
[{
type : mds.Token_Type.Paragraph,
children: ["\\" + content_1]
children: ["\\" + "foo"]
}]
)

Expand Down Expand Up @@ -385,31 +382,31 @@ test_single_write("Link with Image",
)

test_single_write("Escaped link Begin",
"\\[" + content_1 + "](url)",
"\\[foo](url)",
[{
type : mds.Token_Type.Paragraph,
children: ["[" + content_1 + "](url)"]
children: ["[foo](url)"]
}]
)

test_single_write("Escaped link End",
"[" + content_1 + "\\](url)",
"[foo\\](url)",
[{
type : mds.Token_Type.Paragraph,
children: [{
type : mds.Token_Type.Link,
children: [content_1 + "](url)"],
children: ["foo](url)"],
}]
}]
)

test_single_write("Un-Escaped link Both",
"\\\\[" + content_1 + "\\\\](url)",
"\\\\[foo\\\\](url)",
[{
type : mds.Token_Type.Paragraph,
children: ["\\", {
type : mds.Token_Type.Link,
children: [content_1 + "\\"],
children: ["foo\\"],
}]
}]
)
Expand Down

0 comments on commit ca2e98d

Please sign in to comment.