-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
54 lines (49 loc) · 1.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const { parsers: typescriptParsers } = require('prettier/parser-typescript')
const { parsers: javascriptParsers } = require('prettier/parser-babel')
const { parsers: htmlParsers } = require('prettier/parser-html')
function preprocessClassName(text) {
const classNamePattern = /className\s*=\s*["']([^"']+)["']/g
return text.replace(classNamePattern, (match, classNames) => {
const cleanClassNames = classNames
.trim()
.split(/\s+/)
.filter((item, index, self) => self.indexOf(item) === index)
.join(' ')
return `className="${cleanClassNames}"`
})
}
function preprocessClass(text) {
const classPattern = /class\s*=\s*["']([^"']+)["']/g
return text.replace(classPattern, (match, classNames) => {
const cleanClassNames = classNames
.trim()
.split(/\s+/)
.filter((item, index, self) => self.indexOf(item) === index)
.join(' ')
return `class="${cleanClassNames}"`
})
}
module.exports = {
parsers: {
typescript: {
...typescriptParsers.typescript,
preprocess: preprocessClassName
},
babel: {
...javascriptParsers.babel,
preprocess: preprocessClassName
},
vue: {
...htmlParsers.html,
preprocess: preprocessClass
},
angular: {
...htmlParsers.html,
preprocess: preprocessClass
},
html: {
...htmlParsers.html,
preprocess: preprocessClass
}
}
}