diff --git a/src/md4c.c b/src/md4c.c index d5b077a..0407da0 100644 --- a/src/md4c.c +++ b/src/md4c.c @@ -26,7 +26,6 @@ #include "md4c.h" #include -// #include #include #include @@ -4558,9 +4557,9 @@ md_process_verbatim_block_contents(MD_CTX* ctx, MD_TEXTTYPE text_type, const MD_ MD_ASSERT(indent >= 0); /* Output code indentation. */ - while(indent > (int) SIZEOF_ARRAY(indent_chunk_str)) { + while(indent >= indent_chunk_size) { MD_TEXT(text_type, indent_chunk_str, indent_chunk_size); - indent -= SIZEOF_ARRAY(indent_chunk_str); + indent -= indent_chunk_size; } if(indent > 0) MD_TEXT(text_type, indent_chunk_str, indent); diff --git a/test/issue5.js b/test/issue5.js new file mode 100644 index 0000000..470f6f5 --- /dev/null +++ b/test/issue5.js @@ -0,0 +1,96 @@ +// https://github.com/rsms/markdown-wasm/issues/5 +const libdir = process.argv.includes("-debug") ? "build/debug" : "dist" +const md = require(`../${libdir}/markdown.node.js`) + +// this triggers the bug +const input1 = Buffer.from( + "```\n" + + " |\n" + + "```" +, "utf8") +const expected1 = Buffer.from( + "
" +
+  "                 |\n" +
+  "
\n" +, "utf8") + +// this does not (indent is 1 less space) +const input2 = Buffer.from( + "```\n" + + " |\n" + + "```" +, "utf8") +const expected2 = Buffer.from( + "
" +
+  "                |\n" +
+  "
\n" +, "utf8") + +// this does not (indent is 1 more space) +const input3 = Buffer.from( + "```\n" + + " |\n" + + "```" +, "utf8") +const expected3 = Buffer.from( + "
" +
+  "                  |\n" +
+  "
\n" +, "utf8") + +let fails = 0 +check("test1", expected1, input1) +check("test2", expected2, input2) +check("test3", expected3, input3) + + +function check(name, expected, inputData) { + const actual = Buffer.from(md.parse(inputData, { asMemoryView: true })) + + if (expected.compare(actual) == 0) { + console.log(`${name} OK`) + return + } + fails++ + const line = "——————————————————————————————————————————————————" + const wave = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" + console.error(`${name} FAIL.`) + + console.error(`\n\nExpected output:\n${line}`) + inspectBuf(expected, actual) + console.error(`${line}\n\nActual output:\n${line}`) + inspectBuf(actual, expected) + console.error(line) + + function inspectBuf(buf, otherbuf) { + process.stderr.write(buf) + if (buf[buf.length-1] != 0x0a) { + process.stderr.write("\n") + } + console.error(wave) + const styleReset = "\x1b[22;39m" + const styleNone = s=>s + const styleDiff = process.stderr.isTTY ? s => "\x1b[1;33m"+s+styleReset : styleNone + const styleErr = process.stderr.isTTY ? s => "\x1b[1;31m"+s+styleReset : styleNone + + for (let i = 0; i < buf.length; i++) { + let b = buf[i] + + let style = styleNone + if (b < 0x20 && b != 0x09 && b != 0x0A && b != 0x0D) { + // byte is unexpected control character (except TAB, CR, LF) + style = styleErr + } else if (otherbuf && otherbuf[i] != b) { + style = styleDiff + } + + process.stderr.write(style(b.toString(16).padStart(2, '0')) + " ") + + if (b == 0x0a) { + process.stderr.write("\n") + } + } + } +} + +process.exit(fails > 0 ? 1 : 0) diff --git a/test/test.sh b/test/test.sh new file mode 100755 index 0000000..ac3064f --- /dev/null +++ b/test/test.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +set -euo pipefail +cd "$(dirname "$0")" + +node issue5.js "$@"