From f501cbc737b057996a1baff35d500557512b8154 Mon Sep 17 00:00:00 2001 From: sunface Date: Tue, 29 Mar 2022 09:17:16 +0800 Subject: [PATCH] Add language switch icon in top-right corner --- ChangeLog.md | 8 ++++++++ en/assets/{custom1.js => custom3.js} | 11 +++++++++++ en/assets/lang.js | 26 +++++++++++++++++++++++++ en/book.toml | 2 +- en/deploy.sh | 2 -- en/src/lifetime/advance.md | 2 ++ zh-CN/assets/{custom1.js => custom3.js} | 11 +++++++++++ zh-CN/assets/lang.js | 26 +++++++++++++++++++++++++ zh-CN/book.toml | 2 +- zh-CN/src/lifetime/advance.md | 2 ++ 10 files changed, 88 insertions(+), 4 deletions(-) rename en/assets/{custom1.js => custom3.js} (90%) create mode 100644 en/assets/lang.js rename zh-CN/assets/{custom1.js => custom3.js} (90%) create mode 100644 zh-CN/assets/lang.js diff --git a/ChangeLog.md b/ChangeLog.md index 9473f7b..e49816a 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,5 +1,13 @@ # ChangeLog +### 2022-03-29 + +- Add language switch icon in top-right corner + +### 2022-03-28 + +- Add [Basic lifetime](https://practice.rs/lifetime/basic.html) + ### 2022-03-25 - Replace gitalk with giscus comments system which is based on Github Discussions diff --git a/en/assets/custom1.js b/en/assets/custom3.js similarity index 90% rename from en/assets/custom1.js rename to en/assets/custom3.js index 579bde3..3306fa0 100644 --- a/en/assets/custom1.js +++ b/en/assets/custom3.js @@ -110,6 +110,17 @@ var initAll = function () { pagePath = "index" } + // add vistors count + var ele = document.createElement("div"); + ele.setAttribute("align","center"); + var count = document.createElement("img") + count.setAttribute("src", "https://visitor-badge.glitch.me/badge?page_id=practice/en/" + path); + ele.appendChild(count); + var divider =document.createElement("hr") + + document.getElementById("giscus-container").appendChild(ele); + document.getElementById("giscus-container").appendChild(divider); + // 选取浏览器默认使用的语言 const lang = navigator.language || navigator.userLanguage diff --git a/en/assets/lang.js b/en/assets/lang.js new file mode 100644 index 0000000..061d375 --- /dev/null +++ b/en/assets/lang.js @@ -0,0 +1,26 @@ +(function () { + var path = window.location.pathname; + var link = "https://practice.rs" + path; + var word = "English"; + var lang = "zh-CN"; + var changeLang = "切换到英语"; + + if (window.location.href.indexOf("zh.") == -1) { + link = "https://zh.practice.rs" + path; + word = "简体中文"; + lang = "en"; + changeLang = "Switch to Chinese" + } + + var lang_node = ''; + if (link != '') { + lang_node = ' ' + word + ''; + } + + console.log(lang_node) + var insertNode = document.getElementsByClassName('right-buttons'); + if (insertNode.length > 0) { + var html = insertNode[0].innerHTML; + insertNode[0].innerHTML = html + lang_node; + } + })() \ No newline at end of file diff --git a/en/book.toml b/en/book.toml index 4d4bab2..eae6c2c 100644 --- a/en/book.toml +++ b/en/book.toml @@ -14,7 +14,7 @@ level = 1 [output.html] additional-css = ["theme/style1.css"] -additional-js = ["assets/custom1.js"] +additional-js = ["assets/custom3.js","assets/lang.js"] git-repository-url = "https://github.com/sunface/rust-by-practice" edit-url-template = "https://github.com/sunface/rust-by-practice/edit/master/en/{path}" diff --git a/en/deploy.sh b/en/deploy.sh index 899da72..880ab5c 100755 --- a/en/deploy.sh +++ b/en/deploy.sh @@ -4,8 +4,6 @@ mdbook build ## copy CNAME info to book dir cp ./assets/CNAME ./book/ -cp ./assets/*.html ./book/ -cp ./assets/sitemap.xml ./book/ ## init git repo cd book diff --git a/en/src/lifetime/advance.md b/en/src/lifetime/advance.md index 4ee0e07..cba2b30 100644 --- a/en/src/lifetime/advance.md +++ b/en/src/lifetime/advance.md @@ -1 +1,3 @@ # advance + +aaaa \ No newline at end of file diff --git a/zh-CN/assets/custom1.js b/zh-CN/assets/custom3.js similarity index 90% rename from zh-CN/assets/custom1.js rename to zh-CN/assets/custom3.js index 579bde3..3306fa0 100644 --- a/zh-CN/assets/custom1.js +++ b/zh-CN/assets/custom3.js @@ -110,6 +110,17 @@ var initAll = function () { pagePath = "index" } + // add vistors count + var ele = document.createElement("div"); + ele.setAttribute("align","center"); + var count = document.createElement("img") + count.setAttribute("src", "https://visitor-badge.glitch.me/badge?page_id=practice/en/" + path); + ele.appendChild(count); + var divider =document.createElement("hr") + + document.getElementById("giscus-container").appendChild(ele); + document.getElementById("giscus-container").appendChild(divider); + // 选取浏览器默认使用的语言 const lang = navigator.language || navigator.userLanguage diff --git a/zh-CN/assets/lang.js b/zh-CN/assets/lang.js new file mode 100644 index 0000000..061d375 --- /dev/null +++ b/zh-CN/assets/lang.js @@ -0,0 +1,26 @@ +(function () { + var path = window.location.pathname; + var link = "https://practice.rs" + path; + var word = "English"; + var lang = "zh-CN"; + var changeLang = "切换到英语"; + + if (window.location.href.indexOf("zh.") == -1) { + link = "https://zh.practice.rs" + path; + word = "简体中文"; + lang = "en"; + changeLang = "Switch to Chinese" + } + + var lang_node = ''; + if (link != '') { + lang_node = ' ' + word + ''; + } + + console.log(lang_node) + var insertNode = document.getElementsByClassName('right-buttons'); + if (insertNode.length > 0) { + var html = insertNode[0].innerHTML; + insertNode[0].innerHTML = html + lang_node; + } + })() \ No newline at end of file diff --git a/zh-CN/book.toml b/zh-CN/book.toml index f65c86c..9e3feb1 100644 --- a/zh-CN/book.toml +++ b/zh-CN/book.toml @@ -14,7 +14,7 @@ level = 1 [output.html] additional-css = ["theme/style1.css"] -additional-js = ["assets/custom1.js"] +additional-js = ["assets/custom3.js","assets/lang.js"] git-repository-url = "https://github.com/sunface/rust-by-practice" edit-url-template = "https://github.com/sunface/rust-by-practice/edit/master/zh-CN/{path}" diff --git a/zh-CN/src/lifetime/advance.md b/zh-CN/src/lifetime/advance.md index 4ee0e07..cba2b30 100644 --- a/zh-CN/src/lifetime/advance.md +++ b/zh-CN/src/lifetime/advance.md @@ -1 +1,3 @@ # advance + +aaaa \ No newline at end of file