File tree 3 files changed +39
-16
lines changed
3 files changed +39
-16
lines changed Original file line number Diff line number Diff line change 84
84
"typescript" : " ^5.0.2" ,
85
85
"vitest" : " ^1.6.0"
86
86
},
87
- "dependencies" : {
88
- "detect-indent" : " ^6.1.0"
89
- },
90
87
"peerDependencies" : {
91
88
"@babel/core" : " ^7.10.2" ,
92
89
"coffeescript" : " ^2.5.1" ,
Original file line number Diff line number Diff line change 1
- import detectIndent from 'detect-indent' ;
2
1
import pug from 'pug' ;
3
2
4
3
import type { Transformer , Options } from '../types' ;
@@ -67,8 +66,8 @@ const transformer: Transformer<Options.Pug> = async ({
67
66
...options ,
68
67
} ;
69
68
70
- const { type : indentationType } = detectIndent ( content ) ;
71
- const input = `${ GET_MIXINS ( indentationType ?? 'space' ) } \n${ content } ` ;
69
+ const spaces = guessIndentString ( content ) ;
70
+ const input = `${ GET_MIXINS ( spaces ? 'space' : 'tab ') } \n${ content } ` ;
72
71
const compiled = pug . compile (
73
72
input ,
74
73
pugOptions ,
@@ -94,4 +93,41 @@ const transformer: Transformer<Options.Pug> = async ({
94
93
} ;
95
94
} ;
96
95
96
+ // Sourced from `golden-fleece`
97
+ // https://github.com/Rich-Harris/golden-fleece/blob/f2446f331640f325e13609ed99b74b6a45e755c2/src/patch.ts#L302
98
+ function guessIndentString ( str : string ) : number | undefined {
99
+ const lines = str . split ( '\n' ) ;
100
+
101
+ let tabs = 0 ;
102
+ let spaces = 0 ;
103
+ let minSpaces = 8 ;
104
+
105
+ lines . forEach ( ( line ) => {
106
+ const match = / ^ (?: + | \t + ) / . exec ( line ) ;
107
+
108
+ if ( ! match ) return ;
109
+
110
+ const [ whitespace ] = match ;
111
+
112
+ if ( whitespace . length === line . length ) return ;
113
+
114
+ if ( whitespace [ 0 ] === '\t' ) {
115
+ tabs += 1 ;
116
+ } else {
117
+ spaces += 1 ;
118
+ if ( whitespace . length > 1 && whitespace . length < minSpaces ) {
119
+ minSpaces = whitespace . length ;
120
+ }
121
+ }
122
+ } ) ;
123
+
124
+ if ( spaces > tabs ) {
125
+ let result = '' ;
126
+
127
+ while ( minSpaces -- ) result += ' ' ;
128
+
129
+ return result . length ;
130
+ }
131
+ }
132
+
97
133
export { transformer } ;
You can’t perform that action at this time.
0 commit comments