Skip to content

Instantly share code, notes, and snippets.

@SystemDisc
SystemDisc / page.tsx
Created July 23, 2024 07:52
Get Tailwind CSS styles for a dynamic HTML string (e.g. CMS content) in a Next.js 14+ Server Component
import appRootPath from 'app-root-path';
import { readFile } from 'fs/promises';
import { resolve } from 'path';
import postcss from 'postcss';
import tailwindcss, { Config } from 'tailwindcss';
function extractTailwindClasses(htmlContent: string): string[] {
const classRegex = /class=("([^"]+?)"|'([^']+?)')/g;
const classes = new Set<string>();