Skip to content

Commit 4f76614

Browse files
committed
feat: generate theme css txt
1 parent 9c613de commit 4f76614

File tree

2 files changed

+112
-4
lines changed

2 files changed

+112
-4
lines changed

apps/web/src/utils/themeHelpers.ts

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,52 @@
1-
import type { Theme } from '@md/shared'
1+
import type { Block, Inline, Theme } from '@md/shared'
2+
import { selectorComments } from '@md/shared'
23

3-
export function generateThemeCSS(_theme: Theme) {
4-
return ``
4+
export function generateThemeCSS(theme: Theme): string {
5+
const cssLines: string[] = []
6+
7+
// 添加注释说明
8+
cssLines.push(`/**`)
9+
cssLines.push(` * 按 Alt/Option + Shift + F 可格式化`)
10+
cssLines.push(` * 如需使用主题色,请使用 var(--md-primary-color) 代替颜色值`)
11+
cssLines.push(` * 如:color: var(--md-primary-color);`)
12+
cssLines.push(` */`)
13+
cssLines.push(``)
14+
15+
// 生成基础样式(顶层容器)
16+
cssLines.push(`/* 顶层容器样式 */`)
17+
cssLines.push(`container {`)
18+
cssLines.push(`}`)
19+
cssLines.push(``)
20+
21+
// 生成块级元素样式
22+
Object.entries(theme.block).forEach(([selector, styles]) => {
23+
if (selector !== `container`) {
24+
const comment = selectorComments[selector as Block | Inline] || `${selector}样式`
25+
cssLines.push(`/* ${comment} */`)
26+
cssLines.push(`${selector} {`)
27+
Object.entries(styles).forEach(([property, value]) => {
28+
if (value) {
29+
cssLines.push(` ${property}: ${value};`)
30+
}
31+
})
32+
cssLines.push(`}`)
33+
cssLines.push(``)
34+
}
35+
})
36+
37+
// 生成内联元素样式
38+
Object.entries(theme.inline).forEach(([selector, styles]) => {
39+
const comment = selectorComments[selector as Block | Inline] || `${selector}样式`
40+
cssLines.push(`/* ${comment} */`)
41+
cssLines.push(`${selector} {`)
42+
Object.entries(styles).forEach(([property, value]) => {
43+
if (value) {
44+
cssLines.push(` ${property}: ${value};`)
45+
}
46+
})
47+
cssLines.push(`}`)
48+
cssLines.push(``)
49+
})
50+
51+
return cssLines.join(`\n`)
552
}

packages/shared/src/configs/theme.ts

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { IConfigOption, Theme } from '../types'
1+
import type { Block, IConfigOption, Inline, Theme } from '../types'
22
import { toMerged } from 'es-toolkit'
33

44
const defaultTheme: Theme = {
@@ -667,3 +667,64 @@ export const themeOptions: IConfigOption<keyof typeof themeMap>[] = [
667667
desc: `@okooo5km`,
668668
},
669669
]
670+
671+
/**
672+
* 选择器注释
673+
* 自定义 CSS 编辑器中使用
674+
*/
675+
export const selectorComments: Record<Block | Inline, string> = {
676+
container: `顶层容器样式`,
677+
h1: `一级标题样式`,
678+
h2: `二级标题样式`,
679+
h3: `三级标题样式`,
680+
h4: `四级标题样式`,
681+
h5: `五级标题样式`,
682+
h6: `六级标题样式`,
683+
image: `图片样式`,
684+
blockquote: `引用样式`,
685+
blockquote_p: `引用段落样式`,
686+
p: `段落样式`,
687+
hr: `分割线样式`,
688+
codespan: `行内代码样式`,
689+
em: `斜体样式`,
690+
strong: `粗体样式`,
691+
link: `链接样式`,
692+
wx_link: `微信链接样式`,
693+
ol: `有序列表样式`,
694+
ul: `无序列表样式`,
695+
listitem: `列表项样式`,
696+
code: `代码块样式`,
697+
code_pre: `代码块外层样式`,
698+
inline_katex: `行内公式样式`,
699+
block_katex: `公式块样式`,
700+
table: `表格样式`,
701+
thead: `表头样式`,
702+
th: `表头单元格样式`,
703+
td: `表格单元格样式`,
704+
footnotes: `脚注样式`,
705+
figure: `图表样式`,
706+
figcaption: `图表标题样式`,
707+
footnote: `脚注引用样式`,
708+
blockquote_note: `GFM note 样式`,
709+
blockquote_tip: `GFM tip 样式`,
710+
blockquote_info: `GFM info 样式`,
711+
blockquote_important: `GFM important 样式`,
712+
blockquote_warning: `GFM warning 样式`,
713+
blockquote_caution: `GFM caution 样式`,
714+
blockquote_title: `GFM 通用标题`,
715+
blockquote_title_note: `GFM note 标题`,
716+
blockquote_title_tip: `GFM tip 标题`,
717+
blockquote_title_info: `GFM info 标题`,
718+
blockquote_title_important: `GFM important 标题`,
719+
blockquote_title_warning: `GFM warning 标题`,
720+
blockquote_title_caution: `GFM caution 标题`,
721+
blockquote_p_note: `GFM note 段落样式`,
722+
blockquote_p_tip: `GFM tip 段落样式`,
723+
blockquote_p_info: `GFM info 段落样式`,
724+
blockquote_p_important: `GFM important 段落样式`,
725+
blockquote_p_warning: `GFM warning 段落样式`,
726+
blockquote_p_caution: `GFM caution 段落样式`,
727+
markup_highlight: `高亮标记样式`,
728+
markup_underline: `下划线标记样式`,
729+
markup_wavyline: `波浪线标记样式`,
730+
}

0 commit comments

Comments
 (0)