From eda722c9aaed0315080df1ede9ca8f90e8f1d090 Mon Sep 17 00:00:00 2001 From: Lawrence Ching Date: Fri, 26 Apr 2024 06:31:20 +0800 Subject: [PATCH] #787 support CRLF for markdown script (#788) --- .gitattributes | 1 + src/cli.ts | 2 +- test/cli.test.js | 5 +++++ test/fixtures/markdown-crlf.md | 3 +++ 4 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 .gitattributes create mode 100755 test/fixtures/markdown-crlf.md diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000000..0385bb6b11 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +test/fixtures/markdown-crlf.md eol=crlf \ No newline at end of file diff --git a/src/cli.ts b/src/cli.ts index 7bd06ffedb..9411310e4f 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -195,7 +195,7 @@ function transformMarkdown(buf: Buffer) { const jsCodeBlock = /^(```+|~~~+)(js|javascript)$/ const shCodeBlock = /^(```+|~~~+)(sh|bash)$/ const otherCodeBlock = /^(```+|~~~+)(.*)$/ - for (let line of source.split('\n')) { + for (let line of source.split(/\r?\n/)) { switch (state) { case 'root': if (/^( {4}|\t)/.test(line) && prevLineIsEmpty) { diff --git a/test/cli.test.js b/test/cli.test.js index dd7a06efe9..ed283e8b9a 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -142,6 +142,11 @@ describe('cli', () => { await $`node build/cli.js test/fixtures/markdown.md` }) + test('markdown scripts are working for CRLF', async () => { + let p = await $`node build/cli.js test/fixtures/markdown-crlf.md` + assert.ok(p.stdout.includes('Hello, world!')) + }) + test('exceptions are caught', async () => { let out1 = await $`node build/cli.js <<<${'await $`wtf`'}`.nothrow() assert.match(out1.stderr, /Error:/) diff --git a/test/fixtures/markdown-crlf.md b/test/fixtures/markdown-crlf.md new file mode 100755 index 0000000000..a8f5f4805e --- /dev/null +++ b/test/fixtures/markdown-crlf.md @@ -0,0 +1,3 @@ +```js +echo`Hello, world!` +``` \ No newline at end of file