Skip to content

Commit

Permalink
Refactor support for ~~~ in markdown scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Feb 27, 2023
1 parent d6d19be commit 2fa391f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 60 deletions.
49 changes: 0 additions & 49 deletions docs/markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,6 @@ await $`whoami`
await $`echo ${__dirname}`
```
~~~js
await $`whoami`
await $`echo ${__dirname}`
~~~

````js
await $`whoami`
await $`echo ${__dirname}`
````

~~~~js
await $`whoami`
await $`echo ${__dirname}`
~~~~

The `__filename` will be pointed to **markdown.md**:

```js
Expand All @@ -48,44 +33,10 @@ VAR=$(date)
echo "$VAR" | wc -c
```

~~~bash
VAR=$(date)
echo "$VAR" | wc -c
~~~

````bash
VAR=$(date)
echo "$VAR" | wc -c
````

~~~~bash
VAR=$(date)
echo "$VAR" | wc -c
~~~~

Other code blocks are ignored:

```css
body .hero {
margin: 42px;
}
```

~~~css
body .hero {
margin: 42px;
}
~~~

````css
body .hero {
margin: 42px;
}
````

~~~~css
body .hero {
margin: 42px;
}
~~~~

24 changes: 13 additions & 11 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,27 +187,29 @@ function transformMarkdown(buf: Buffer) {
const source = buf.toString()
const output = []
let state = 'root'
let sequence = ''
let match = ''
let codeBlockEnd = ''
let prevLineIsEmpty = true
const jsCodeBlock = /^(```+|~~~+)(js|javascript)$/
const shCodeBlock = /^(```+|~~~+)(sh|bash)$/
const otherCodeBlock = /^(```+|~~~+)(.*)$/
for (let line of source.split('\n')) {
switch (state) {
case 'root':
if (/^( {4}|\t)/.test(line) && prevLineIsEmpty) {
output.push(line)
state = 'tab'
} else if (/^(```+|~~~+)(js|javascript)$/.test(line)) {
} else if (jsCodeBlock.test(line)) {
output.push('')
state = 'js'
sequence = line.match(/^(```+|~~~+)(js|javascript)$/)![1]
} else if (/^(```+|~~~+)(sh|bash)$/.test(line)) {
codeBlockEnd = line.match(jsCodeBlock)![1]
} else if (shCodeBlock.test(line)) {
output.push('await $`')
state = 'bash'
sequence = line.match(/^(```+|~~~+)(sh|bash)$/)![1]
} else if (/^(```+|~~~+).*$/.test(line)) {
codeBlockEnd = line.match(shCodeBlock)![1]
} else if (otherCodeBlock.test(line)) {
output.push('')
state = 'other'
sequence = line.match(/^(```+|~~~+)(.*)$/)![1]
codeBlockEnd = line.match(otherCodeBlock)![1]
} else {
prevLineIsEmpty = line === ''
output.push('// ' + line)
Expand All @@ -224,23 +226,23 @@ function transformMarkdown(buf: Buffer) {
}
break
case 'js':
if (line === sequence) {
if (line === codeBlockEnd) {
output.push('')
state = 'root'
} else {
output.push(line)
}
break
case 'bash':
if (line === sequence) {
if (line === codeBlockEnd) {
output.push('`')
state = 'root'
} else {
output.push(line)
}
break
case 'other':
if (line === sequence) {
if (line === codeBlockEnd) {
output.push('')
state = 'root'
} else {
Expand Down
4 changes: 4 additions & 0 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ test('markdown scripts are working', async () => {
await $`node build/cli.js docs/markdown.md`
})

test('markdown scripts are working', async () => {
await $`node build/cli.js docs/markdown.md`
})

test('exceptions are caught', async () => {
let out1 = await $`node build/cli.js <<<${'await $`wtf`'}`.nothrow()
assert.match(out1.stderr, 'Error:')
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ await $`whoami`
await $`echo ${__dirname}`
```
~~~js
await $`echo "tilda"`
~~~

```js
console.log(chalk.yellowBright(__filename))
```
Expand Down

0 comments on commit 2fa391f

Please sign in to comment.