Commit 2fa391f
Changed files (4)
docs/markdown.md
@@ -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
@@ -48,21 +33,6 @@ 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
@@ -70,22 +40,3 @@ body .hero {
margin: 42px;
}
```
-
-~~~css
-body .hero {
- margin: 42px;
-}
-~~~
-
-````css
-body .hero {
- margin: 42px;
-}
-````
-
-~~~~css
-body .hero {
- margin: 42px;
-}
-~~~~
-
src/cli.ts
@@ -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)
@@ -224,7 +226,7 @@ function transformMarkdown(buf: Buffer) {
}
break
case 'js':
- if (line === sequence) {
+ if (line === codeBlockEnd) {
output.push('')
state = 'root'
} else {
@@ -232,7 +234,7 @@ function transformMarkdown(buf: Buffer) {
}
break
case 'bash':
- if (line === sequence) {
+ if (line === codeBlockEnd) {
output.push('`')
state = 'root'
} else {
@@ -240,7 +242,7 @@ function transformMarkdown(buf: Buffer) {
}
break
case 'other':
- if (line === sequence) {
+ if (line === codeBlockEnd) {
output.push('')
state = 'root'
} else {
test/fixtures/markdown.md
@@ -12,6 +12,10 @@ await $`whoami`
await $`echo ${__dirname}`
```
+~~~js
+await $`echo "tilda"`
+~~~
+
```js
console.log(chalk.yellowBright(__filename))
```
test/cli.test.js
@@ -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:')