Parse an inline CSS style string from Pierre's highlighted HAST output.
(styleValue: unknown)
| 187 | |
| 188 | /** Parse an inline CSS style string from Pierre's highlighted HAST output. */ |
| 189 | function parseStyleValue(styleValue: unknown) { |
| 190 | if (typeof styleValue !== "string") { |
| 191 | return EMPTY_STYLE_VALUES; |
| 192 | } |
| 193 | |
| 194 | const cached = parsedStyleValueCache.get(styleValue); |
| 195 | if (cached) { |
| 196 | return cached; |
| 197 | } |
| 198 | |
| 199 | const styles = new Map<string, string>(); |
| 200 | for (const segment of styleValue.split(";")) { |
| 201 | const separator = segment.indexOf(":"); |
| 202 | if (separator <= 0) { |
| 203 | continue; |
| 204 | } |
| 205 | |
| 206 | const key = segment.slice(0, separator).trim(); |
| 207 | const value = segment.slice(separator + 1).trim(); |
| 208 | if (key && value) { |
| 209 | styles.set(key, value); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | parsedStyleValueCache.set(styleValue, styles); |
| 214 | return styles; |
| 215 | } |
| 216 | |
| 217 | const RESERVED_PIERRE_TOKEN_COLORS = { |
| 218 | dark: { |