Skip to content

Commit

Permalink
rename variables and add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Jan 27, 2024
1 parent b6a6a28 commit 9c00d5f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export const CodeFrame: React.FC<CodeFrameProps> = function CodeFrame({
// Strip leading spaces out of the code frame:
const formattedFrame = React.useMemo<string>(() => {
const lines = codeFrame.split(/\r?\n/g)
const prefixLength = lines

// Find the minimum length of leading spaces after `|` in the code frame
const miniLeadingSpacesLength = lines
.map((line) =>
/^>? +\d+ +\| [ ]+/.exec(stripAnsi(line)) === null
? null
Expand All @@ -24,12 +26,14 @@ export const CodeFrame: React.FC<CodeFrameProps> = function CodeFrame({
.map((v) => v!.pop()!)
.reduce((c, n) => (isNaN(c) ? n.length : Math.min(c, n.length)), NaN)

if (prefixLength > 1) {
// When the minimum length of leading spaces is greater than 1, remove them
// from the code frame to help the indentation looks better when there's a lot leading spaces.
if (miniLeadingSpacesLength > 1) {
return lines
.map((line, a) =>
~(a = line.indexOf('|'))
? line.substring(0, a) +
line.substring(a).replace(`^\\ {${prefixLength}}`, '')
line.substring(a).replace(`^\\ {${miniLeadingSpacesLength}}`, '')
: line
)
.join('\n')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export const CodeFrame: React.FC<CodeFrameProps> = function CodeFrame({
// Strip leading spaces out of the code frame:
const formattedFrame = React.useMemo<string>(() => {
const lines = codeFrame.split(/\r?\n/g)
const prefixLength = lines

// Find the minimum length of leading spaces after `|` in the code frame
const miniLeadingSpacesLength = lines
.map((line) =>
/^>? +\d+ +\| [ ]+/.exec(stripAnsi(line)) === null
? null
Expand All @@ -26,12 +28,16 @@ export const CodeFrame: React.FC<CodeFrameProps> = function CodeFrame({
.map((v) => v!.pop()!)
.reduce((c, n) => (isNaN(c) ? n.length : Math.min(c, n.length)), NaN)

if (prefixLength > 1) {
// When the minimum length of leading spaces is greater than 1, remove them
// from the code frame to help the indentation looks better when there's a lot leading spaces.
if (miniLeadingSpacesLength > 1) {
return lines
.map((line, a) =>
~(a = line.indexOf('|'))
? line.substring(0, a) +
line.substring(a).replace(new RegExp(`^\\ {${prefixLength}}`), '')
line
.substring(a)
.replace(new RegExp(`^\\ {${miniLeadingSpacesLength}}`), '')
: line
)
.join('\n')
Expand Down

0 comments on commit 9c00d5f

Please sign in to comment.