dsh-fonttune 0.1.6 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -7,35 +7,51 @@
7
7
  * in the Host user-settings document next to every other preference. The
8
8
  * plugin's own composition entry is the base layer, so a value the user
9
9
  * clears falls back to the profile's config rather than to nothing.
10
- * 2. Contribute a `<style>` row to the served index. The browser half applies
11
- * the same declarations once it activates, but that is after the shell has
12
- * already painted; injecting here is what keeps the first frame from
13
- * flashing DSH's default typography.
10
+ * 2. Contribute the first-frame stylesheet to the served index as an `html` row.
11
+ * The browser half applies the same declarations once it activates, but that
12
+ * is after the shell has already painted; injecting here is what keeps the
13
+ * first frame from flashing DSH's default typography. The row carries this
14
+ * plugin's `data-plugin-css` stamp on purpose: the browser half adopts that
15
+ * element instead of appending a second copy, because a served copy is only
16
+ * rebuilt on the next index render and could never drop a rule the settings
17
+ * no longer produce. When `perTheme` is on, the row carries the dark theme's
18
+ * rules prefixed with the dark-theme attribute, so the first paint of each
19
+ * theme already uses that theme's own values.
14
20
  *
15
21
  * @module dsh-fonttune
16
22
  */
17
23
  import z from "@deepseek-ai/schemastery";
18
24
  // Default import, not named: `shared.cjs` is a CommonJS module, and Node's
19
25
  // CJS-to-ESM interop only guarantees `module.exports` as the default binding
20
- // (staticky-detected named exports would break this half at boot).
26
+ // (statically-detected named exports would break this half at boot).
21
27
  import shared from "./shared.cjs";
22
28
 
23
29
  const {
24
30
  buildFontCss,
31
+ CODE_LINE_HEIGHT_FIELD,
25
32
  CODE_SIZE_FIELD,
26
33
  CODE_WEIGHT_FIELD,
34
+ DARK_VALUES_FIELD,
27
35
  FALLBACK_TOKENS,
36
+ FEATURES_FIELD,
37
+ LIGATURES_FIELD,
38
+ LINE_HEIGHT_DIALOG_FIELD,
39
+ LINE_HEIGHT_FIELD,
40
+ MAX_FEATURES_LENGTH,
41
+ MAX_STACK_LENGTH,
28
42
  MONO_FIELD,
29
43
  NAMESPACE,
30
44
  normalizeConfig,
45
+ NO_SYNTHETIC_BOLD_FIELD,
46
+ NO_SYNTHETIC_ITALIC_FIELD,
47
+ PER_THEME_FIELD,
31
48
  SANS_FIELD,
49
+ SIZE_DIALOG_FIELD,
32
50
  SIZE_FIELD,
33
- SIZE_MAX,
34
- SIZE_MIN,
51
+ STACK_DIALOG_FIELD,
52
+ STYLE_TAG,
53
+ WEIGHT_DIALOG_FIELD,
35
54
  WEIGHT_FIELD,
36
- WEIGHT_MAX,
37
- WEIGHT_MIN,
38
- WEIGHT_UNSET,
39
55
  } = shared;
40
56
 
41
57
  /**
@@ -49,14 +65,22 @@ const SAFE_STACK = /^[^{};<>\\]*$/;
49
65
  */
50
66
  const MAX_STACK = 200;
51
67
 
