src/: add src/js/pages/redirect-to-surah-slug.ts

This commit is contained in:
0x1eef 2022-11-06 06:51:28 -03:00 committed by Robert
parent e31fac44bb
commit 296aa9a165
3 changed files with 17 additions and 13 deletions

7
Rules
View file

@ -59,6 +59,13 @@ compile "/js/pages/redirect-to-random-surah.ts" do
write "/js/pages/redirect-to-random-surah.js"
end
##
# /js/pages/redirect-to-surah-slug.js
compile "/js/pages/redirect-to-surah-slug.ts" do
filter :webpack, exe: "./node_modules/webpack/bin/webpack.js"
write "/js/pages/redirect-to-surah-slug.js"
end
##
# CSS
compile("/css/_*.scss") { write(nil) }

View file

@ -7,18 +7,6 @@
<script class="surah-id-to-slug" type="application/json">
<%= File.read(File.join(Dir.getwd, "src", "slugs.json")) %>
</script>
<script type="text/javascript">
(function() {
var parts = location.pathname
.split("/")
.filter(function(s) { return s.length })
.slice(-2);
var el = document.querySelector(".surah-id-to-slug");
var locale = parts[0];
var surahId = parts[1];
var slugs = JSON.parse(el.innerText);
location.replace(["", locale, slugs[surahId]].join("/"));
})();
</script>
<script src="/js/pages/redirect-to-surah-slug.js"></script>
</body>
</html>

View file

@ -0,0 +1,9 @@
(function() {
const [locale, surahId] = location.pathname
.split("/")
.filter(function(s) { return s.length })
.slice(-2);
const el: HTMLElement = document.querySelector(".surah-id-to-slug");
const slugs = JSON.parse(el.innerText);
location.replace(["", locale, slugs[surahId]].join("/"));
})();