Skip to content

Commit fc7702a

Browse files
committed
refactor: move normalizeColor function to styleUtils and update its usage in markdown styles
1 parent 5f4315f commit fc7702a

3 files changed

Lines changed: 15 additions & 22 deletions

File tree

src/normalizeMarkdownInputStyle.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { processColor, type ColorValue } from 'react-native';
22
import type { MarkdownInputStyle } from './EnrichedMarkdownInput';
3+
import { normalizeColor } from './styleUtils';
34

45
interface MarkdownInputStyleInternal {
56
strong: {
@@ -18,9 +19,6 @@ interface MarkdownInputStyleInternal {
1819
};
1920
}
2021

21-
const normalizeColor = (color: string | undefined): ColorValue | undefined =>
22-
color ? processColor(color) : undefined;
23-
2422
const DEFAULT_LINK_COLOR = '#2563EB';
2523
const DEFAULT_SPOILER_COLOR = '#374151';
2624
const DEFAULT_SPOILER_BG_COLOR = '#E5E7EB';

src/normalizeMarkdownStyle.ts

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
1-
import { Platform, processColor } from 'react-native';
1+
import { Platform } from 'react-native';
22
import type { MarkdownStyle } from './types/MarkdownStyle';
33
import type {
44
BlockTextAlign,
55
EmphasisFontStyle,
66
MarkdownStyleInternal,
77
} from './types/MarkdownStyleInternal';
8-
import { mergeSpoilerDefaults, isStyleEqual } from './styleUtils';
9-
10-
// On native, processColor converts hex strings to ARGB integers the renderer
11-
// expects. On web, CSS accepts hex strings natively — no conversion needed.
12-
// MarkdownStyleInternal types colors as `string`; native consumers
13-
// (EnrichedMarkdownTextNativeComponent) accept `ColorValue` (string | number)
14-
// at runtime, so the ARGB integers processColor produces are handled correctly.
15-
export const normalizeColor = (
16-
color: string | undefined
17-
): string | undefined =>
18-
color
19-
? Platform.OS === 'web'
20-
? color
21-
: ((processColor(color) ?? undefined) as string | undefined)
22-
: undefined;
8+
import {
9+
mergeSpoilerDefaults,
10+
isStyleEqual,
11+
normalizeColor,
12+
} from './styleUtils';
2313

2414
const getSystemFont = (): string =>
2515
Platform.select({
@@ -75,7 +65,7 @@ const baseHeader: {
7565
textAlign: 'auto',
7666
};
7767

78-
const DEFAULT_NORMALIZED_STYLE: MarkdownStyleInternal = Object.freeze({
68+
const DEFAULT_NORMALIZED_STYLE = Object.freeze({
7969
paragraph: {
8070
fontSize: 16,
8171
fontFamily: getSystemFont(),
@@ -237,7 +227,7 @@ const DEFAULT_NORMALIZED_STYLE: MarkdownStyleInternal = Object.freeze({
237227
particles: { density: 8, speed: 20 },
238228
solid: { borderRadius: 4 },
239229
},
240-
});
230+
} as MarkdownStyleInternal);
241231

242232
const refCache = new WeakMap<MarkdownStyle, MarkdownStyleInternal>();
243233
const structuralCache: {

src/styleUtils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1+
import { processColor, type ColorValue } from 'react-native';
12
import type { MarkdownStyle } from './types/MarkdownStyle';
23
import type { MarkdownStyleInternal } from './types/MarkdownStyleInternal';
34

5+
export const normalizeColor = (
6+
color: string | undefined
7+
): ColorValue | undefined => (color ? processColor(color) : undefined);
8+
49
export function mergeSpoilerDefaults(
510
user: MarkdownStyle['spoiler'],
611
defaults: MarkdownStyleInternal['spoiler']
712
): MarkdownStyleInternal['spoiler'] {
813
return {
9-
color: user?.color ?? defaults.color,
14+
color: (normalizeColor(user?.color) as string) ?? defaults.color,
1015
particles: {
1116
density: user?.particles?.density ?? defaults.particles.density,
1217
speed: user?.particles?.speed ?? defaults.particles.speed,

0 commit comments

Comments
 (0)