@unocss/preset-typography 0.48.4 → 0.49.0

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.
package/README.md CHANGED
@@ -99,6 +99,14 @@ export default defineConfig({
99
99
  the `table` element **(NOTE: `not` utility is only usable in class since it is
100
100
  only used in CSS** **selector & not scanned by UnoCSS)**.
101
101
 
102
+ - **Compatibility Options**
103
+
104
+ This preset used some pseudo-classes which are not widely supported, but you
105
+ can disable them.
106
+
107
+ - If you enabled `noColonNot` or `noColonWhere`, `not-prose` will be unavailable.
108
+ - If you enabled `noColonIs`, attributify mode will have a wrong behavior.
109
+
102
110
  ## Utilities
103
111
 
104
112
  | Rule | Styles by this rule |
@@ -146,6 +154,12 @@ The CSS declarations passed to `cssExtend` will
146
154
  ### Type of `TypographyOptions`
147
155
 
148
156
  ```ts
157
+ export interface TypographyCompatibilityOptions {
158
+ noColonWhere?: boolean
159
+ noColonIs?: boolean
160
+ noColonNot?: boolean
161
+ }
162
+
149
163
  export interface TypographyOptions {
150
164
  /**
151
165
  * The class name to use the typographic utilities.
@@ -164,6 +178,15 @@ export interface TypographyOptions {
164
178
  * @defaultValue undefined
165
179
  */
166
180
  cssExtend?: Record<string, CSSObject>
181
+
182
+ /**
183
+ * Compatibility option. Notice that it will affect some features.
184
+ * For more instructions, see
185
+ * [README](https://github.com/unocss/unocss/tree/main/packages/preset-typography)
186
+ *
187
+ * @defaultValue undefined
188
+ */
189
+ compatibility?: TypographyCompatibilityOptions
167
190
  }
168
191
  ```
169
192
 
package/dist/index.cjs CHANGED
@@ -161,8 +161,10 @@ const DEFAULT = {
161
161
  }
162
162
  };
163
163
 
164
- function getCSS(escapedSelector, selectorName, preflights) {
164
+ function getCSS(options) {
165
165
  let css = "";
166
+ const { escapedSelector, selectorName, preflights, compatibility } = options;
167
+ const disableNotUtility = compatibility?.noColonNot || compatibility?.noColonWhere;
166
168
  for (const selector in preflights) {
167
169
  const cssDeclarationBlock = preflights[selector];
168
170
  const notProseSelector = `:not(:where(.not-${selectorName},.not-${selectorName} *))`;
@@ -171,14 +173,18 @@ function getCSS(escapedSelector, selectorName, preflights) {
171
173
  if (match) {
172
174
  const matchStr = match[0];
173
175
  s = s.replace(matchStr, "");
174
- return `${escapedSelector} :where(${s})${notProseSelector}${matchStr}`;
176
+ return escapedSelector.map(
177
+ (e) => disableNotUtility ? `${e} ${s}${matchStr}` : `${e} :where(${s})${notProseSelector}${matchStr}`
178
+ ).join(",");
175
179
  }
176
180
  return null;
177
181
  }).filter((v) => v);
178
182
  if (pseudoCSSMatchArray.length) {
179
183
  css += pseudoCSSMatchArray.join(",");
180
184
  } else {
181
- css += `${escapedSelector} :where(${selector})${notProseSelector}`;
185
+ css += escapedSelector.map(
186
+ (e) => disableNotUtility ? selector.split(",").map((s) => `${e} ${s}`).join(",") : `${e} :where(${selector})${notProseSelector}`
187
+ ).join(",");
182
188
  }
183
189
  css += "{";
184
190
  for (const k in cssDeclarationBlock) {
@@ -189,25 +195,27 @@ function getCSS(escapedSelector, selectorName, preflights) {
189
195
  }
190
196
  return css;
191
197
  }
192
- function getPreflights(escapedSelector, selectorName, cssExtend) {
193
- if (!escapedSelector.startsWith("."))
194
- escapedSelector = `:is(${escapedSelector},.${selectorName})`;
198
+ function getPreflights(options) {
199
+ const { escapedSelectors, selectorName, cssExtend, compatibility } = options;
200
+ let escapedSelector = Array.from(escapedSelectors);
201
+ if (!escapedSelector[escapedSelector.length - 1].startsWith(".") && !compatibility?.noColonIs)
202
+ escapedSelector = [`:is(${escapedSelector[escapedSelector.length - 1]},.${selectorName})`];
195
203
  if (cssExtend)
196
- return getCSS(escapedSelector, selectorName, core.mergeDeep(DEFAULT, cssExtend));
197
- return getCSS(escapedSelector, selectorName, DEFAULT);
204
+ return getCSS({ escapedSelector, selectorName, preflights: core.mergeDeep(DEFAULT, cssExtend), compatibility });
205
+ return getCSS({ escapedSelector, selectorName, preflights: DEFAULT, compatibility });
198
206
  }
199
207
 
200
208
  function presetTypography(options) {
201
209
  if (options?.className) {
202
210
  console.warn('[unocss:preset-typography] "className" is deprecated. Use "selectorName" instead.');
203
211
  }
204
- let hasProseClass = false;
205
- let escapedSelector = "";
212
+ const escapedSelectors = /* @__PURE__ */ new Set();
206
213
  const selectorName = options?.selectorName || options?.className || "prose";
207
214
  const selectorNameRE = new RegExp(`^${selectorName}$`);
208
215
  const colorsRE = new RegExp(`^${selectorName}-([-\\w]+)$`);
209
216
  const invertRE = new RegExp(`^${selectorName}-invert$`);
210
217
  const cssExtend = options?.cssExtend;
218
+ const compatibility = options?.compatibility;
211
219
  return {
212
220
  name: "@unocss/preset-typography",
213
221
  enforce: "post",
@@ -216,8 +224,7 @@ function presetTypography(options) {
216
224
  [
217
225
  selectorNameRE,
218
226
  (_, { rawSelector }) => {
219
- hasProseClass = true;
220
- escapedSelector = core.toEscapedSelector(rawSelector);
227
+ escapedSelectors.add(core.toEscapedSelector(rawSelector));
221
228
  return { "color": "var(--un-prose-body)", "max-width": "65ch" };
222
229
  },
223
230
  { layer: "typography" }
@@ -274,7 +281,10 @@ function presetTypography(options) {
274
281
  preflights: [
275
282
  {
276
283
  layer: "typography",
277
- getCSS: () => hasProseClass ? getPreflights(escapedSelector, selectorName, cssExtend) : void 0
284
+ getCSS: () => {
285
+ if (escapedSelectors.size > 0)
286
+ return getPreflights({ escapedSelectors, selectorName, cssExtend, compatibility });
287
+ }
278
288
  }
279
289
  ]
280
290
  };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,12 @@
1
1
  import { CSSObject, Preset } from '@unocss/core';
2
2
 
3
+ /** @public */
4
+ interface TypographyCompatibilityOptions {
5
+ noColonWhere?: boolean
6
+ noColonIs?: boolean
7
+ noColonNot?: boolean
8
+ }
9
+
3
10
  /**
4
11
  * @public
5
12
  */
@@ -20,6 +27,14 @@ interface TypographyOptions {
20
27
  * @defaultValue undefined
21
28
  */
22
29
  cssExtend?: Record<string, CSSObject>;
30
+ /**
31
+ * Compatibility option. Notice that it will affect some features.
32
+ * For more instructions, see
33
+ * [README](https://github.com/unocss/unocss/tree/main/packages/preset-typography)
34
+ *
35
+ * @defaultValue undefined
36
+ */
37
+ compatibility?: TypographyCompatibilityOptions;
23
38
  /**
24
39
  * @deprecated use `selectorName` instead. It will be removed in 1.0.
25
40
  */
package/dist/index.mjs CHANGED
@@ -157,8 +157,10 @@ const DEFAULT = {
157
157
  }
158
158
  };
159
159
 
160
- function getCSS(escapedSelector, selectorName, preflights) {
160
+ function getCSS(options) {
161
161
  let css = "";
162
+ const { escapedSelector, selectorName, preflights, compatibility } = options;
163
+ const disableNotUtility = compatibility?.noColonNot || compatibility?.noColonWhere;
162
164
  for (const selector in preflights) {
163
165
  const cssDeclarationBlock = preflights[selector];
164
166
  const notProseSelector = `:not(:where(.not-${selectorName},.not-${selectorName} *))`;
@@ -167,14 +169,18 @@ function getCSS(escapedSelector, selectorName, preflights) {
167
169
  if (match) {
168
170
  const matchStr = match[0];
169
171
  s = s.replace(matchStr, "");
170
- return `${escapedSelector} :where(${s})${notProseSelector}${matchStr}`;
172
+ return escapedSelector.map(
173
+ (e) => disableNotUtility ? `${e} ${s}${matchStr}` : `${e} :where(${s})${notProseSelector}${matchStr}`
174
+ ).join(",");
171
175
  }
172
176
  return null;
173
177
  }).filter((v) => v);
174
178
  if (pseudoCSSMatchArray.length) {
175
179
  css += pseudoCSSMatchArray.join(",");
176
180
  } else {
177
- css += `${escapedSelector} :where(${selector})${notProseSelector}`;
181
+ css += escapedSelector.map(
182
+ (e) => disableNotUtility ? selector.split(",").map((s) => `${e} ${s}`).join(",") : `${e} :where(${selector})${notProseSelector}`
183
+ ).join(",");
178
184
  }
179
185
  css += "{";
180
186
  for (const k in cssDeclarationBlock) {
@@ -185,25 +191,27 @@ function getCSS(escapedSelector, selectorName, preflights) {
185
191
  }
186
192
  return css;
187
193
  }
188
- function getPreflights(escapedSelector, selectorName, cssExtend) {
189
- if (!escapedSelector.startsWith("."))
190
- escapedSelector = `:is(${escapedSelector},.${selectorName})`;
194
+ function getPreflights(options) {
195
+ const { escapedSelectors, selectorName, cssExtend, compatibility } = options;
196
+ let escapedSelector = Array.from(escapedSelectors);
197
+ if (!escapedSelector[escapedSelector.length - 1].startsWith(".") && !compatibility?.noColonIs)
198
+ escapedSelector = [`:is(${escapedSelector[escapedSelector.length - 1]},.${selectorName})`];
191
199
  if (cssExtend)
192
- return getCSS(escapedSelector, selectorName, mergeDeep(DEFAULT, cssExtend));
193
- return getCSS(escapedSelector, selectorName, DEFAULT);
200
+ return getCSS({ escapedSelector, selectorName, preflights: mergeDeep(DEFAULT, cssExtend), compatibility });
201
+ return getCSS({ escapedSelector, selectorName, preflights: DEFAULT, compatibility });
194
202
  }
195
203
 
196
204
  function presetTypography(options) {
197
205
  if (options?.className) {
198
206
  console.warn('[unocss:preset-typography] "className" is deprecated. Use "selectorName" instead.');
199
207
  }
200
- let hasProseClass = false;
201
- let escapedSelector = "";
208
+ const escapedSelectors = /* @__PURE__ */ new Set();
202
209
  const selectorName = options?.selectorName || options?.className || "prose";
203
210
  const selectorNameRE = new RegExp(`^${selectorName}$`);
204
211
  const colorsRE = new RegExp(`^${selectorName}-([-\\w]+)$`);
205
212
  const invertRE = new RegExp(`^${selectorName}-invert$`);
206
213
  const cssExtend = options?.cssExtend;
214
+ const compatibility = options?.compatibility;
207
215
  return {
208
216
  name: "@unocss/preset-typography",
209
217
  enforce: "post",
@@ -212,8 +220,7 @@ function presetTypography(options) {
212
220
  [
213
221
  selectorNameRE,
214
222
  (_, { rawSelector }) => {
215
- hasProseClass = true;
216
- escapedSelector = toEscapedSelector(rawSelector);
223
+ escapedSelectors.add(toEscapedSelector(rawSelector));
217
224
  return { "color": "var(--un-prose-body)", "max-width": "65ch" };
218
225
  },
219
226
  { layer: "typography" }
@@ -270,7 +277,10 @@ function presetTypography(options) {
270
277
  preflights: [
271
278
  {
272
279
  layer: "typography",
273
- getCSS: () => hasProseClass ? getPreflights(escapedSelector, selectorName, cssExtend) : void 0
280
+ getCSS: () => {
281
+ if (escapedSelectors.size > 0)
282
+ return getPreflights({ escapedSelectors, selectorName, cssExtend, compatibility });
283
+ }
274
284
  }
275
285
  ]
276
286
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unocss/preset-typography",
3
- "version": "0.48.4",
3
+ "version": "0.49.0",
4
4
  "description": "Typography preset for UnoCSS",
5
5
  "author": "Jeff Yang",
6
6
  "license": "MIT",
@@ -32,7 +32,7 @@
32
32
  "dist"
33
33
  ],
34
34
  "dependencies": {
35
- "@unocss/core": "0.48.4"
35
+ "@unocss/core": "0.49.0"
36
36
  },
37
37
  "scripts": {
38
38
  "build": "unbuild",