forked from mxx1111/mdlook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcssVariables.ts
More file actions
101 lines (87 loc) · 2.38 KB
/
Copy pathcssVariables.ts
File metadata and controls
101 lines (87 loc) · 2.38 KB
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
/**
* CSS 变量生成工具
* 根据配置动态生成 CSS 变量样式
*/
import type { HeadingLevel, HeadingStyles, HeadingStyleType } from '@md/shared/configs'
export interface CSSVariableConfig {
primaryColor: string
fontFamily: string
fontSize: string
isUseIndent?: boolean
isUseJustify?: boolean
headingStyles?: HeadingStyles
}
/**
* 生成 CSS 变量样式
* @param config - 配置对象
* @returns CSS 变量字符串
*/
export function generateCSSVariables(config: CSSVariableConfig): string {
return `
:root {
/* 动态配置变量 */
--md-primary-color: ${config.primaryColor};
--md-font-family: ${config.fontFamily};
--md-font-size: ${config.fontSize};
}
/* 段落缩进和对齐 */
#output p {
${config.isUseIndent ? 'text-indent: 2em;' : ''}
${config.isUseJustify ? 'text-align: justify;' : ''}
}
`.trim()
}
/**
* 生成标题样式 CSS(单独导出,用于在主题 CSS 之后应用)
*/
export function generateHeadingStyles(config: CSSVariableConfig): string {
return generateHeadingStylesCSS(config.headingStyles)
}
/**
* 生成标题样式 CSS
*/
function generateHeadingStylesCSS(headingStyles?: HeadingStyles): string {
if (!headingStyles)
return ``
const levels: HeadingLevel[] = [`h1`, `h2`, `h3`, `h4`, `h5`, `h6`]
const cssRules: string[] = []
for (const level of levels) {
const style = headingStyles[level]
// 自定义样式由用户在 CSS 编辑器中直接编辑,这里只处理预设样式
if (style && style !== `default` && style !== `custom`) {
cssRules.push(generateHeadingCSS(level, style))
}
}
return cssRules.join(`\n\n`)
}
/**
* 生成单个标题级别的样式 CSS
*/
function generateHeadingCSS(level: HeadingLevel, style: HeadingStyleType): string {
const baseStyles = `
display: block;
text-align: left;
background: transparent;`
switch (style) {
case `color-only`:
return `#output ${level} {
color: var(--md-primary-color);
background: transparent;
}`
case `border-bottom`:
return `#output ${level} {${baseStyles}
padding-bottom: 0.3em;
border-bottom: 2px solid var(--md-primary-color);
color: var(--md-primary-color);
}`
case `border-left`:
return `#output ${level} {${baseStyles}
margin-left: 0;
padding-left: 10px;
border-left: 4px solid var(--md-primary-color);
color: var(--md-primary-color);
}`
default:
return ``
}
}