`testing console.log('*koala*');
`
`
test
\n\n", "block quote test 1"); + +test_assert_equal(parse_md_to_html("> ```\n> alert('e')\n> ```"), "test
\nTEST
\nbeach
\n\n
weedd
\n
\n", "block quote test 2"); + +//todo: ordered lists, unordered lists, tables + +console.log(parse_md_to_html("- burger\n- fries\n- pizza")); console.log(`Total Passed: \x1B[32m${passed_tests}/${total_tests}\x1B[m\nTotal Failed: \x1B[31m${failed_tests}/${total_tests}\x1B[m`); diff --git a/makoto.ts b/makoto.ts index cf51017..1954e10 100644 --- a/makoto.ts +++ b/makoto.ts @@ -34,11 +34,16 @@ export function parse_md_to_html_with_warnings(md: string): ParseResult { let in_code_block: boolean = false; let first_line_code_block: boolean = false; let code_block_lang: string | undefined = undefined; + let space_start: boolean = false; + let in_blockquote: boolean = false; + let in_unordered_list: boolean = false; + let ordered_list_num: number = 0; //loop through characters let chars: string = md; for (let i=0; i < chars.length; i++) { let char: string = chars[i]; + let end_add_char: boolean = true; //sanitize input if (char === "<") { char = "<"; @@ -66,29 +71,31 @@ export function parse_md_to_html_with_warnings(md: string): ParseResult { //it can only be the first line once :) is_first_line = false; } + //preserving the newlines/linebreaks of the code block + if (in_code_block && char === "\n" && !first_line_code_block) { + html_line += "\nalert('e')\n
\n
")) { @@ -138,6 +145,12 @@ export function parse_md_to_html_with_warnings(md: string): ParseResult { //if last character if (i === chars.length-1 && char !== "\n") { let add_char: boolean = true; + //close code block div + if (in_code_block && i === chars.length-1) { + in_code_block = false; + add_char = false; + html_line = "
")) { html += "
\n"; + } else if ((html_line.startsWith("\n"; + continue; + } else if (in_blockquote && chars[i-1] === "\n" && (char !== ">" || chars[i+1] !== " ")) { + html_line = "\n"; + in_blockquote = false; + } else if (char === ">" && chars[i+1] === " " && (chars[i-1] === "\n" || i === 0)) { + //do not add the '>' to the html + end_add_char = false; + } else if (char === " " && chars[i-1] === ">" && chars[i-2] === "\n") { + //do not add the ' ' in '> ' to the html + end_add_char = false; + } //code blocks - if (char === "`" && chars[i+1] !== "`" && (chars.slice(i-3, i) === "\n``" || (i === 2 && chars.slice(i-2, i) === "``"))) { + if (char === "`" && chars[i+1] !== "`" && ((chars.slice(i-3, i) === "\n``" || (i === 2 && chars.slice(0, i) === "``")) || (in_blockquote && (chars.slice(i-5, i) === "\n> ``" || (i === 4 && chars.slice(0, i) === "> ``"))))) { if (!in_code_block) { //make sure there is ``` further on, that is not backslashed let skip_next: boolean = false; @@ -229,6 +269,12 @@ export function parse_md_to_html_with_warnings(md: string): ParseResult { } else if (chars.slice(adjusted_index-3, adjusted_index+1) === "\n```" && (adjusted_index === chars.length-1 || chars[adjusted_index+1] === "\n")) { end_found = true; break; + } else if (in_blockquote && chars.slice(adjusted_index-5, adjusted_index+1) === "\n> ```" && (adjusted_index === chars.length-1 || chars[adjusted_index+1] === "\n")) { + end_found = true; + break; + } else if (in_blockquote && chars[adjusted_index] === "\n" && (chars[adjusted_index+1] !== ">" || chars[adjusted_index+2] !== " ")) { + //blockquote ended without finding end + break; } } if (end_found) { @@ -257,9 +303,31 @@ export function parse_md_to_html_with_warnings(md: string): ParseResult { continue; } else if (in_code_block) { //do not render markdown inside code blocks... obviously - html_line += char; + //preserve spaces at the beginning of lines + if (char === " " && space_start) { + html_line += " "; + } else if (in_blockquote && ((char === " " && chars.slice(i-2, i) === "\n>") || (char === ">" && chars[i-1] === "\n" && chars[i+1] === " "))) { + //do not add the blockquote syntax thing "> " to the codeblock + } else { + space_start = false; + html_line += char; + } continue; } + //handle unordered lists + if (char === " " && chars[i-1] === "-" && (chars[i-2] === "\n" || i === 1)) { + //it's a unordered list bullet point!! + if (!in_unordered_list) { + html_line = "