redirect from /<locale>/<surah-id>/ to /<locale>/<surah-slug>/

This change will trigger redirects from - for example, "/en/1/"
to "/en/al-fatihah".
This commit is contained in:
0x1eef 2022-11-04 23:26:03 -03:00 committed by Robert
parent c6d1f7cc11
commit d00b8277a3
5 changed files with 58 additions and 11 deletions

34
Rules
View file

@ -1,16 +1,16 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
require "ryo"
LOCALES = %w[ar en]
SURAH_ID_TO_SLUG = Ryo.from(
JSON.parse(
File.read(File.join(Dir.getwd, "src", "slugs.json"))
)
)
##
# /<locale>/<surahno>/index.html
compile "/surah.html" do
1.upto(114) do |surahno|
LOCALES.each { write(File.join("/", _1, surahno.to_s, "index.html")) }
end
end
# Root path (/)
compile "/index.html" do
write("/index.html")
end
@ -21,6 +21,26 @@ end
LOCALES.each { passthrough "/#{_1}/#{surahno}/surah.json" }
end
##
# /<locale>/<surahno>/index.html
compile "/slug_redirect.html.erb" do
filter :erb
1.upto(114) do |surahno|
LOCALES.each { write(File.join("/", _1, surahno.to_s, "index.html")) }
end
end
##
# /<locale>/surah_slug>/index.html
Ryo.each(SURAH_ID_TO_SLUG) do |surah_id, slug|
LOCALES.each do |locale|
compile "/surah.html", rep: "#{locale}/#{slug}" do
filter :erb, locals: {locale: locale, surah_id: surah_id}
write "/#{locale}/#{slug}/index.html"
end
end
end
##
# /js/pages/surah.js
compile "/js/pages/TheSurahPage.tsx" do

View file

@ -54,6 +54,8 @@ function TheSurahPage ({ locale, surahId }: PageProps) {
);
}
const [locale, surahId] = location.pathname.split('/').filter((e) => e);
const root = ReactDOM.createRoot(document.querySelector('.surah'));
root.render(<TheSurahPage locale={locale} surahId={parseInt(surahId)} />);
const el = document.querySelector('.surah');
const locale = el.getAttribute('data-locale');
const surahId = parseInt(el.getAttribute('data-surah-id'));
const root = ReactDOM.createRoot(el);
root.render(<TheSurahPage locale={locale} surahId={surahId} />);

View file

@ -0,0 +1,24 @@
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<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.href = location.replace(["", locale, slugs[surahId]].join("/"));
})();
</script>
</body>
</html>

1
src/slugs.json Normal file
View file

@ -0,0 +1 @@
{"1":"al-fatihah","2":"al-baqarah","3":"ali-imran","4":"an-nisa","5":"al-maidah","6":"al-anam","7":"al-araf","8":"al-anfal","9":"at-tawbah","10":"yunus","11":"hud","12":"yusuf","13":"ar-rad","14":"ibrahim","15":"al-hijr","16":"an-nahl","17":"al-isra","18":"al-kahf","19":"maryam","20":"taha","21":"al-anbya","22":"al-hajj","23":"al-muminun","24":"an-nur","25":"al-furqan","26":"ash-shuara","27":"an-naml","28":"al-qasas","29":"al-ankabut","30":"ar-rum","31":"luqman","32":"as-sajdah","33":"al-ahzab","34":"saba","35":"fatir","36":"ya-sin","37":"as-saffat","38":"sad","39":"az-zumar","40":"ghafir","41":"fussilat","42":"ash-shuraa","43":"az-zukhruf","44":"ad-dukhan","45":"al-jathiyah","46":"al-ahqaf","47":"muhammad","48":"al-fath","49":"al-hujurat","50":"qaf","51":"adh-dhariyat","52":"at-tur","53":"an-najm","54":"al-qamar","55":"ar-rahman","56":"al-waqiah","57":"al-hadid","58":"al-mujadila","59":"al-hashr","60":"al-mumtahanah","61":"as-saf","62":"al-jumuah","63":"al-munafiqun","64":"at-taghabun","65":"at-talaq","66":"at-tahrim","67":"al-mulk","68":"al-qalam","69":"al-haqqah","70":"al-maarij","71":"nuh","72":"al-jinn","73":"al-muzzammil","74":"al-muddaththir","75":"al-qiyamah","76":"al-insan","77":"al-mursalat","78":"an-naba","79":"an-naziat","80":"abasa","81":"at-takwir","82":"al-infitar","83":"al-mutaffifin","84":"al-inshiqaq","85":"al-buruj","86":"at-tariq","87":"al-ala","88":"al-ghashiyah","89":"al-fajr","90":"al-balad","91":"ash-shams","92":"al-layl","93":"ad-duhaa","94":"ash-sharh","95":"at-tin","96":"al-alaq","97":"al-qadr","98":"al-bayyinah","99":"az-zalzalah","100":"al-adiyat","101":"al-qariah","102":"at-takathur","103":"al-asr","104":"al-humazah","105":"al-fil","106":"quraysh","107":"al-maun","108":"al-kawthar","109":"al-kafirun","110":"an-nasr","111":"al-masad","112":"al-ikhlas","113":"al-falaq","114":"an-nas"}

View file

@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div class="surah"></div>
<div class="surah" data-locale="<%= locale %>" data-surah-id="<%= surah_id %>"></div>
<script src="/js/pages/surah.js"></script>
</body>
</html>