Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style(util): improve formatCmd #1069

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading