| 1 | import type { CSSProperties } from 'vue' |
| 2 | |
| 3 | /** |
| 4 | * Resolve urls from frontmatter and append with the base url |
| 5 | */ |
| 6 | export function resolveAssetUrl(url: string) { |
| 7 | if (url.startsWith('/')) |
| 8 | return import.meta.env.BASE_URL + url.slice(1) |
| 9 | return url |
| 10 | } |
| 11 | |
| 12 | export function handleBackground(background?: string, dim = false, backgroundSize = 'cover'): CSSProperties { |
| 13 | const isColor = background && (background[0] === '#' || background.startsWith('rgb')) |
| 14 | |
| 15 | const style = { |
| 16 | background: isColor |
| 17 | ? background |
| 18 | : undefined, |
| 19 | color: (background && !isColor) |
| 20 | ? 'white' |
| 21 | : undefined, |
| 22 | backgroundImage: isColor |
| 23 | ? undefined |
| 24 | : background |
| 25 | ? dim |
| 26 | ? `linear-gradient(#0005, #0008), url(${resolveAssetUrl(background)})` |
| 27 | : `url("${resolveAssetUrl(background)}")` |
| 28 | : undefined, |
| 29 | backgroundRepeat: 'no-repeat', |
| 30 | backgroundPosition: 'center', |
| 31 | backgroundSize, |
| 32 | } |
| 33 | |
| 34 | if (!style.background) |
| 35 | delete style.background |
| 36 | |
| 37 | return style |
| 38 | } |
| 39 |