Skip to content

Commit d6d19be

Browse files
Support ~~~ syntax in md
Co-authored-by: Jakob Voss <[email protected]>
1 parent 101b810 commit d6d19be

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

docs/markdown.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Markdown Scripts
22

33
It's possible to write scripts using markdown. Only code blocks will be executed
4-
by zx.
4+
by zx.
55

66
> You can run this markdown file:
77
>
@@ -14,6 +14,21 @@ 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+
1732
The `__filename` will be pointed to **markdown.md**:
1833

1934
```js
@@ -33,10 +48,44 @@ VAR=$(date)
3348
echo "$VAR" | wc -c
3449
```
3550

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+
3666
Other code blocks are ignored:
3767

3868
```css
3969
body .hero {
4070
margin: 42px;
4171
}
4272
```
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: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,22 +187,27 @@ function transformMarkdown(buf: Buffer) {
187187
const source = buf.toString()
188188
const output = []
189189
let state = 'root'
190+
let sequence = ''
191+
let match = ''
190192
let prevLineIsEmpty = true
191193
for (let line of source.split('\n')) {
192194
switch (state) {
193195
case 'root':
194196
if (/^( {4}|\t)/.test(line) && prevLineIsEmpty) {
195197
output.push(line)
196198
state = 'tab'
197-
} else if (/^```(js|javascript)$/.test(line)) {
199+
} else if (/^(```+|~~~+)(js|javascript)$/.test(line)) {
198200
output.push('')
199201
state = 'js'
200-
} else if (/^```(sh|bash)$/.test(line)) {
202+
sequence = line.match(/^(```+|~~~+)(js|javascript)$/)![1]
203+
} else if (/^(```+|~~~+)(sh|bash)$/.test(line)) {
201204
output.push('await $`')
202205
state = 'bash'
203-
} else if (/^```.*$/.test(line)) {
206+
sequence = line.match(/^(```+|~~~+)(sh|bash)$/)![1]
207+
} else if (/^(```+|~~~+).*$/.test(line)) {
204208
output.push('')
205209
state = 'other'
210+
sequence = line.match(/^(```+|~~~+)(.*)$/)![1]
206211
} else {
207212
prevLineIsEmpty = line === ''
208213
output.push('// ' + line)
@@ -219,23 +224,23 @@ function transformMarkdown(buf: Buffer) {
219224
}
220225
break
221226
case 'js':
222-
if (/^```$/.test(line)) {
227+
if (line === sequence) {
223228
output.push('')
224229
state = 'root'
225230
} else {
226231
output.push(line)
227232
}
228233
break
229234
case 'bash':
230-
if (/^```$/.test(line)) {
235+
if (line === sequence) {
231236
output.push('`')
232237
state = 'root'
233238
} else {
234239
output.push(line)
235240
}
236241
break
237242
case 'other':
238-
if (/^```$/.test(line)) {
243+
if (line === sequence) {
239244
output.push('')
240245
state = 'root'
241246
} else {

0 commit comments

Comments
 (0)