summaryrefslogtreecommitdiffstats
path: root/layouts/partials/scripts-end.html
blob: 89c242a67377c82ad47e9cb1788b763a309bbe63 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
{{ if eq .Section "search" }}
<script defer src="https://cdn.jsdelivr.net/npm/art-template@4.13.2/lib/template-web.min.js"></script>
<script defer src="https://cdn.jsdelivr.net/npm/fuse.js@6.5.3/dist/fuse.min.js"></script>
{{ else if eq .Section "archives" }}
{{ else }}
<script defer src="https://cdn.jsdelivr.net/npm/medium-zoom@1.0.6/dist/medium-zoom.min.js"></script>
<script defer src="https://cdn.jsdelivr.net/npm/lazysizes@5.3.2/lazysizes.min.js"></script>
<script defer src="https://cdn.jsdelivr.net/npm/prismjs@1.27.0/components/prism-core.min.js"></script>
<script defer src="https://cdn.jsdelivr.net/npm/prismjs@1.27.0/plugins/autoloader/prism-autoloader.min.js"></script>
{{ end }}
{{ $options := (dict "targetPath" "assets/js/fuji.min.js" "minify" true "target" "es2015" "format" "iife") }}
{{ $built := resources.Get "js/fuji.js" | js.Build $options | resources.Fingerprint "sha512" }}
<script defer src="{{ $built.RelPermalink }}" integrity="{{ $built.Data.Integrity }}"></script>
{{ if or .Params.math .Site.Params.math }}
{{ partial "math.html" . }}
{{ end }}
{{ with .Params.aplayer }}
<script src="https://cdn.jsdelivr.net/npm/aplayer@1.10.1/dist/APlayer.min.js"></script>
<script>
  (function () {
    function parseBasePath(basePath, rawURL) {
      if (!/^\/[^/]/.test(rawURL)) {
        return rawURL;
      }
      return rawURL.replace(/^\//, basePath + '/').replace(/\/\//g, '/');
    }
    // in-post aplayer
    var pContainers = document.querySelectorAll('.content article .post-aplayer');
    if (pContainers && pContainers.length > 0) {
      pContainers.forEach(function (container) {
        var attrs = ['urls', 'names', 'artists', 'covers', 'base'];
        attrs = attrs.map(function (attr) {
          var arr = container.getAttribute('data-' + attr).replace(/( |%20)/ig, '').split(',');
          if (arr.length > 0){
            return arr.map(function (val) {
              return String(val).trim();
            });
          } else {
            return attr;
          }
        });
        var basePath = new URL(attrs[4][0] || '/', 'https://example.org').pathname;
        var audio = [];
        for (var i = 0; i < attrs[0].length; i++) {
          var a = {
            name: attrs[1][i],
            url: parseBasePath(basePath, attrs[0][i]),
          };
          (typeof attrs[2] !== 'string') && (a.artist = attrs[2][i]);
          (typeof attrs[3] !== 'string') && (a.cover = parseBasePath(basePath, attrs[3][i]));
          audio.push(a);
        }
        new APlayer({
          container: container,
          theme: '#8f82bc',
          volume: 0.1,
          listFolded: true,
          audio: audio
        });
      });
    }
    // aplayer toc anchors fix
    // see https://github.com/MoePlayer/APlayer/issues/242#issuecomment-602471423
    document.addEventListener('DOMContentLoaded', function () {
      var issueTocEle = document.querySelector('.sidebar-toc');
      if (issueTocEle) {
        var newTocEle = issueTocEle.cloneNode(true);
        issueTocEle.parentNode.replaceChild(newTocEle, issueTocEle);
      }
    });
  })();
</script>
{{ end }}