Skip to content

Commit

Permalink
fix(build): dedent of a single-line region (#1687)
Browse files Browse the repository at this point in the history
Co-authored-by: Divyansh Singh <[email protected]>
  • Loading branch information
0x009922 and brc-dd authored Dec 30, 2022
1 parent 655bca1 commit 7de7fff
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 22 deletions.
60 changes: 60 additions & 0 deletions __tests__/unit/node/markdown/plugins/snippet.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { dedent } from 'node/markdown/plugins/snippet'

describe('node/markdown/plugins/snippet', () => {
describe('dedent', () => {
test('when 0-level is minimal, do not remove spaces', () => {
expect(
dedent(
[
//
'fn main() {',
' println!("Hello");',
'}'
].join('\n')
)
).toMatchInlineSnapshot(`
"fn main() {
println!(\\"Hello\\");
}"
`)
})

test('when 4-level is minimal, remove 4 spaces', () => {
expect(
dedent(
[
//
' let a = {',
' value: 42',
' };'
].join('\n')
)
).toMatchInlineSnapshot(`
"let a = {
value: 42
};"
`)
})

test('when only 1 line is passed, dedent it', () => {
expect(dedent(' let a = 42;')).toEqual('let a = 42;')
})

test('handle tabs as well', () => {
expect(
dedent(
[
//
' let a = {',
' value: 42',
' };'
].join('\n')
)
).toMatchInlineSnapshot(`
"let a = {
value: 42
};"
`)
})
})
})
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"test-build": "VITE_TEST_BUILD=1 pnpm test-preview",
"debug-preview": "DEBUG=1 vitest -r __tests__/e2e",
"debug-build": "VITE_TEST_BUILD=1 pnpm debug-preview",
"unit-dev": "vitest -r __tests__/unit",
"e2e-dev": "wait-on -d 100 dist/node/cli.js && node ./bin/vitepress dev __tests__/e2e",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
"release": "node scripts/release.js",
Expand Down
33 changes: 11 additions & 22 deletions src/node/markdown/plugins/snippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,18 @@ import path from 'path'
import MarkdownIt from 'markdown-it'
import { RuleBlock } from 'markdown-it/lib/parser_block'

function dedent(text: string) {
const wRegexp = /^([ \t]*)(.*)\n/gm
let match
let minIndentLength = null

while ((match = wRegexp.exec(text)) !== null) {
const [indentation, content] = match.slice(1)
if (!content) continue

const indentLength = indentation.length
if (indentLength > 0) {
minIndentLength =
minIndentLength !== null
? Math.min(minIndentLength, indentLength)
: indentLength
} else break
}
export function dedent(text: string): string {
const lines = text.split('\n')

const minIndentLength = lines.reduce((acc, line) => {
for (let i = 0; i < line.length; i++) {
if (line[i] !== ' ' && line[i] !== '\t') return Math.min(i, acc)
}
return acc
}, Infinity)

if (minIndentLength) {
text = text.replace(
new RegExp(`^[ \t]{${minIndentLength}}(.*)`, 'gm'),
'$1'
)
if (minIndentLength < Infinity) {
return lines.map((x) => x.slice(minIndentLength)).join('\n')
}

return text
Expand Down

0 comments on commit 7de7fff

Please sign in to comment.