diff options
Diffstat (limited to 'layouts')
-rw-r--r-- | layouts/partials/scripts-end.html | 87 | ||||
-rw-r--r-- | layouts/shortcodes/aplayer.html | 1 |
2 files changed, 49 insertions, 39 deletions
diff --git a/layouts/partials/scripts-end.html b/layouts/partials/scripts-end.html index 11329ef..ec4cbfe 100644 --- a/layouts/partials/scripts-end.html +++ b/layouts/partials/scripts-end.html @@ -17,48 +17,57 @@ {{ with .Params.aplayer }} <script src="https://cdn.jsdelivr.net/npm/aplayer@1.10.1/dist/APlayer.min.js"></script> <script> - // 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']; - 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; + (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 + }); }); - var audio = []; - for (var i = 0; i < attrs[0].length; i++) { - var a = { - name: attrs[1][i], - url: attrs[0][i], - }; - (typeof attrs[2] !== 'string') && (a.artist = attrs[2][i]); - (typeof attrs[3] !== 'string') && (a.cover = attrs[3][i]); - audio.push(a); + } + // 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); } - 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 }} diff --git a/layouts/shortcodes/aplayer.html b/layouts/shortcodes/aplayer.html index 37ed544..e02adce 100644 --- a/layouts/shortcodes/aplayer.html +++ b/layouts/shortcodes/aplayer.html @@ -1,6 +1,7 @@ <div class="post-player-wrapper"> <div class="post-aplayer" + data-base="{{- .Site.BaseURL -}}" data-urls="{{- .Get "urls" -}}" data-names="{{- .Get "names" -}}" {{ with .Get "artists" -}}data-artists="{{- . -}}"{{- end }} |