satoru-render 1.0.4 → 1.0.6

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/README.md CHANGED
@@ -362,6 +362,7 @@ npx satoru-render input.html -f webp --verbose
362
362
  | `css` | `string` | Extra CSS to inject into the document. |
363
363
  | `baseUrl` | `string` | Base URL for relative path resolution. |
364
364
  | `logLevel` | `LogLevel` | Verbosity: `None`, `Error`, `Warning`, `Info`, `Debug`. |
365
+ | `mediaType` | `"screen" \| "print"` | CSS media type for `@media` queries. Default: `"screen"`. |
365
366
 
366
367
  ---
367
368
 
package/dist/cli.js CHANGED
@@ -30,6 +30,9 @@ async function main() {
30
30
  else if (arg === "--verbose") {
31
31
  options.verbose = true;
32
32
  }
33
+ else if (arg === "--media") {
34
+ options.mediaType = args[++i];
35
+ }
33
36
  else if (arg === "--help") {
34
37
  printHelp();
35
38
  return;
@@ -67,6 +70,7 @@ async function main() {
67
70
  height: options.height,
68
71
  format: options.format,
69
72
  logLevel: options.verbose ? LogLevel.Debug : LogLevel.None,
73
+ mediaType: options.mediaType,
70
74
  css: "body { background-color: white; }",
71
75
  };
72
76
  if (options.verbose) {
@@ -152,6 +156,7 @@ Options:
152
156
  -h, --height <number> Viewport height (default: 0, auto-calculate)
153
157
  -f, --format <format> Output format: svg, png, webp, pdf
154
158
  --no-jsdom Disable JSDOM hydration (enabled by default)
159
+ --media <type> Media type: screen, print (default: screen)
155
160
  --verbose Enable detailed logging
156
161
  --help Show this help message
157
162
  `);
package/dist/core.d.ts CHANGED
@@ -85,6 +85,8 @@ export interface RenderOptions {
85
85
  fontMap?: Record<string, string>;
86
86
  logLevel?: LogLevel;
87
87
  onLog?: (level: LogLevel, message: string) => void;
88
+ /** Media type for CSS @media queries (default: "screen") */
89
+ mediaType?: "screen" | "print";
88
90
  }
89
91
  export declare const DEFAULT_FONT_MAP: Record<string, string>;
90
92
  export declare function resolveGoogleFonts(resource: RequiredResource, userAgent?: string): Promise<ResolvedFontResult | Uint8Array | null>;
@@ -118,6 +120,7 @@ export declare abstract class SatoruBase {
118
120
  backgroundColor?: string;
119
121
  format?: "svg" | "png" | "webp" | "pdf";
120
122
  textToPaths?: boolean;
123
+ mediaType?: "screen" | "print";
121
124
  }): Promise<string | Uint8Array>;
122
125
  destroyInstance(inst: any): Promise<void>;
123
126
  loadFallbackFont(data: Uint8Array): Promise<void>;
package/dist/core.js CHANGED
@@ -240,6 +240,7 @@ export class SatoruBase {
240
240
  fitPositionX: options.fitPosition?.x ?? 0.5,
241
241
  fitPositionY: options.fitPosition?.y ?? 0.5,
242
242
  backgroundColor: this.parseColor(options.backgroundColor),
243
+ mediaType: options.mediaType === "print" ? 1 : 0,
243
244
  });
244
245
  if (!result) {
245
246
  if (options.format === "svg")
@@ -469,10 +470,39 @@ export class SatoruBase {
469
470
  // Handle regular Uint8Array / ArrayBufferView
470
471
  if (data instanceof Uint8Array ||
471
472
  ArrayBuffer.isView(data)) {
472
- const uint8 = data instanceof Uint8Array
473
+ let finalUint8 = data instanceof Uint8Array
473
474
  ? data
474
475
  : new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
475
- await loadResourceData(r, uint8);
476
+ if (r.type === "css") {
477
+ const cssText = new TextDecoder().decode(finalUint8);
478
+ const isAbsolute = /^[a-z][a-z0-9+.-]*:/i.test(r.url) || r.url.startsWith("data:");
479
+ let cssBaseUrl = r.url;
480
+ if (!isAbsolute && baseUrl) {
481
+ try {
482
+ const base = /^[a-z][a-z0-9+.-]*:\/\//i.test(baseUrl)
483
+ ? baseUrl
484
+ : new URL(`file:///${baseUrl.replace(/\\/g, "/")}`).href;
485
+ cssBaseUrl = new URL(r.url, base).href;
486
+ }
487
+ catch (e) { }
488
+ }
489
+ if (cssBaseUrl) {
490
+ const rewrittenCss = cssText.replace(/url\(['"]?([^'")]+)['"]?\)/g, (match, urlParam) => {
491
+ if (urlParam.startsWith("data:") || /^[a-z][a-z0-9+.-]*:/i.test(urlParam)) {
492
+ return match;
493
+ }
494
+ try {
495
+ const newUrl = new URL(urlParam, cssBaseUrl).href;
496
+ return `url("${newUrl}")`;
497
+ }
498
+ catch (e) {
499
+ return match;
500
+ }
501
+ });
502
+ finalUint8 = new TextEncoder().encode(rewrittenCss);
503
+ }
504
+ }
505
+ await loadResourceData(r, finalUint8);
476
506
  }
477
507
  }
478
508
  catch (e) {
@@ -512,6 +542,7 @@ export class SatoruBase {
512
542
  fitPositionX: options.fitPosition?.x ?? 0.5,
513
543
  fitPositionY: options.fitPosition?.y ?? 0.5,
514
544
  backgroundColor: this.parseColor(options.backgroundColor),
545
+ mediaType: options.mediaType === "print" ? 1 : 0,
515
546
  });
516
547
  if (!result) {
517
548
  if (format === "svg")
Binary file