Skip to content
Snippets Groups Projects
redirect.js 2.23 KiB
Newer Older
resynth1943's avatar
resynth1943 committed
const fs = require('fs');
const path = require('path');

function redirect (oldPath, newPath) {
    const oldRelativePath = path.join(__dirname, '../', './_site/', oldPath);
    const newRelativePath = path.join(__dirname, '../', './_site/', newPath);

    const newSitePath = path.join('https://resynth1943.net', newPath);
    fs.mkdirSync(path.dirname(oldRelativePath), { recursive: true });

    console.log(`Redirecting ${oldPath}${newPath}`);
    fs.writeFileSync(oldRelativePath, `\
<!doctype html>

<html>
    <head>
        <meta charset="utf-8" />
        <!-- Pleace this snippet right after opening the head tag to make it work properly -->
        <!-- This code is licensed under GNU GPL v3 -->
        <!-- You are allowed to freely copy, distribute and use this code, but removing author credit is strictly prohibited -->
        <!-- Generated by http://insider.zone/tools/client-side-url-redirect-generator/ -->
        <!-- Addendum: this is actually generated via a script, not their web tool. -->
        <!-- Nevertheless, I have stolen their code, so let's leave the license there. -->
            
        <!-- REDIRECTING STARTS -->
        <link rel="canonical" href="${newSitePath}" />
        <noscript>
            <meta http-equiv="refresh" content="0;URL=${newSitePath}">
        </noscript>
        <!--[if lt IE 9]><script type="text/javascript">var IE_fix=true;</script><![endif]-->
        <script type="text/javascript">
            var url = "${newSitePath}";
            if(typeof IE_fix != "undefined") // IE8 and lower fix to pass the http referer
            {
                document.write("redirecting..."); // Don't remove this line or appendChild() will fail because it is called before document.onload to make the redirect as fast as possible. Nobody will see this text, it is only a tech fix.
                var referLink = document.createElement("a");
                referLink.href = url;
                document.body.appendChild(referLink);
                referLink.click();
            }
            else { window.location.replace(url); } // All other browsers
        </script>
        <!-- Credit goes to http://insider.zone/ -->
        <!-- REDIRECTING ENDS -->
    </head>
</html>
    `);
}

module.exports = redirect;