/
extractor.ts
35 lines (32 loc) · 930 Bytes
/
extractor.ts
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
import type { Extractor } from '@unocss/core'
import type { TagifyOptions } from './types'
export const MARKER = '__TAGIFY__'
export const htmlTagRE = /<([\w\d-:]+)/g
export const extractorTagify = (options: TagifyOptions): Extractor => {
const {
prefix = '',
excludedTags = ['b', /^h\d+$/, 'table'],
} = options
return {
name: 'tagify',
extract({ code }) {
return new Set(
Array.from(code.matchAll(htmlTagRE))
.filter(({ 1: match }) => {
for (const exclude of excludedTags) {
if (typeof exclude === 'string') {
if (match === exclude)
return false
}
else {
if (exclude.test(match))
return false
}
}
return match.startsWith(prefix)
})
.map(([, matched]) => `${MARKER}${matched}`),
)
},
}
}