Skip to content

Commit

Permalink
feat(github): ✨ add view on github redirection
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothy-Liuxf committed May 16, 2024
1 parent 2dfed9a commit 100398e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
4 changes: 2 additions & 2 deletions 404.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
if (lang_index !== -1) {
const lang = paths[lang_index];
if (lang === 'zh-CN') {
window.location.href = paths.slice(0, 4).join('/') + '/unavailable';
window.location.href = paths.slice(0, 4).join('/') + '/unavailable.html';
} else {
const new_paths = paths.slice(0, lang_index).concat(
lang === 'en-US' || lang === 'zh-TW' ? 'zh-CN' : 'en-US'
).concat(paths.slice(lang_index + 1));
window.location.href = new_paths.join('/').replace(/\.md$/, '.html');
}
} else {
window.location.href = paths.slice(0, 4).join('/') + '/unavailable';
window.location.href = paths.slice(0, 4).join('/') + '/unavailable.html';
}
</script>
</head>
Expand Down
21 changes: 20 additions & 1 deletion _layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,15 @@
}
</style>
<script type="text/javascript">
const base_url = "{{ site.github.url }}";
const repo_url = "{{ site.github.repository_url }}";
const repo_branch = "{{ site.github.source.branch }}";
const repo_path = "{{ site.github.source.path }}";
const current_url = window.location.href;
const paths = current_url.split('/');
const pattern = /^(zh-CN|zh-TW|en-US|fr-FR)$/;
const lang_index = paths.findIndex(path => pattern.test(path));
const is_main_page = paths.length === 5 && current_url.endsWith('/');

document.documentElement.lang = lang_index !== -1 ? paths[lang_index] : 'en-US';

Expand All @@ -83,6 +88,20 @@
window.location.href = paths.slice(0, 4).join('/') + '/'
}
};

const viewOnGitHub = () => {
if (is_main_page) {
window.location.href = base_url + '/';
} else {
let target = `${repo_url}/blob/${repo_branch}/${repo_path.replace(/\/$/, '')}${current_url.slice(base_url.length)}`;
if (target.endsWith('/')) {
target += 'README.md';
} else if (target.endsWith('.html')) {
target = target.replace(/\.html$/, '.md');
}
window.location.href = target;
}
};
</script>
{% include head-custom.html %}

Expand Down Expand Up @@ -148,7 +167,7 @@ <h2>{{ site.description | default: site.github.project_tagline }}</h2>
back.innerText = 'Back';
break;
}
} else if (paths.length == 5 && current_url.endsWith('/')) {
} else if (is_main_page) {
back.style.display = 'none';
} else {
back.innerText = 'Home';
Expand Down

0 comments on commit 100398e

Please sign in to comment.