@c-time/frelio-data-json-generator 1.4.5 → 1.4.7

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/dist/index.d.ts CHANGED
@@ -266,6 +266,12 @@ type RecipeExecutionContext = {
266
266
  warnings: GenerationWarning[];
267
267
  /** ログレベル */
268
268
  logLevel: 'debug' | 'info' | 'quiet';
269
+ /**
270
+ * レシピ既定の出力タイムゾーン(issue #92)。
271
+ * date 整形時、フィールド単位の timezone が無い場合のフォールバック。
272
+ * オフセット表記(例 "+09:00")。未設定は UTC。
273
+ */
274
+ defaultTimezone?: string;
269
275
  };
270
276
  /**
271
277
  * ロガーインターフェース
package/dist/index.js CHANGED
@@ -423,7 +423,7 @@ function formatValue(value, type, options = {}) {
423
423
  case "string":
424
424
  return String(value);
425
425
  case "date":
426
- return formatDate(value, options.format);
426
+ return formatDate(value, options.format, options.timezone);
427
427
  case "number":
428
428
  return formatNumber(value, options.format);
429
429
  case "escape":
@@ -461,15 +461,52 @@ function formatValue(value, type, options = {}) {
461
461
  return value;
462
462
  }
463
463
  }
464
- function formatDate(value, format) {
464
+ function parseOffsetMinutes(timezone) {
465
+ if (timezone === "Z") return 0;
466
+ const m = /^([+-])(\d{2}):(\d{2})$/.exec(timezone);
467
+ if (!m) return null;
468
+ const sign = m[1] === "-" ? -1 : 1;
469
+ return sign * (parseInt(m[2], 10) * 60 + parseInt(m[3], 10));
470
+ }
471
+ function offsetLabel(offsetMin) {
472
+ const sign = offsetMin < 0 ? "-" : "+";
473
+ const abs = Math.abs(offsetMin);
474
+ const hh = String(Math.floor(abs / 60)).padStart(2, "0");
475
+ const mm = String(abs % 60).padStart(2, "0");
476
+ return `${sign}${hh}:${mm}`;
477
+ }
478
+ function formatDate(value, format, timezone) {
465
479
  const date = value instanceof Date ? value : new Date(String(value));
466
480
  if (isNaN(date.getTime())) {
467
481
  return String(value);
468
482
  }
483
+ const offsetMin = timezone ? parseOffsetMinutes(timezone) : null;
469
484
  if (!format) {
470
- return date.toISOString();
471
- }
472
- return format.replace(/YYYY/g, String(date.getFullYear())).replace(/MM/g, String(date.getMonth() + 1).padStart(2, "0")).replace(/DD/g, String(date.getDate()).padStart(2, "0")).replace(/HH/g, String(date.getHours()).padStart(2, "0")).replace(/mm/g, String(date.getMinutes()).padStart(2, "0")).replace(/ss/g, String(date.getSeconds()).padStart(2, "0"));
485
+ if (offsetMin === null) {
486
+ return date.toISOString();
487
+ }
488
+ const shifted = new Date(date.getTime() + offsetMin * 6e4);
489
+ const base = shifted.toISOString().replace(/\.\d{3}Z$/, "");
490
+ return `${base}${offsetLabel(offsetMin)}`;
491
+ }
492
+ let year, month, day, hour, minute, second;
493
+ if (offsetMin === null) {
494
+ year = date.getFullYear();
495
+ month = date.getMonth() + 1;
496
+ day = date.getDate();
497
+ hour = date.getHours();
498
+ minute = date.getMinutes();
499
+ second = date.getSeconds();
500
+ } else {
501
+ const shifted = new Date(date.getTime() + offsetMin * 6e4);
502
+ year = shifted.getUTCFullYear();
503
+ month = shifted.getUTCMonth() + 1;
504
+ day = shifted.getUTCDate();
505
+ hour = shifted.getUTCHours();
506
+ minute = shifted.getUTCMinutes();
507
+ second = shifted.getUTCSeconds();
508
+ }
509
+ return format.replace(/YYYY/g, String(year)).replace(/MM/g, String(month).padStart(2, "0")).replace(/DD/g, String(day).padStart(2, "0")).replace(/HH/g, String(hour).padStart(2, "0")).replace(/mm/g, String(minute).padStart(2, "0")).replace(/ss/g, String(second).padStart(2, "0"));
473
510
  }
474
511
  function formatNumber(value, format) {
475
512
  const num = typeof value === "number" ? value : parseFloat(String(value));
@@ -595,21 +632,21 @@ function compareValues(a, b) {
595
632
  if (typeof a === "number" && typeof b === "number") {
596
633
  return a - b;
597
634
  }
598
- if (typeof a === "string" && typeof b === "string") {
599
- return a.localeCompare(b);
600
- }
601
635
  if (a instanceof Date && b instanceof Date) {
602
636
  return a.getTime() - b.getTime();
603
637
  }
604
638
  if (typeof a === "string" && typeof b === "string") {
605
- const dateA = Date.parse(a);
606
- const dateB = Date.parse(b);
607
- if (!isNaN(dateA) && !isNaN(dateB)) {
608
- return dateA - dateB;
639
+ if (isIsoDateOrDateTime(a) && isIsoDateOrDateTime(b)) {
640
+ return Date.parse(a) - Date.parse(b);
609
641
  }
642
+ return a.localeCompare(b);
610
643
  }
611
644
  return 0;
612
645
  }
646
+ var ISO_DATE_OR_DATETIME_REGEX = /^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}:\d{2}(\.\d+)?([+-]\d{2}:\d{2}|Z))?$/;
647
+ function isIsoDateOrDateTime(value) {
648
+ return ISO_DATE_OR_DATETIME_REGEX.test(value);
649
+ }
613
650
 
614
651
  // src/transform/relation-expander.ts
615
652
  async function expandRelationList(field, content, context) {
@@ -674,16 +711,17 @@ async function applyCustomFields(data, customFields, content, context) {
674
711
  const expanded = await expandRelationList(field, content, context);
675
712
  result[field.field] = expanded;
676
713
  } else {
677
- const transformed = applyTransformField(field, content, result);
714
+ const transformed = applyTransformField(field, content, result, context.defaultTimezone);
678
715
  result[field.field] = transformed;
679
716
  }
680
717
  }
681
718
  return result;
682
719
  }
683
- function applyTransformField(field, content, currentData) {
720
+ function applyTransformField(field, content, currentData, defaultTimezone) {
684
721
  const sourceValue = resolveSourceExpression(field.source, content, currentData);
685
722
  return formatValue(sourceValue, field.type, {
686
723
  format: field.format,
724
+ timezone: field.timezone ?? defaultTimezone,
687
725
  length: field.length,
688
726
  suffix: field.suffix,
689
727
  separator: field.separator,
@@ -728,6 +766,49 @@ function getValueByKey2(key, content, currentData) {
728
766
  return value;
729
767
  }
730
768
 
769
+ // src/recipe/navigation-resolver.ts
770
+ function resolveNavigation(navigation, current, allContents, detailOutputPath) {
771
+ const filtered = applyFilters(allContents, navigation.filters);
772
+ const sorted = applySort(filtered, navigation.sort);
773
+ const idx = sorted.findIndex((c) => c.contentId === current.contentId);
774
+ const toItem = (c) => c ? buildNavigationItem(c, navigation.fields, detailOutputPath) : null;
775
+ return {
776
+ // 現在コンテンツが絞り込み外(idx === -1)の場合、prev/next は null
777
+ prev: idx > 0 ? toItem(sorted[idx - 1]) : null,
778
+ next: idx >= 0 && idx < sorted.length - 1 ? toItem(sorted[idx + 1]) : null,
779
+ first: toItem(sorted[0]),
780
+ last: toItem(sorted[sorted.length - 1])
781
+ };
782
+ }
783
+ function buildNavigationItem(content, fields, detailOutputPath) {
784
+ const item = {
785
+ contentId: content.contentId,
786
+ slug: content.slug,
787
+ title: content.title,
788
+ href: computeHref(detailOutputPath, content)
789
+ };
790
+ if (fields === null || fields === void 0) {
791
+ for (const [key, value] of Object.entries(content.data)) {
792
+ if (!(key in item)) {
793
+ item[key] = value;
794
+ }
795
+ }
796
+ } else {
797
+ for (const key of fields) {
798
+ if (key in content.data) {
799
+ item[key] = content.data[key];
800
+ }
801
+ }
802
+ }
803
+ return item;
804
+ }
805
+ function computeHref(detailOutputPath, content) {
806
+ const resolved = sanitizePath(resolveOutputPath(detailOutputPath, content));
807
+ let href = "/" + toHtmlOutputPath(resolved);
808
+ href = href.replace(/\/index\.html$/, "/");
809
+ return href;
810
+ }
811
+
731
812
  // src/recipe/detail-processor.ts
732
813
  async function processDetailRecipes(contentTypeId, recipes, targets, context) {
733
814
  const outputs = [];
@@ -746,7 +827,7 @@ async function processDetailRecipes(contentTypeId, recipes, targets, context) {
746
827
  continue;
747
828
  }
748
829
  try {
749
- const output = await processDetailContent(recipe, content, context);
830
+ const output = await processDetailContent(recipe, content, contents, context);
750
831
  if (output) {
751
832
  outputs.push(output);
752
833
  }
@@ -762,7 +843,7 @@ async function processDetailRecipes(contentTypeId, recipes, targets, context) {
762
843
  }
763
844
  return outputs;
764
845
  }
765
- async function processDetailContent(recipe, content, context) {
846
+ async function processDetailContent(recipe, content, allContents, context) {
766
847
  const outputPath = sanitizePath(resolveOutputPath(recipe.outputPath, content));
767
848
  const pathError = validateOutputPath(outputPath);
768
849
  if (pathError) {
@@ -787,6 +868,13 @@ async function processDetailContent(recipe, content, context) {
787
868
  if (recipe.customFields) {
788
869
  data = await applyCustomFields(data, recipe.customFields, content, context);
789
870
  }
871
+ if (recipe.navigation) {
872
+ const nav = resolveNavigation(recipe.navigation, content, allContents, recipe.outputPath);
873
+ data.prev = nav.prev;
874
+ data.next = nav.next;
875
+ data.first = nav.first;
876
+ data.last = nav.last;
877
+ }
790
878
  const dataJson = {
791
879
  type: "update",
792
880
  template: recipe.templatePath,
@@ -1168,7 +1256,8 @@ ${formatZodErrors(recipeValidation.errors)}`);
1168
1256
  outputRootPath,
1169
1257
  errors,
1170
1258
  warnings,
1171
- logLevel
1259
+ logLevel,
1260
+ defaultTimezone: recipe.outputTimezone
1172
1261
  };
1173
1262
  const outputs = [];
1174
1263
  for (const [contentTypeId, contentTypeRecipe] of Object.entries(recipe.contentTypes)) {