@mintlify/common
Version:
Commonly shared code within Mintlify
60 lines (59 loc) • 2.58 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import postcss from 'postcss';
import tailwindcss from 'tailwindcss';
import { MINTLIFY_TAILWIND_PREFIX } from '../index.js';
function removeDarkClassNamePrefix(css) {
return css.replace(/\.mint-dark \*/g, '.dark *');
}
function getAllSelectors(nodes) {
const selectors = [];
for (const node of nodes) {
if (node.type === 'rule') {
selectors.push(node.selector);
}
else if (node.type === 'atrule' && node.nodes) {
selectors.push(...getAllSelectors(node.nodes));
}
}
return selectors;
}
function getContent(content) {
const extensions = ['html', 'tsx', 'mdx'];
return extensions.map((extension) => ({ raw: content, extension }));
}
/** Get generated TailwindCSS from the content with prefix added to each class */
export function getDynamicTailwindCss(_a) {
return __awaiter(this, arguments, void 0, function* ({ content, prefix = MINTLIFY_TAILWIND_PREFIX, }) {
const css = `@tailwind utilities;`;
const tailwindPlugin = tailwindcss({
content: getContent(content),
corePlugins: { preflight: false },
prefix,
darkMode: 'class',
});
const result = yield postcss([tailwindPlugin]).process(css, { from: undefined });
result.css = removeDarkClassNamePrefix(result.css);
return result.css;
});
}
export function getTailwindSelectors(_a) {
return __awaiter(this, arguments, void 0, function* ({ content }) {
const css = `@tailwind utilities;`;
const tailwindPlugin = tailwindcss({
content: getContent(content),
corePlugins: { preflight: false },
darkMode: 'class',
});
const result = yield postcss([tailwindPlugin]).process(css, { from: undefined });
const selectors = getAllSelectors(result.root.nodes);
return selectors;
});
}