/
index.ts
144 lines (136 loc) · 4.71 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import type { CSSObject, Preset, RuleContext } from '@unocss/core'
import type { Theme } from '@unocss/preset-mini'
import { toEscapedSelector } from '@unocss/core'
import { getPreflights } from './preflights'
/**
* @public
*/
export interface TypographyOptions {
/**
* The selector name to use the typographic utilities.
* To undo the styles to the elements, use it like
* `not-${selectorName}` which is by default `not-prose`.
*
* Note: `not` utility is only available in class mode.
*
* @defaultValue `prose`
*/
selectorName?: string
/**
* Extend or override CSS selectors with CSS declaration block.
*
* @defaultValue undefined
*/
cssExtend?: Record<string, CSSObject>
/**
* @deprecated use `selectorName` instead. It will be removed in 1.0.
*/
className?: string
}
/**
* UnoCSS Preset for Typography
*
* ```js
* // unocss.config.js
* import { presetAttributify, presetUno, defineConfig, presetTypography } from 'unocss'
*
* export default defineConfig({
* presets: [
* presetAttributify(), // required if using attributify mode
* presetUno(), // required
* presetTypography()
* ]
* })
* ```
*
* @returns typography preset
* @public
*/
export function presetTypography(options?: TypographyOptions): Preset {
if (options?.className) {
console.warn('[unocss:preset-typography] "className" is deprecated. '
+ 'Use "selectorName" instead.')
}
let hasProseClass = false
let escapedSelector = ''
const selectorName = options?.selectorName || options?.className || 'prose'
const selectorNameRE = new RegExp(`^${selectorName}$`)
const colorsRE = new RegExp(`^${selectorName}-([-\\w]+)$`)
const invertRE = new RegExp(`^${selectorName}-invert$`)
const cssExtend = options?.cssExtend
return {
name: '@unocss/preset-typography',
enforce: 'post',
layers: { typography: -20 },
rules: [
[
selectorNameRE,
(_, { rawSelector }) => {
hasProseClass = true
escapedSelector = toEscapedSelector(rawSelector)
return { 'color': 'var(--un-prose-body)', 'max-width': '65ch' }
},
{ layer: 'typography' },
],
[
colorsRE,
([, color], { theme }: RuleContext<Theme>) => {
const baseColor = theme.colors?.[color]
if (baseColor == null)
return
const colorObject = typeof baseColor === 'object' ? baseColor : {}
return {
'--un-prose-body': colorObject[700] ?? baseColor,
'--un-prose-headings': colorObject[900] ?? baseColor,
'--un-prose-links': colorObject[900] ?? baseColor,
'--un-prose-lists': colorObject[400] ?? baseColor,
'--un-prose-hr': colorObject[200] ?? baseColor,
'--un-prose-captions': colorObject[500] ?? baseColor,
'--un-prose-code': colorObject[900] ?? baseColor,
'--un-prose-borders': colorObject[200] ?? baseColor,
'--un-prose-bg-soft': colorObject[100] ?? baseColor,
// invert colors (dark mode)
'--un-prose-invert-body': colorObject[200] ?? baseColor,
'--un-prose-invert-headings': colorObject[100] ?? baseColor,
'--un-prose-invert-links': colorObject[100] ?? baseColor,
'--un-prose-invert-lists': colorObject[500] ?? baseColor,
'--un-prose-invert-hr': colorObject[700] ?? baseColor,
'--un-prose-invert-captions': colorObject[400] ?? baseColor,
'--un-prose-invert-code': colorObject[100] ?? baseColor,
'--un-prose-invert-borders': colorObject[700] ?? baseColor,
'--un-prose-invert-bg-soft': colorObject[800] ?? baseColor,
'--un-prose-font-mono': theme.fontFamily?.mono,
}
},
{ layer: 'typography' },
],
[
invertRE,
() => {
return {
'--un-prose-body': 'var(--un-prose-invert-body)',
'--un-prose-headings': 'var(--un-prose-invert-headings)',
'--un-prose-links': 'var(--un-prose-invert-links)',
'--un-prose-lists': 'var(--un-prose-invert-lists)',
'--un-prose-hr': 'var(--un-prose-invert-hr)',
'--un-prose-captions': 'var(--un-prose-invert-captions)',
'--un-prose-code': 'var(--un-prose-invert-code)',
'--un-prose-borders': 'var(--un-prose-invert-borders)',
'--un-prose-bg-soft': 'var(--un-prose-invert-bg-soft)',
}
},
{ layer: 'typography' },
],
],
preflights: [
{
layer: 'typography',
getCSS: () =>
hasProseClass
? getPreflights(escapedSelector, selectorName, cssExtend)
: undefined,
},
],
}
}
export default presetTypography