Skip to content

Commit 828c7eb

Browse files
authored
refactor(cli): optimize md parser (google#1068)
1 parent 3798607 commit 828c7eb

File tree

3 files changed

+42
-21
lines changed

3 files changed

+42
-21
lines changed

src/cli.ts

+20-17
Original file line numberDiff line numberDiff line change
@@ -219,39 +219,42 @@ export function injectGlobalRequire(origin: string) {
219219
export function transformMarkdown(buf: Buffer | string): string {
220220
const source = buf.toString()
221221
const output = []
222+
const tabRe = /^( +|\t)/
223+
const codeBlockRe =
224+
/^(?<fence>(`{3,20}|~{3,20}))(?:(?<js>(js|javascript|ts|typescript))|(?<bash>(sh|shell|bash))|.*)$/
222225
let state = 'root'
223226
let codeBlockEnd = ''
224227
let prevLineIsEmpty = true
225-
const jsCodeBlock = /^(```{1,20}|~~~{1,20})(js|javascript|ts|typescript)$/
226-
const shCodeBlock = /^(```{1,20}|~~~{1,20})(sh|shell|bash)$/
227-
const otherCodeBlock = /^(```{1,20}|~~~{1,20})(.*)$/
228228
for (const line of source.split(/\r?\n/)) {
229229
switch (state) {
230230
case 'root':
231-
if (/^( {4}|\t)/.test(line) && prevLineIsEmpty) {
231+
if (tabRe.test(line) && prevLineIsEmpty) {
232232
output.push(line)
233233
state = 'tab'
234-
} else if (jsCodeBlock.test(line)) {
235-
output.push('')
234+
continue
235+
}
236+
const { fence, js, bash } = line.match(codeBlockRe)?.groups || {}
237+
if (!fence) {
238+
prevLineIsEmpty = line === ''
239+
output.push('// ' + line)
240+
continue
241+
}
242+
codeBlockEnd = fence
243+
if (js) {
236244
state = 'js'
237-
codeBlockEnd = line.match(jsCodeBlock)![1]
238-
} else if (shCodeBlock.test(line)) {
239-
output.push('await $`')
240-
state = 'bash'
241-
codeBlockEnd = line.match(shCodeBlock)![1]
242-
} else if (otherCodeBlock.test(line)) {
243245
output.push('')
244-
state = 'other'
245-
codeBlockEnd = line.match(otherCodeBlock)![1]
246+
} else if (bash) {
247+
state = 'bash'
248+
output.push('await $`')
246249
} else {
247-
prevLineIsEmpty = line === ''
248-
output.push('// ' + line)
250+
state = 'other'
251+
output.push('')
249252
}
250253
break
251254
case 'tab':
252255
if (line === '') {
253256
output.push('')
254-
} else if (/^( +|\t)/.test(line)) {
257+
} else if (tabRe.test(line)) {
255258
output.push(line)
256259
} else {
257260
output.push('// ' + line)

src/core.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
// limitations under the License.
1414

1515
import {
16-
type StdioOptions,
16+
type ChildProcess,
1717
type IOType,
18+
type StdioOptions,
1819
spawn,
1920
spawnSync,
20-
type ChildProcess,
2121
} from 'node:child_process'
2222
import { type Encoding } from 'node:crypto'
2323
import { type AsyncHook, AsyncLocalStorage, createHook } from 'node:async_hooks'

test/cli.test.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -338,14 +338,32 @@ describe('cli', () => {
338338
# Title
339339
340340
~~~js
341-
await $\`echo "tilde"\`
341+
await $\`echo "js"\`
342+
~~~
343+
344+
typescript code block
345+
~~~~~ts
346+
await $\`echo "ts"\`
347+
~~~~~
348+
349+
~~~
350+
unknown code block
342351
~~~
343352
344353
`), `//
345354
// # Title
346355
//
347356
348-
await $\`echo "tilde"\`
357+
await $\`echo "js"\`
358+
359+
//
360+
// typescript code block
361+
362+
await $\`echo "ts"\`
363+
364+
//
365+
366+
// unknown code block
349367
350368
//
351369
// `)

0 commit comments

Comments
 (0)