Commit eda722c

Lawrence Ching <lawrenceching@users.noreply.github.com>
2024-04-26 00:31:20
#787 support CRLF for markdown script (#788)
1 parent 5fd3d8a
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) {
test/fixtures/markdown-crlf.md
@@ -0,0 +1,3 @@
+```js
+echo`Hello, world!`
+```
\ No newline at end of file
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:/)
.gitattributes
@@ -0,0 +1,1 @@
+test/fixtures/markdown-crlf.md eol=crlf
\ No newline at end of file