File tree 3 files changed +72
-22
lines changed
__tests__/unit/node/markdown/plugins
src/node/markdown/plugins
3 files changed +72
-22
lines changed Original file line number Diff line number Diff line change
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
+ } )
Original file line number Diff line number Diff line change 68
68
"test-build" : " VITE_TEST_BUILD=1 pnpm test-preview" ,
69
69
"debug-preview" : " DEBUG=1 vitest -r __tests__/e2e" ,
70
70
"debug-build" : " VITE_TEST_BUILD=1 pnpm debug-preview" ,
71
+ "unit-dev" : " vitest -r __tests__/unit" ,
71
72
"e2e-dev" : " wait-on -d 100 dist/node/cli.js && node ./bin/vitepress dev __tests__/e2e" ,
72
73
"changelog" : " conventional-changelog -p angular -i CHANGELOG.md -s" ,
73
74
"release" : " node scripts/release.js" ,
Original file line number Diff line number Diff line change @@ -3,29 +3,18 @@ import path from 'path'
3
3
import MarkdownIt from 'markdown-it'
4
4
import { RuleBlock } from 'markdown-it/lib/parser_block'
5
5
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 )
23
15
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' )
29
18
}
30
19
31
20
return text
You can’t perform that action at this time.
0 commit comments