hexo-theme-linen 1.0.50 → 1.0.52

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.
@@ -643,8 +643,328 @@ function initMaskScrollLock() {
643
643
  );
644
644
  }
645
645
 
646
+ function initTravelRoutes() {
647
+ var contentEl = document.querySelector("#post.travel-post #page-content");
648
+ if (!contentEl) return;
649
+
650
+ var ROUTE_HEADING_RE = /^(?:路线|路线规划|Route|Route Planning)$/i;
651
+ var PALETTE = [
652
+ "#c97a63",
653
+ "#a8452e",
654
+ "#8a9a86",
655
+ "#c4b7a6",
656
+ "#b87d56",
657
+ "#6d8b74",
658
+ "#d49a79",
659
+ "#9c8f84",
660
+ ];
661
+ var isEnglish = /^en/i.test(document.documentElement.lang || "");
662
+
663
+ function getHeadingText(heading) {
664
+ var clone = heading.cloneNode(true);
665
+ clone
666
+ .querySelectorAll(".headerlink, .header-anchor")
667
+ .forEach(function (el) {
668
+ el.remove();
669
+ });
670
+ return (clone.textContent || "")
671
+ .replace(/^[#¶\s]+|[#¶\s]+$/g, "")
672
+ .trim();
673
+ }
674
+
675
+ function parseZhNum(str) {
676
+ var map = {
677
+ 半: 0.5,
678
+ 一: 1,
679
+ 二: 2,
680
+ 两: 2,
681
+ 三: 3,
682
+ 四: 4,
683
+ 五: 5,
684
+ 六: 6,
685
+ 七: 7,
686
+ 八: 8,
687
+ 九: 9,
688
+ 十: 10,
689
+ };
690
+ if (Object.prototype.hasOwnProperty.call(map, str)) return map[str];
691
+ if (/^十[一二三四五六七八九]$/.test(str)) return 10 + map[str[1]];
692
+ return null;
693
+ }
694
+
695
+ function parseDuration(text) {
696
+ var trimmed = String(text || "").trim();
697
+ if (!trimmed) return null;
698
+
699
+ var compound = trimmed.match(
700
+ /^(\d+(?:\.\d+)?)\s*(日|天)\s*\d+(?:\.\d+)?\s*(?:晚|夜)$/
701
+ );
702
+ if (compound) {
703
+ var val = Number(compound[1]);
704
+ if (isFinite(val) && val > 0) {
705
+ return {
706
+ label: trimmed,
707
+ weight: val,
708
+ unitKind: "day",
709
+ unitText: compound[2],
710
+ };
711
+ }
712
+ }
713
+
714
+ if (/^(半日|半天|half\s+day)$/i.test(trimmed)) {
715
+ var isEn = /day/i.test(trimmed);
716
+ return {
717
+ label: trimmed,
718
+ weight: 0.5,
719
+ unitKind: "day",
720
+ unitText: isEn ? "days" : trimmed.slice(-1) === "日" ? "日" : "天",
721
+ };
722
+ }
723
+
724
+ var zhNum = trimmed.match(
725
+ /^(十?[一二两三四五六七八九]|十)\s*(日|天|晚|夜)(半)?$/
726
+ );
727
+ if (zhNum) {
728
+ var base = parseZhNum(zhNum[1]);
729
+ if (base) {
730
+ var unitChar = zhNum[2];
731
+ return {
732
+ label: trimmed,
733
+ weight: base + (zhNum[3] ? 0.5 : 0),
734
+ unitKind: unitChar === "晚" || unitChar === "夜" ? "night" : "day",
735
+ unitText: unitChar,
736
+ };
737
+ }
738
+ }
739
+
740
+ var num = trimmed.match(
741
+ /^(\d+(?:\.\d+)?)\s*(日|天|晚|夜|小时|days?|nights?|hours?|hrs?|d|h)$/i
742
+ );
743
+ if (num) {
744
+ var n = Number(num[1]);
745
+ if (isFinite(n) && n > 0) {
746
+ var lower = num[2].toLowerCase();
747
+ var unitKind = "day";
748
+ var unitText = num[2];
749
+ if (lower === "晚" || lower === "夜" || lower.indexOf("night") === 0) {
750
+ unitKind = "night";
751
+ unitText = lower.indexOf("night") === 0 ? "nights" : num[2];
752
+ } else if (
753
+ lower === "小时" ||
754
+ lower.indexOf("hour") === 0 ||
755
+ lower.indexOf("hr") === 0 ||
756
+ lower === "h"
757
+ ) {
758
+ unitKind = "hour";
759
+ unitText = lower === "小时" ? "小时" : "hours";
760
+ } else if (lower.indexOf("day") === 0 || lower === "d") {
761
+ unitKind = "day";
762
+ unitText = "days";
763
+ }
764
+ return { label: trimmed, weight: n, unitKind: unitKind, unitText: unitText };
765
+ }
766
+ }
767
+
768
+ return null;
769
+ }
770
+
771
+ function parseItem(rawHtml) {
772
+ var content = String(rawHtml || "").trim();
773
+ if (/^<p>[\s\S]*<\/p>$/i.test(content) && !/<\/p>\s*<p>/i.test(content)) {
774
+ content = content.replace(/^<p>|<\/p>$/gi, "").trim();
775
+ }
776
+
777
+ var note = "";
778
+ var duration = null;
779
+
780
+ var paren = content.match(
781
+ /^(.*?)[\s]*(?:(([^()]+))|\(([^()]+)\))\s*$/
782
+ );
783
+ if (paren && paren[1].trim()) {
784
+ var inner = (paren[2] || paren[3] || "").trim();
785
+ var pd = parseDuration(inner);
786
+ if (pd) {
787
+ duration = pd;
788
+ } else {
789
+ note = inner;
790
+ }
791
+ content = paren[1].trim();
792
+ }
793
+
794
+ if (!duration) {
795
+ var tail = content.match(
796
+ /^(.*?)(?:[\s·•\-—::,,/]+)(\d+(?:\.\d+)?\s*(?:日|天)\s*\d+(?:\.\d+)?\s*(?:晚|夜)|\d+(?:\.\d+)?\s*(?:日|天|晚|夜|小时|days?|nights?|hours?|hrs?|d|h)|(?:半日|半天|half\s+day)|(?:十?[一二两三四五六七八九]|十)\s*(?:日|天|晚|夜)(?:半)?)\s*$/i
797
+ );
798
+ if (tail && tail[1].trim()) {
799
+ var td = parseDuration(tail[2]);
800
+ if (td) {
801
+ duration = td;
802
+ content = tail[1].trim();
803
+ }
804
+ }
805
+ }
806
+
807
+ if (!duration) {
808
+ var compact = content.match(
809
+ /^(.*?)(\d+(?:\.\d+)?\s*(?:日|天)\s*\d+(?:\.\d+)?\s*(?:晚|夜)|\d+(?:\.\d+)?\s*(?:日|天|晚|夜|小时)|半日|半天)$/
810
+ );
811
+ if (compact && compact[1].trim()) {
812
+ var cd = parseDuration(compact[2]);
813
+ if (cd) {
814
+ duration = cd;
815
+ content = compact[1].trim();
816
+ }
817
+ }
818
+ }
819
+
820
+ var place = content.replace(/[\s·•\-—::,,/]+$/, "").trim();
821
+ if (!place) {
822
+ return { place: rawHtml.trim(), duration: null, note: "" };
823
+ }
824
+ return { place: place, duration: duration, note: note };
825
+ }
826
+
827
+ function getCols(count) {
828
+ if (count <= 2) return 2;
829
+ if (count === 3 || count === 5 || count === 6 || count === 9) return 3;
830
+ return 4;
831
+ }
832
+
833
+ contentEl.querySelectorAll("h2, h3").forEach(function (heading) {
834
+ if (!ROUTE_HEADING_RE.test(getHeadingText(heading))) return;
835
+
836
+ var next = heading.nextElementSibling;
837
+ while (next && next.id === "gitalk-initiator") {
838
+ next = next.nextElementSibling;
839
+ }
840
+ if (!next || (next.tagName !== "UL" && next.tagName !== "OL")) return;
841
+ if (next.classList.contains("travel-route-grid")) return;
842
+ if (next.querySelector("ul, ol, img, iframe, table, pre, blockquote, figure")) {
843
+ return;
844
+ }
845
+
846
+ var lis = Array.prototype.slice.call(next.children).filter(function (el) {
847
+ return el.tagName === "LI";
848
+ });
849
+ if (lis.length < 2) return;
850
+
851
+ var stops = lis.map(function (li) {
852
+ return parseItem(li.innerHTML);
853
+ });
854
+ var withDuration = stops.filter(function (s) {
855
+ return !!s.duration;
856
+ });
857
+ var showBar = withDuration.length >= 2;
858
+ var cols = getCols(stops.length);
859
+
860
+ var block = document.createElement("div");
861
+ block.className = "travel-route-block" + (showBar ? " has-bar" : "");
862
+ block.style.setProperty("--route-cols", String(cols));
863
+
864
+ if (showBar) {
865
+ var unitKinds = new Set(
866
+ withDuration.map(function (s) {
867
+ return s.duration.unitKind;
868
+ })
869
+ );
870
+
871
+ if (
872
+ withDuration.length === stops.length &&
873
+ unitKinds.size === 1 &&
874
+ (unitKinds.has("day") || unitKinds.has("night"))
875
+ ) {
876
+ var total = Number(
877
+ withDuration
878
+ .reduce(function (sum, s) {
879
+ return sum + s.duration.weight;
880
+ }, 0)
881
+ .toFixed(2)
882
+ );
883
+ var firstUnit = withDuration[0].duration.unitText;
884
+ var useEn = isEnglish || /^[a-z]+$/i.test(firstUnit);
885
+ var summary = document.createElement("div");
886
+ summary.className = "travel-route-summary";
887
+ summary.textContent = useEn
888
+ ? stops.length + " STOPS · " + total + " " + firstUnit.toUpperCase()
889
+ : stops.length + " 站 · 共 " + total + " " + firstUnit;
890
+ block.appendChild(summary);
891
+ }
892
+
893
+ var bar = document.createElement("div");
894
+ bar.className = "travel-route-bar";
895
+ bar.setAttribute("aria-hidden", "true");
896
+ stops.forEach(function (stop, idx) {
897
+ if (!stop.duration) return;
898
+ var seg = document.createElement("span");
899
+ var color = PALETTE[idx % PALETTE.length];
900
+ var flexWeight = stop.duration.weight;
901
+ if (unitKinds.size > 1 && stop.duration.unitKind === "hour") {
902
+ flexWeight = Math.max(stop.duration.weight / 24, 0.25);
903
+ }
904
+ seg.className = "travel-route-seg";
905
+ seg.style.flex = String(flexWeight);
906
+ seg.style.setProperty("--stop-color", color);
907
+ var plainPlace = stop.place.replace(/<[^>]*>/g, "").trim();
908
+ seg.title = plainPlace + " · " + stop.duration.label;
909
+ bar.appendChild(seg);
910
+ });
911
+ block.appendChild(bar);
912
+ }
913
+
914
+ var grid = document.createElement("ul");
915
+ grid.className = "travel-route-grid";
916
+ stops.forEach(function (stop, idx) {
917
+ var color = PALETTE[idx % PALETTE.length];
918
+ var li = document.createElement("li");
919
+ li.className = "travel-route-item";
920
+ li.style.setProperty("--stop-color", color);
921
+
922
+ var top = document.createElement("div");
923
+ top.className = "travel-route-item-top";
924
+ var idxSpan = document.createElement("span");
925
+ idxSpan.className = "travel-route-index";
926
+ idxSpan.textContent = String(idx + 1).padStart(2, "0");
927
+ var dot = document.createElement("span");
928
+ dot.className = "travel-route-dot";
929
+ dot.setAttribute("aria-hidden", "true");
930
+ top.appendChild(idxSpan);
931
+ top.appendChild(dot);
932
+
933
+ var placeDiv = document.createElement("div");
934
+ placeDiv.className = "travel-route-place";
935
+ placeDiv.innerHTML = stop.place;
936
+
937
+ li.appendChild(top);
938
+ li.appendChild(placeDiv);
939
+
940
+ if (stop.duration) {
941
+ var daysSpan = document.createElement("span");
942
+ daysSpan.className = "travel-route-days";
943
+ daysSpan.textContent = stop.duration.label;
944
+ li.appendChild(daysSpan);
945
+ }
946
+
947
+ if (stop.note) {
948
+ var noteSpan = document.createElement("span");
949
+ noteSpan.className = "travel-route-note";
950
+ noteSpan.textContent = stop.note;
951
+ li.appendChild(noteSpan);
952
+ }
953
+
954
+ grid.appendChild(li);
955
+ });
956
+
957
+ block.appendChild(grid);
958
+ next.parentNode.replaceChild(block, next);
959
+ });
960
+ }
961
+
646
962
  if (document.readyState === "loading") {
647
- document.addEventListener("DOMContentLoaded", initMaskScrollLock);
963
+ document.addEventListener("DOMContentLoaded", function () {
964
+ initMaskScrollLock();
965
+ initTravelRoutes();
966
+ });
648
967
  } else {
649
968
  initMaskScrollLock();
969
+ initTravelRoutes();
650
970
  }