Skip to content

Commit 7de7fff

Browse files
0x009922brc-dd
andauthored
fix(build): dedent of a single-line region (#1687)
Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
1 parent 655bca1 commit 7de7fff

File tree

3 files changed

+72
-22
lines changed

3 files changed

+72
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { dedent } from 'node/markdown/plugins/snippet'
2+
3+
describe('node/markdown/plugins/snippet', () => {
4+
describe('dedent', () => {
5+
test('when 0-level is minimal, do not remove spaces', () => {
6+
expect(
7+
dedent(
8+
[
9+
//
10+
'fn main() {',
11+
' println!("Hello");',
12+
'}'
13+
].join('\n')
14+
)
15+
).toMatchInlineSnapshot(`
16+
"fn main() {
17+
println!(\\"Hello\\");
18+
}"
19+
`)
20+
})
21+
22+
test('when 4-level is minimal, remove 4 spaces', () => {
23+
expect(
24+
dedent(
25+
[
26+
//
27+
' let a = {',
28+
' value: 42',
29+
' };'
30+
].join('\n')
31+
)
32+
).toMatchInlineSnapshot(`
33+
"let a = {
34+
value: 42
35+
};"
36+
`)
37+
})
38+
39+
test('when only 1 line is passed, dedent it', () => {
40+
expect(dedent(' let a = 42;')).toEqual('let a = 42;')
41+
})
42+
43+
test('handle tabs as well', () => {
44+
expect(
45+
dedent(
46+
[
47+
//
48+
' let a = {',
49+
' value: 42',
50+
' };'
51+
].join('\n')
52+
)
53+
).toMatchInlineSnapshot(`
54+
"let a = {
55+
value: 42
56+
};"
57+
`)
58+
})
59+
})
60+
})

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
"test-build": "VITE_TEST_BUILD=1 pnpm test-preview",
6969
"debug-preview": "DEBUG=1 vitest -r __tests__/e2e",
7070
"debug-build": "VITE_TEST_BUILD=1 pnpm debug-preview",
71+
"unit-dev": "vitest -r __tests__/unit",
7172
"e2e-dev": "wait-on -d 100 dist/node/cli.js && node ./bin/vitepress dev __tests__/e2e",
7273
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
7374
"release": "node scripts/release.js",

src/node/markdown/plugins/snippet.ts

+11-22
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,18 @@ import path from 'path'
33
import MarkdownIt from 'markdown-it'
44
import { RuleBlock } from 'markdown-it/lib/parser_block'
55

6-
function dedent(text: string) {
7-
const wRegexp = /^([ \t]*)(.*)\n/gm
8-
let match
9-
let minIndentLength = null
10-
11-
while ((match = wRegexp.exec(text)) !== null) {
12-
const [indentation, content] = match.slice(1)
13-
if (!content) continue
14-
15-
const indentLength = indentation.length
16-
if (indentLength > 0) {
17-
minIndentLength =
18-
minIndentLength !== null
19-
? Math.min(minIndentLength, indentLength)
20-
: indentLength
21-
} else break
22-
}
6+
export function dedent(text: string): string {
7+
const lines = text.split('\n')
8+
9+
const minIndentLength = lines.reduce((acc, line) => {
10+
for (let i = 0; i < line.length; i++) {
11+
if (line[i] !== ' ' && line[i] !== '\t') return Math.min(i, acc)
12+
}
13+
return acc
14+
}, Infinity)
2315

24-
if (minIndentLength) {
25-
text = text.replace(
26-
new RegExp(`^[ \t]{${minIndentLength}}(.*)`, 'gm'),
27-
'$1'
28-
)
16+
if (minIndentLength < Infinity) {
17+
return lines.map((x) => x.slice(minIndentLength)).join('\n')
2918
}
3019

3120
return text

0 commit comments

Comments
 (0)