68
+ /**
69
+ * A `font-feature-settings` value may only carry feature tags, on/off/number
70
+ * and the entry separators — anything else cannot appear in a valid value.
71
+ */
72
+ const SAFE_FEATURES = /^[a-zA-Z0-9"' ,]*$/;
73
+
52
74
  /**
53
75
  * The durable settings section.
54
76
  *
55
77
  * Every field defaults to "leave DSH alone": an empty stack injects no family
56
- * rule, a zero offset injects no size rule on either axis, and a zero weight
57
- * injects no weight rule (an unset CODE weight still writes the rule that keeps
58
- * code out of the body weight). Installing the plugin therefore changes nothing
59
- * until the user asks for something.
78
+ * rule, a zero offset injects no size rule, a ratio of 100 injects no
79
+ * line-height rule, and a zero weight injects no weight rule (an unset code
80
+ * weight still writes the rule that keeps code out of the interface/dialog
81
+ * weights). A dialog field left at its neutral value follows the interface
82
+ * value. Installing the plugin therefore changes nothing until the user asks
83
+ * for something.
60
84
  */
61
85
  export const Config = z.object({
62
86
  [SANS_FIELD]: z
@@ -64,7 +88,15 @@ export const Config = z.object({
64
88
  .max(MAX_STACK)
65
89
  .pattern(SAFE_STACK)
66
90
  .default("")
67
- .description("CSS font-family list for the UI and conversation text"),
91
+ .description("CSS font-family list for the interface text"),
92
+ [STACK_DIALOG_FIELD]: z
93
+ .string()
94
+ .max(MAX_STACK)
95
+ .pattern(SAFE_STACK)
96
+ .default("")
97
+ .description(
98
+ "CSS font-family list for the conversation markdown; empty follows the interface stack"
99
+ ),
68
100
  [MONO_FIELD]: z
69
101
  .string()
70
102
  .max(MAX_STACK)
@@ -73,51 +105,155 @@ export const Config = z.object({
73
105
  .description("CSS font-family list for code and monospaced text"),
74
106
  [SIZE_FIELD]: z
75
107
  .number()
76
- .min(SIZE_MIN)
77
- .max(SIZE_MAX)
108
+ .min(shared.SIZE_MIN)
109
+ .max(shared.SIZE_MAX)
78
110
  .default(0)
79
111
  .description(
80
- `Body/UI font-size offset in px (${SIZE_MIN}..${SIZE_MAX}, 0 keeps DSH's own sizes)`
112
+ `Interface font-size offset in px (${shared.SIZE_MIN}..${shared.SIZE_MAX}, 0 keeps DSH's own sizes)`
113
+ ),
114
+ [SIZE_DIALOG_FIELD]: z
115
+ .number()
116
+ .min(shared.SIZE_MIN)
117
+ .max(shared.SIZE_MAX)
118
+ .default(0)
119
+ .description(
120
+ "Conversation font-size offset in px; 0 follows the interface offset"
81
121
  ),
82
122
  [CODE_SIZE_FIELD]: z
83
123
  .number()
84
- .min(SIZE_MIN)
85
- .max(SIZE_MAX)
124
+ .min(shared.SIZE_MIN)
125
+ .max(shared.SIZE_MAX)
86
126
  .default(0)
87
127
  .description(
88
- `Code font-size offset in px (${SIZE_MIN}..${SIZE_MAX}, 0 keeps DSH's own sizes; independent of the body offset)`
128
+ `Code font-size offset in px (${shared.SIZE_MIN}..${shared.SIZE_MAX}, 0 keeps DSH's own sizes; independent of the interface offset)`
89
129
  ),
90
130
  [WEIGHT_FIELD]: z
91
131
  .number()
92
- .min(WEIGHT_UNSET)
93
- .max(WEIGHT_MAX)
94
- .default(WEIGHT_UNSET)
132
+ .min(shared.WEIGHT_UNSET)
133
+ .max(shared.WEIGHT_MAX)
134
+ .default(shared.WEIGHT_UNSET)
95
135
  .description(
96
- `Body/UI font weight (${WEIGHT_MIN}..${WEIGHT_MAX}, ${WEIGHT_UNSET} keeps DSH's own weights; code is not affected)`
136
+ `Interface font weight (${shared.WEIGHT_MIN}..${shared.WEIGHT_MAX}, ${shared.WEIGHT_UNSET} keeps DSH's own weights; code is not affected)`
137
+ ),
138
+ [WEIGHT_DIALOG_FIELD]: z
139
+ .number()
140
+ .min(shared.WEIGHT_UNSET)
141
+ .max(shared.WEIGHT_MAX)
142
+ .default(shared.WEIGHT_UNSET)
143
+ .description(
144
+ "Conversation font weight; 0 follows the interface weight"
97
145
  ),
98
146
  [CODE_WEIGHT_FIELD]: z
99
147
  .number()
100
- .min(WEIGHT_UNSET)
101
- .max(WEIGHT_MAX)
102
- .default(WEIGHT_UNSET)
148
+ .min(shared.WEIGHT_UNSET)
149
+ .max(shared.WEIGHT_MAX)
150
+ .default(shared.WEIGHT_UNSET)
151
+ .description(
152
+ `Code font weight (${shared.WEIGHT_MIN}..${shared.WEIGHT_MAX}, ${shared.WEIGHT_UNSET} keeps DSH's own weights; independent of the interface weight)`
153
+ ),
154
+ [LINE_HEIGHT_FIELD]: z
155
+ .number()
156
+ .min(shared.LINE_HEIGHT_MIN)
157
+ .max(shared.LINE_HEIGHT_MAX)
158
+ .default(shared.LINE_HEIGHT_MIN)
103
159
  .description(
104
- `Code font weight (${WEIGHT_MIN}..${WEIGHT_MAX}, ${WEIGHT_UNSET} keeps DSH's own weights; independent of the body weight)`
160
+ "Interface line-height ratio in percent (100 keeps DSH's own line heights)"
105
161
  ),
162
+ [LINE_HEIGHT_DIALOG_FIELD]: z
163
+ .number()
164
+ .min(shared.LINE_HEIGHT_MIN)
165
+ .max(shared.LINE_HEIGHT_MAX)
166
+ .default(shared.LINE_HEIGHT_MIN)
167
+ .description(
168
+ "Conversation line-height ratio in percent; 100 follows the interface ratio"
169
+ ),
170
+ [CODE_LINE_HEIGHT_FIELD]: z
171
+ .number()
172
+ .min(shared.CODE_LINE_HEIGHT_MIN)
173
+ .max(shared.CODE_LINE_HEIGHT_MAX)
174
+ .default(0)
175
+ .description(
176
+ "Code line-height offset in px (0 keeps DSH's own line heights)"
177
+ ),
178
+ [LIGATURES_FIELD]: z
179
+ .number()
180
+ .min(0)
181
+ .max(2)
182
+ .default(0)
183
+ .description("Code ligatures: 0 DSH default, 1 forced on, 2 forced off"),
184
+ [FEATURES_FIELD]: z
185
+ .string()
186
+ .max(MAX_FEATURES_LENGTH)
187
+ .pattern(SAFE_FEATURES)
188
+ .default("")
189
+ .description("Advanced font-feature-settings value for code"),
190
+ [NO_SYNTHETIC_ITALIC_FIELD]: z
191
+ .boolean()
192
+ .default(false)
193
+ .description("Refuse synthetic italic (stops CJK text from being tilted)"),
194
+ [NO_SYNTHETIC_BOLD_FIELD]: z
195
+ .boolean()
196
+ .default(false)
197
+ .description("Refuse synthetic bold (for faces without a real bold)"),
198
+ [shared.UI_FOLLOWS_FIELD]: z
199
+ .boolean()
200
+ .default(true)
201
+ .description(
202
+ "The interface follows the conversation: its family and weight take the conversation's values (off = the interface keeps its own)"
203
+ ),
204
+ [PER_THEME_FIELD]: z
205
+ .boolean()
206
+ .default(false)
207
+ .description("Keep a separate value set for the dark theme"),
208
+ [DARK_VALUES_FIELD]: z
209
+ .string()
210
+ .max(8000)
211
+ .default("{}")
212
+ .description("Dark theme sparse overrides (JSON map of axis field to value)"),
213
+ [shared.PRESETS_FIELD]: z
214
+ .string()
215
+ .max(20000)
216
+ .default("[]")
217
+ .description("Saved presets (JSON array of {name, values, savedAt})"),
218
+ [shared.ACTIVE_PRESET_FIELD]: z
219
+ .string()
220
+ .max(shared.MAX_PRESET_NAME)
221
+ .default("")
222
+ .description("Name of the preset the card currently edits (edits auto-save into it)"),
106
223
  });
107
224
 
108
225
  /**
109
226
  * One row of the structured index injection table.
110
- * @typedef {{kind: "style", text: string}} StyleRow
227
+ * @typedef {{kind: "style", text: string} | {kind: "html", placement: "head", html: string}} StyleRow
111
228
  */
112
229
 
113
230
  /**
114
231
  * The style row carrying one configuration.
232
+ *
233
+ * Deliberately an `html` row rather than a `style` row: the served `<style>`
234
+ * has to carry the same `data-plugin-css` stamp the browser half writes, so the
235
+ * browser half can ADOPT this element instead of appending a second one. Two
236
+ * copies of the stylesheet cannot be kept in sync — the served row is only
237
+ * rebuilt on the next index render, so a rule the settings no longer produce
238
+ * would keep applying from the stale copy until the user reloads the page.
115
239
  * @param {unknown} resolved - the current settings section.
116
240
  * @returns {StyleRow} the row the web server renders into `<head>`.
117
241
  */
118
242
  function styleRow(resolved) {
119
- const text = buildFontCss(normalizeConfig(resolved), FALLBACK_TOKENS);
120
- return { kind: "style", text };
243
+ const cfg = normalizeConfig(resolved);
244
+ // 0.2.0 ships the per-theme editor out; light and dark stay one value set
245
+ // (the dark fields remain in the schema, dormant, for a future release).
246
+ cfg[PER_THEME_FIELD] = false;
247
+ const text = buildFontCss(cfg, FALLBACK_TOKENS);
248
+ // The family sanitizer already refuses `<`, `>` and `/`, so no configuration
249
+ // can close the element early; the guard is here because this row is raw
250
+ // markup and a future input path must not be able to break out of it.
251
+ const safe = text.includes("</style") ? "" : text;
252
+ return {
253
+ kind: "html",
254
+ placement: "head",
255
+ html: `<style data-plugin="${NAMESPACE}" data-plugin-css="${STYLE_TAG}">${safe}</style>`,
256
+ };
121
257
  }
122
258
 
123
259
  /**