@mintlify/common 1.0.318 → 1.0.320

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,6 +10,7 @@ type MDXOptionsData = {
10
10
  pageMetadata: PageMetaTags;
11
11
  config?: DocsConfig | MintConfig;
12
12
  subdomain?: string;
13
+ tailwindSelectors?: string[];
13
14
  };
14
15
  export declare const getMDXOptions: ({ data, remarkPlugins, rehypePlugins, mdxExtracts, }: {
15
16
  data: MDXOptionsData;
@@ -30,7 +30,7 @@ export const getMDXOptions = ({ data, remarkPlugins = [], rehypePlugins = [], md
30
30
  rehypeRawComponents,
31
31
  rehypeZoomImages,
32
32
  rehypeUnicodeIds,
33
- rehypeDynamicTailwindCss,
33
+ [rehypeDynamicTailwindCss, data.tailwindSelectors],
34
34
  ...rehypePlugins,
35
35
  ],
36
36
  format: 'mdx',
@@ -1,3 +1,3 @@
1
1
  import type { Root } from 'hast';
2
2
  export declare const MINTLIFY_TAILWIND_PREFIX = "mint-";
3
- export declare const rehypeDynamicTailwindCss: () => (tree: Root) => void;
3
+ export declare const rehypeDynamicTailwindCss: (tailwindSelectors?: string[]) => (tree: Root) => void;
@@ -1,23 +1,47 @@
1
+ import { walk } from 'estree-walker';
1
2
  import { visit } from 'unist-util-visit';
2
3
  export const MINTLIFY_TAILWIND_PREFIX = 'mint-';
3
- export const rehypeDynamicTailwindCss = () => {
4
- return (tree) => {
5
- visit(tree, (node) => {
6
- if ((node.type === 'mdxJsxFlowElement' || node.type === 'mdxJsxTextElement') &&
7
- node.name &&
8
- typeof node.name === 'string' &&
9
- /^[a-z]/.test(node.name) && // check for valid html tags
10
- !node.name.includes('.') &&
11
- node.attributes.length > 0) {
12
- const className = node.attributes.find((attr) => attr.type === 'mdxJsxAttribute' && attr.name === 'className');
13
- if (className && typeof className.value === 'string') {
14
- className.value = transformClassNames(className.value);
4
+ export const rehypeDynamicTailwindCss = (tailwindSelectors) => (tree) => {
5
+ visit(tree, (node) => {
6
+ if ((node.type === 'mdxJsxFlowElement' || node.type === 'mdxJsxTextElement') &&
7
+ node.name &&
8
+ typeof node.name === 'string' &&
9
+ /^[a-z]/.test(node.name) && // check for valid html tags
10
+ !node.name.includes('.') &&
11
+ node.attributes.length > 0) {
12
+ const className = node.attributes.find((attr) => attr.type === 'mdxJsxAttribute' && attr.name === 'className');
13
+ if (className && typeof className.value === 'string') {
14
+ className.value = transformClassNames(className.value, tailwindSelectors);
15
+ }
16
+ }
17
+ if (node.type === 'mdxjsEsm' && node.data && node.data.estree) {
18
+ transformMdxJsEsmNode(node.data.estree, tailwindSelectors);
19
+ }
20
+ });
21
+ };
22
+ const transformMdxJsEsmNode = (estreeNode, tailwindSelectors) => {
23
+ walk(estreeNode, {
24
+ enter(node) {
25
+ if (node.type === 'JSXAttribute' &&
26
+ node.name.type === 'JSXIdentifier' &&
27
+ (node.name.name === 'className' || node.name.name === 'class')) {
28
+ if (node.value && node.value.type === 'Literal' && typeof node.value.value === 'string') {
29
+ const originalValue = node.value.value;
30
+ const transformedValue = transformClassNames(originalValue, tailwindSelectors);
31
+ if (originalValue !== transformedValue) {
32
+ node.value.value = transformedValue;
33
+ if (typeof node.value.raw === 'string') {
34
+ node.value.raw = JSON.stringify(transformedValue);
35
+ }
36
+ }
15
37
  }
16
38
  }
17
- });
18
- };
39
+ },
40
+ });
19
41
  };
20
- const transformClassNames = (value) => {
42
+ const transformClassNames = (value, tailwindSelectors) => {
43
+ if (!tailwindSelectors || !tailwindSelectors.length)
44
+ return value;
21
45
  return value
22
46
  .split(/\s+/)
23
47
  .map((className) => {
@@ -27,10 +51,22 @@ const transformClassNames = (value) => {
27
51
  return className;
28
52
  }
29
53
  const parts = className.split(':');
30
- const baseClass = parts.pop();
54
+ let baseClass = parts.pop();
55
+ const hasImportantModifier = baseClass.startsWith('!');
56
+ if (hasImportantModifier) {
57
+ baseClass = baseClass.slice(1);
58
+ }
31
59
  const variants = parts;
32
- const transformedClass = `${MINTLIFY_TAILWIND_PREFIX}${baseClass}`;
33
- return [...variants, transformedClass].join(':');
60
+ if (tailwindSelectors.some((selector) => {
61
+ // remove the leading dot and any escaping backslashes from the selector
62
+ // for example, .bg-orange-400\\/50 -> bg-orange-400/50
63
+ const normalizedSelector = selector.replace(/^\.|\\+/g, '');
64
+ return normalizedSelector.includes(baseClass);
65
+ })) {
66
+ const transformedClass = `${hasImportantModifier ? '!' : ''}${MINTLIFY_TAILWIND_PREFIX}${baseClass}`;
67
+ return [...variants, transformedClass].join(':');
68
+ }
69
+ return className;
34
70
  })
35
71
  .join(' ');
36
72
  };