Skip to content

Commit

Permalink
style(util): improve formatCmd
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Jan 8, 2025
1 parent 828c7eb commit 86fcf8e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
3 changes: 1 addition & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,14 @@ export function injectGlobalRequire(origin: string) {
}

export function transformMarkdown(buf: Buffer | string): string {
const source = buf.toString()
const output = []
const tabRe = /^( +|\t)/
const codeBlockRe =
/^(?<fence>(`{3,20}|~{3,20}))(?:(?<js>(js|javascript|ts|typescript))|(?<bash>(sh|shell|bash))|.*)$/
let state = 'root'
let codeBlockEnd = ''
let prevLineIsEmpty = true
for (const line of source.split(/\r?\n/)) {
for (const line of buf.toString().split(/\r?\n/)) {
switch (state) {
case 'root':
if (tabRe.test(line) && prevLineIsEmpty) {
Expand Down
18 changes: 6 additions & 12 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,23 +277,19 @@ export function formatCmd(cmd?: string): string {
}

function space() {
if (/\s/.test(ch)) return space
return root
return /\s/.test(ch) ? space : root
}

function word() {
if (/[\w/.]/i.test(ch)) return word
return root
return /[\w/.]/i.test(ch) ? word : root
}

function syntax() {
if (isSyntax(ch)) return syntax
return root
return isSyntax(ch) ? syntax : root
}

function dollar() {
if (ch === "'") return str
return root
return ch === "'" ? str : root
}

function str() {
Expand All @@ -311,13 +307,11 @@ export function formatCmd(cmd?: string): string {
}

function strDouble() {
if (ch === '"') return strEnd
return strDouble
return ch === '"' ? strEnd : strDouble
}

function strSingle() {
if (ch === "'") return strEnd
return strSingle
return ch === "'" ? strEnd : strSingle
}

function strEnd() {
Expand Down

0 comments on commit 86fcf8e

Please sign in to comment.