Skip to content

Commit 2fa391f

Browse files
committed
Refactor support for ~~~ in markdown scripts
1 parent d6d19be commit 2fa391f

File tree

4 files changed

+21
-60
lines changed

4 files changed

+21
-60
lines changed

docs/markdown.md

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,6 @@ await $`whoami`
1414
await $`echo ${__dirname}`
1515
```
1616
17-
~~~js
18-
await $`whoami`
19-
await $`echo ${__dirname}`
20-
~~~
21-
22-
````js
23-
await $`whoami`
24-
await $`echo ${__dirname}`
25-
````
26-
27-
~~~~js
28-
await $`whoami`
29-
await $`echo ${__dirname}`
30-
~~~~
31-
3217
The `__filename` will be pointed to **markdown.md**:
3318

3419
```js
@@ -48,44 +33,10 @@ VAR=$(date)
4833
echo "$VAR" | wc -c
4934
```
5035

51-
~~~bash
52-
VAR=$(date)
53-
echo "$VAR" | wc -c
54-
~~~
55-
56-
````bash
57-
VAR=$(date)
58-
echo "$VAR" | wc -c
59-
````
60-
61-
~~~~bash
62-
VAR=$(date)
63-
echo "$VAR" | wc -c
64-
~~~~
65-
6636
Other code blocks are ignored:
6737

6838
```css
6939
body .hero {
7040
margin: 42px;
7141
}
7242
```
73-
74-
~~~css
75-
body .hero {
76-
margin: 42px;
77-
}
78-
~~~
79-
80-
````css
81-
body .hero {
82-
margin: 42px;
83-
}
84-
````
85-
86-
~~~~css
87-
body .hero {
88-
margin: 42px;
89-
}
90-
~~~~
91-

src/cli.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -187,27 +187,29 @@ function transformMarkdown(buf: Buffer) {
187187
const source = buf.toString()
188188
const output = []
189189
let state = 'root'
190-
let sequence = ''
191-
let match = ''
190+
let codeBlockEnd = ''
192191
let prevLineIsEmpty = true
192+
const jsCodeBlock = /^(```+|~~~+)(js|javascript)$/
193+
const shCodeBlock = /^(```+|~~~+)(sh|bash)$/
194+
const otherCodeBlock = /^(```+|~~~+)(.*)$/
193195
for (let line of source.split('\n')) {
194196
switch (state) {
195197
case 'root':
196198
if (/^( {4}|\t)/.test(line) && prevLineIsEmpty) {
197199
output.push(line)
198200
state = 'tab'
199-
} else if (/^(```+|~~~+)(js|javascript)$/.test(line)) {
201+
} else if (jsCodeBlock.test(line)) {
200202
output.push('')
201203
state = 'js'
202-
sequence = line.match(/^(```+|~~~+)(js|javascript)$/)![1]
203-
} else if (/^(```+|~~~+)(sh|bash)$/.test(line)) {
204+
codeBlockEnd = line.match(jsCodeBlock)![1]
205+
} else if (shCodeBlock.test(line)) {
204206
output.push('await $`')
205207
state = 'bash'
206-
sequence = line.match(/^(```+|~~~+)(sh|bash)$/)![1]
207-
} else if (/^(```+|~~~+).*$/.test(line)) {
208+
codeBlockEnd = line.match(shCodeBlock)![1]
209+
} else if (otherCodeBlock.test(line)) {
208210
output.push('')
209211
state = 'other'
210-
sequence = line.match(/^(```+|~~~+)(.*)$/)![1]
212+
codeBlockEnd = line.match(otherCodeBlock)![1]
211213
} else {
212214
prevLineIsEmpty = line === ''
213215
output.push('// ' + line)
@@ -224,23 +226,23 @@ function transformMarkdown(buf: Buffer) {
224226
}
225227
break
226228
case 'js':
227-
if (line === sequence) {
229+
if (line === codeBlockEnd) {
228230
output.push('')
229231
state = 'root'
230232
} else {
231233
output.push(line)
232234
}
233235
break
234236
case 'bash':
235-
if (line === sequence) {
237+
if (line === codeBlockEnd) {
236238
output.push('`')
237239
state = 'root'
238240
} else {
239241
output.push(line)
240242
}
241243
break
242244
case 'other':
243-
if (line === sequence) {
245+
if (line === codeBlockEnd) {
244246
output.push('')
245247
state = 'root'
246248
} else {

test/cli.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ test('markdown scripts are working', async () => {
135135
await $`node build/cli.js docs/markdown.md`
136136
})
137137

138+
test('markdown scripts are working', async () => {
139+
await $`node build/cli.js docs/markdown.md`
140+
})
141+
138142
test('exceptions are caught', async () => {
139143
let out1 = await $`node build/cli.js <<<${'await $`wtf`'}`.nothrow()
140144
assert.match(out1.stderr, 'Error:')

test/fixtures/markdown.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ await $`whoami`
1212
await $`echo ${__dirname}`
1313
```
1414
15+
~~~js
16+
await $`echo "tilda"`
17+
~~~
18+
1519
```js
1620
console.log(chalk.yellowBright(__filename))
1721
```

0 commit comments

Comments
 (0)