forked from Milkdown/milkdown
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c1d0a24
commit 1feda21
Showing
7 changed files
with
167 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { MilkdownPlugin } from '../utility'; | ||
import { createCtx } from '../context'; | ||
import { Config } from './config'; | ||
|
||
type PR<K extends string, V = string> = Partial<Record<K, V>>; | ||
|
||
const themeColor = (hex: string) => { | ||
const hex2rgb = (hex: string) => { | ||
const rgbShorthandRegex = /^([a-f\d])([a-f\d])([a-f\d])$/i; | ||
const rgbRegex = /^([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i; | ||
const parse16 = (x: string) => parseInt(x, 16); | ||
|
||
const fullHex = hex.slice(1).replace(rgbShorthandRegex, (_, r, g, b) => { | ||
return r + r + g + g + b + b; | ||
}); | ||
|
||
const [ok, r, g, b] = fullHex.match(rgbRegex) || []; | ||
|
||
return ok ? [r, g, b].map(parse16) : null; | ||
}; | ||
|
||
const rgb = hex2rgb(hex); | ||
if (!rgb) { | ||
console.warn(`Invalid hex: ${hex}`); | ||
return hex; | ||
} | ||
|
||
return rgb.join(', '); | ||
}; | ||
|
||
export type Color = 'neutral' | 'solid' | 'shadow' | 'primary' | 'secondary' | 'line' | 'background' | 'surface'; | ||
|
||
export type Font = 'font' | 'fontCode'; | ||
|
||
export type Size = 'radius' | 'lineWidth'; | ||
|
||
export type Widget = 'icon' | 'scrollbar' | 'shadow' | 'border'; | ||
|
||
export type ThemePack = { | ||
color?: { light?: PR<Color>; dark?: PR<Color> } & PR<Color>; | ||
font?: PR<Font, string[]>; | ||
size?: PR<Size>; | ||
widget?: PR<Widget, string>; | ||
}; | ||
|
||
export type ThemeTool = { | ||
size: Partial<Record<Size, string>>; | ||
widget: Partial<Record<Widget, string>>; | ||
palette: (key: Color, alpha: number) => string; | ||
fonts: (key: Font) => string; | ||
}; | ||
export const themeToolCtx = createCtx<ThemeTool>({ | ||
size: {}, | ||
widget: {}, | ||
fonts: () => '', | ||
palette: () => '', | ||
}); | ||
export const themePackCtx = createCtx<ThemePack>({}); | ||
export const isDarkCtx = createCtx(false); | ||
|
||
export const theme: MilkdownPlugin = (pre) => { | ||
pre.inject(themeToolCtx).inject(isDarkCtx).inject(themePackCtx, {}); | ||
return async (ctx) => { | ||
await ctx.wait(Config); | ||
const isDark = ctx.get(isDarkCtx); | ||
const themePack = ctx.get(themePackCtx); | ||
const { color, font, size = {}, widget = {} } = themePack; | ||
const palette = (key: Color, alpha: number) => { | ||
const value = color?.[isDark ? 'dark' : 'light']?.[key] ?? color?.[key]; | ||
return value ? `rgba(${themeColor(value)}, ${alpha})` : ''; | ||
}; | ||
const fonts = (key: Font) => { | ||
const value = font?.[key] ?? []; | ||
return value.join(', '); | ||
}; | ||
|
||
ctx.set(themeToolCtx, { | ||
size, | ||
widget, | ||
palette, | ||
fonts, | ||
}); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import type { ThemePack } from '@milkdown/core'; | ||
|
||
const font = [ | ||
'Roboto', | ||
'HelveticaNeue-Light', | ||
'Helvetica Neue Light', | ||
'Helvetica Neue', | ||
'Helvetica', | ||
'Arial', | ||
'Lucida Grande', | ||
'sans-serif', | ||
]; | ||
|
||
const fontCode = ['Consolas', 'Monaco', 'Andale Mono', 'Ubuntu Mono', 'monospace']; | ||
|
||
export const Nord = { | ||
nord0: '#2e3440', | ||
nord1: '#3b4252', | ||
nord2: '#434c5e', | ||
nord3: '#4c566a', | ||
nord4: '#d8dee9', | ||
nord5: '#e5e9f0', | ||
nord6: '#eceff4', | ||
nord7: '#8fbcbb', | ||
nord8: '#88c0d0', | ||
nord9: '#81a1c1', | ||
nord10: '#5e81ac', | ||
nord11: '#bf616a', | ||
nord12: '#d08770', | ||
nord13: '#ebcb8b', | ||
nord14: '#a3be8c', | ||
nord15: '#b48ead', | ||
}; | ||
|
||
export const nord: ThemePack = { | ||
font: { | ||
font, | ||
fontCode, | ||
}, | ||
color: { | ||
shadow: Nord.nord1, | ||
primary: Nord.nord10, | ||
secondary: Nord.nord9, | ||
light: { | ||
neutral: Nord.nord0, | ||
solid: Nord.nord3, | ||
line: Nord.nord4, | ||
background: Nord.nord6, | ||
surface: '#fff', | ||
}, | ||
dark: { | ||
neutral: Nord.nord6, | ||
solid: Nord.nord4, | ||
line: Nord.nord2, | ||
background: '#252932', | ||
surface: Nord.nord0, | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"outDir": "lib" | ||
}, | ||
"include": ["src"], | ||
"references": [ | ||
{ "path": "../core" }, | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters