refactor: update type of theme (#369)

* chore: add csstype and es-toolkit

* chore: add @types/prettier

* refactor: update config file to use TypeScript

* refactor: update type of theme
This commit is contained in:
YangFong 2024-08-30 08:14:35 +08:00 committed by GitHub
parent fed75b307e
commit 75925064b3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 64 additions and 33 deletions

19
package-lock.json generated
View File

@ -20,7 +20,9 @@
"core-js": "^3.38.1", "core-js": "^3.38.1",
"cos-js-sdk-v5": "^1.8.4", "cos-js-sdk-v5": "^1.8.4",
"crypto-js": "^4.2.0", "crypto-js": "^4.2.0",
"csstype": "^3.1.3",
"element-plus": "^2.8.0", "element-plus": "^2.8.0",
"es-toolkit": "^1.16.0",
"form-data": "4.0.0", "form-data": "4.0.0",
"highlight.js": "^11.10.0", "highlight.js": "^11.10.0",
"juice": "^8.0.0", "juice": "^8.0.0",
@ -44,6 +46,7 @@
"@types/codemirror": "^5.60.15", "@types/codemirror": "^5.60.15",
"@types/marked": "^4.0.0", "@types/marked": "^4.0.0",
"@types/node": "^22.4.1", "@types/node": "^22.4.1",
"@types/prettier": "^2.7.3",
"@unocss/eslint-plugin": "^0.62.2", "@unocss/eslint-plugin": "^0.62.2",
"@vitejs/plugin-vue": "^5.1.2", "@vitejs/plugin-vue": "^5.1.2",
"autoprefixer": "^10.4.20", "autoprefixer": "^10.4.20",
@ -839,7 +842,6 @@
}, },
"node_modules/@clack/prompts/node_modules/is-unicode-supported": { "node_modules/@clack/prompts/node_modules/is-unicode-supported": {
"version": "1.3.0", "version": "1.3.0",
"dev": true,
"inBundle": true, "inBundle": true,
"license": "MIT", "license": "MIT",
"engines": { "engines": {
@ -2291,6 +2293,12 @@
"integrity": "sha512-37i+OaWTh9qeK4LSHPsyRC7NahnGotNuZvjLSgcPzblpHB3rrCJxAOgI5gCdKm7coonsaX1Of0ILiTcnZjbfxA==", "integrity": "sha512-37i+OaWTh9qeK4LSHPsyRC7NahnGotNuZvjLSgcPzblpHB3rrCJxAOgI5gCdKm7coonsaX1Of0ILiTcnZjbfxA==",
"dev": true "dev": true
}, },
"node_modules/@types/prettier": {
"version": "2.7.3",
"resolved": "https://registry.npmmirror.com/@types/prettier/-/prettier-2.7.3.tgz",
"integrity": "sha512-+68kP9yzs4LMp7VNh8gdzMSPZFL44MLGqiHWvttYJe+6qnuVr4Ek9wSBQoveqY/r+LwjCcU29kNVkidwim+kYA==",
"dev": true
},
"node_modules/@types/tern": { "node_modules/@types/tern": {
"version": "0.23.9", "version": "0.23.9",
"resolved": "https://registry.npmmirror.com/@types/tern/-/tern-0.23.9.tgz", "resolved": "https://registry.npmmirror.com/@types/tern/-/tern-0.23.9.tgz",
@ -5573,6 +5581,15 @@
"url": "https://github.com/sponsors/ljharb" "url": "https://github.com/sponsors/ljharb"
} }
}, },
"node_modules/es-toolkit": {
"version": "1.16.0",
"resolved": "https://registry.npmmirror.com/es-toolkit/-/es-toolkit-1.16.0.tgz",
"integrity": "sha512-eNJh3zF1KmAHRYd1D8rFi1cMFMCjrC6tumBfwuuZdSur97mED/ifyeBoGzxS11L4owCMx3XSmWTo6oxJQkdGng==",
"workspaces": [
"docs",
"benchmarks"
]
},
"node_modules/esbuild": { "node_modules/esbuild": {
"version": "0.23.1", "version": "0.23.1",
"resolved": "https://registry.npmmirror.com/esbuild/-/esbuild-0.23.1.tgz", "resolved": "https://registry.npmmirror.com/esbuild/-/esbuild-0.23.1.tgz",

View File

@ -28,7 +28,9 @@
"core-js": "^3.38.1", "core-js": "^3.38.1",
"cos-js-sdk-v5": "^1.8.4", "cos-js-sdk-v5": "^1.8.4",
"crypto-js": "^4.2.0", "crypto-js": "^4.2.0",
"csstype": "^3.1.3",
"element-plus": "^2.8.0", "element-plus": "^2.8.0",
"es-toolkit": "^1.16.0",
"form-data": "4.0.0", "form-data": "4.0.0",
"highlight.js": "^11.10.0", "highlight.js": "^11.10.0",
"juice": "^8.0.0", "juice": "^8.0.0",
@ -52,6 +54,7 @@
"@types/codemirror": "^5.60.15", "@types/codemirror": "^5.60.15",
"@types/marked": "^4.0.0", "@types/marked": "^4.0.0",
"@types/node": "^22.4.1", "@types/node": "^22.4.1",
"@types/prettier": "^2.7.3",
"@unocss/eslint-plugin": "^0.62.2", "@unocss/eslint-plugin": "^0.62.2",
"@vitejs/plugin-vue": "^5.1.2", "@vitejs/plugin-vue": "^5.1.2",
"autoprefixer": "^10.4.20", "autoprefixer": "^10.4.20",

View File

@ -1,12 +1,10 @@
import CodeMirror from 'codemirror'
export * from './api' export * from './api'
export * from './style' export * from './style'
export * from './theme' export * from './theme'
export const prefix = `MD` export const prefix = `MD`
const isMac = CodeMirror.keyMap.default === CodeMirror.keyMap.macDefault const isMac = window.navigator.platform === `MacIntel`
export const ctrlKey = isMac ? `Cmd` : `Ctrl` export const ctrlKey = isMac ? `Cmd` : `Ctrl`
export const altKey = `Alt` export const altKey = `Alt`

View File

@ -1,35 +1,17 @@
interface Theme { import { toMerged } from 'es-toolkit'
base: Record<string, string | number>
block: Record<string, Record<string, string | number>> import type { Theme } from '@/types'
inline: Record<string, Record<string, string | number>>
}
const baseColor = `#3f3f3f` const baseColor = `#3f3f3f`
function mergeTheme(defaultTheme: Theme, newTheme: Theme): Theme { const defaultTheme: Theme = {
const merge = (defaultObj: Record<string, any>, newObj: Record<string, any>) => {
const result: Record<string, any> = {}
for (const key in defaultObj) {
result[key] = { ...defaultObj[key], ...newObj?.[key] }
}
return result
}
return {
base: { ...defaultTheme.base, ...newTheme.base },
block: merge(defaultTheme.block, newTheme.block),
inline: merge(defaultTheme.inline, newTheme.inline),
}
}
const defaultTheme = {
base: { base: {
'--md-primary-color': `#000000`, '--md-primary-color': `#000000`,
'text-align': `left`, 'text-align': `left`,
'line-height': `1.75`, 'line-height': `1.75`,
}, },
block: { block: {
// 一级标题样式 // 一级标题
h1: { h1: {
'display': `table`, 'display': `table`,
'padding': `0 1em`, 'padding': `0 1em`,
@ -41,7 +23,7 @@ const defaultTheme = {
'text-align': `center`, 'text-align': `center`,
}, },
// 二级标题样式 // 二级标题
h2: { h2: {
'display': `table`, 'display': `table`,
'padding': `0 0.2em`, 'padding': `0 0.2em`,
@ -53,7 +35,7 @@ const defaultTheme = {
'text-align': `center`, 'text-align': `center`,
}, },
// 三级标题样式 // 三级标题
h3: { h3: {
'padding-left': `8px`, 'padding-left': `8px`,
'border-left': `3px solid var(--md-primary-color)`, 'border-left': `3px solid var(--md-primary-color)`,
@ -64,7 +46,7 @@ const defaultTheme = {
'line-height': `1.2`, 'line-height': `1.2`,
}, },
// 四级标题样式 // 四级标题
h4: { h4: {
'margin': `2em 8px 0.5em`, 'margin': `2em 8px 0.5em`,
'color': `var(--md-primary-color)`, 'color': `var(--md-primary-color)`,
@ -72,7 +54,7 @@ const defaultTheme = {
'font-weight': `bold`, 'font-weight': `bold`,
}, },
// 段落样式 // 段落
p: { p: {
'margin': `1.5em 8px`, 'margin': `1.5em 8px`,
'letter-spacing': `0.1em`, 'letter-spacing': `0.1em`,
@ -80,7 +62,7 @@ const defaultTheme = {
'text-align': `justify`, 'text-align': `justify`,
}, },
// 引用样式 // 引用
blockquote: { blockquote: {
'font-style': `normal`, 'font-style': `normal`,
'border-left': `none`, 'border-left': `none`,
@ -91,12 +73,15 @@ const defaultTheme = {
'margin': `2em 8px`, 'margin': `2em 8px`,
}, },
// 引用内容
blockquote_p: { blockquote_p: {
'display': `block`, 'display': `block`,
'font-size': `1em`, 'font-size': `1em`,
'letter-spacing': `0.1em`, 'letter-spacing': `0.1em`,
'color': `rgb(80, 80, 80)`, 'color': `rgb(80, 80, 80)`,
}, },
// 代码块
code_pre: { code_pre: {
'font-size': `14px`, 'font-size': `14px`,
'overflow-x': `auto`, 'overflow-x': `auto`,
@ -105,12 +90,15 @@ const defaultTheme = {
'line-height': `1.5`, 'line-height': `1.5`,
'margin': `10px 8px`, 'margin': `10px 8px`,
}, },
// 行内代码
code: { code: {
'margin': 0, 'margin': 0,
'white-space': `nowrap`, 'white-space': `nowrap`,
'font-family': `Menlo, Operator Mono, Consolas, Monaco, monospace`, 'font-family': `Menlo, Operator Mono, Consolas, Monaco, monospace`,
}, },
// 图片
image: { image: {
'display': `block`, 'display': `block`,
'width': `100% !important`, 'width': `100% !important`,
@ -118,12 +106,14 @@ const defaultTheme = {
'border-radius': `4px`, 'border-radius': `4px`,
}, },
// 有序列表
ol: { ol: {
'padding-left': `1em`, 'padding-left': `1em`,
'margin-left': `0`, 'margin-left': `0`,
'color': `var(--el-text-color-regular)`, 'color': `var(--el-text-color-regular)`,
}, },
// 无序列表
ul: { ul: {
'list-style': `circle`, 'list-style': `circle`,
'padding-left': `1em`, 'padding-left': `1em`,
@ -216,7 +206,7 @@ const defaultTheme = {
}, },
} }
const graceTheme = mergeTheme(defaultTheme, { const graceTheme = toMerged(defaultTheme, {
base: { base: {
}, },
block: { block: {
@ -284,6 +274,14 @@ const graceTheme = mergeTheme(defaultTheme, {
'padding-left': `1.5em`, 'padding-left': `1.5em`,
}, },
footnotes: {
},
figure: {
},
hr: { hr: {
height: `1px`, height: `1px`,
border: `none`, border: `none`,
@ -329,6 +327,10 @@ const graceTheme = mergeTheme(defaultTheme, {
footnote: { footnote: {
color: `rgba(0,0,0,0.5)`, color: `rgba(0,0,0,0.5)`,
}, },
figcaption: {
},
}, },
}) })

View File

@ -1,3 +1,14 @@
import type { PropertiesHyphen } from 'csstype'
type Block = `h1` | `h2` | `h3` | `h4` | `p` | `blockquote` | `blockquote_p` | `code_pre` | `code` | `image` | `ol` | `ul` | `footnotes` | `figure` | `hr`
type Inline = `listitem` | `codespan` | `link` | `wx_link` | `strong` | `table` | `thead` | `td` | `footnote` | `figcaption`
export interface Theme {
base: PropertiesHyphen & { [`--md-primary-color`]?: string }
block: Record<Block, PropertiesHyphen>
inline: Record<Inline, PropertiesHyphen>
}
export interface IConfigOption { export interface IConfigOption {
label: string label: string
value: string value: string