Skip to content

Commit

Permalink
fix(module): smart safelisting for components in snake case
Browse files Browse the repository at this point in the history
Fixes #461
  • Loading branch information
benjamincanac committed Jul 30, 2023
1 parent 93aebe6 commit e25be11
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions src/colors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { omit, kebabCase, camelCase, upperFirst } from 'lodash-es'

const colorsToExclude = [
'inherit',
'transparent',
Expand All @@ -12,19 +14,6 @@ const colorsToExclude = [
'cool'
]

const omit = (obj: object, keys: string[]) => {
return Object.fromEntries(
Object.entries(obj).filter(([key]) => !keys.includes(key))
)
}

const kebabCase = (str: string) => {
return str
?.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
?.map(x => x.toLowerCase())
?.join('-')
}

const safelistByComponent = {
alert: (colorsAsRegex) => [{
pattern: new RegExp(`bg-(${colorsAsRegex})-50`)
Expand Down Expand Up @@ -225,7 +214,7 @@ export const generateSafelist = (colors: string[], globalColors) => {

export const customSafelistExtractor = (prefix, content: string, colors: string[], safelistColors: string[]) => {
const classes = []
const regex = /<(\w+)\s+(?![^>]*:color\b)[^>]*\bcolor=["']([^"']+)["'][^>]*>/gs
const regex = /<([A-Za-z][A-Za-z0-9]*(?:-[A-Za-z][A-Za-z0-9]*)*)\s+(?![^>]*:color\b)[^>]*\bcolor=["']([^"']+)["'][^>]*>/gs

const matches = content.matchAll(regex)

Expand All @@ -234,11 +223,13 @@ export const customSafelistExtractor = (prefix, content: string, colors: string[
for (const match of matches) {
const [, component, color] = match

const camelComponent = upperFirst(camelCase(component))

if (!colors.includes(color) || safelistColors.includes(color)) {
continue
}

let name = safelistComponentAliasesMap[component] ? safelistComponentAliasesMap[component] : component
let name = safelistComponentAliasesMap[camelComponent] ? safelistComponentAliasesMap[camelComponent] : camelComponent

if (!components.includes(name)) {
continue
Expand Down

1 comment on commit e25be11

@vercel
Copy link

@vercel vercel bot commented on e25be11 Jul 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ui – ./

ui.nuxtlabs.com
ui-git-dev-nuxtlabs.vercel.app
ui-nuxtlabs.vercel.app

Please sign in to comment.