From c512a5fda59b4b66f11146e3b6925e30c9f78ff1 Mon Sep 17 00:00:00 2001 From: Jerry <16351105+jerryc127@users.noreply.github.com> Date: Tue, 18 Aug 2020 00:33:05 +0800 Subject: [PATCH 01/11] =?UTF-8?q?1.=20chore:=20=E5=8F=AA=E6=94=AF=E6=8C=81?= =?UTF-8?q?hexo=204.2.0=E4=BB=A5=E4=B8=8A=E7=89=88=E6=9C=AC=202.=20feat:?= =?UTF-8?q?=20=E5=8F=AF=E9=9A=B1=E8=97=8FMobile=20sidebar=20=E5=AD=90?= =?UTF-8?q?=E8=8F=9C=E5=96=AE=203.=20refactor:=20archives=E5=92=8Ccard-pos?= =?UTF-8?q?t=20=E6=94=B9=E7=82=BAflex=E4=BD=88=E5=B1=80=204.=20feat:=20?= =?UTF-8?q?=E7=95=B6=E5=AF=AC=E5=BA=A6=E5=B0=8F=E6=96=BC1024=E6=99=82?= =?UTF-8?q?=EF=BC=8C=E9=BB=9E=E6=93=8Atoc=E6=BB=BE=E5=8B=95=EF=BC=8C?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=BB=BE=E5=8B=95=E6=95=88=E6=9E=9C=205.=20f?= =?UTF-8?q?eat:=20=E9=BB=9E=E6=93=8A=E6=BB=BE=E5=8B=95=EF=BC=8C=E5=A6=82?= =?UTF-8?q?=E6=9E=9C=E6=BB=BE=E5=8B=95=E6=98=AF=E5=90=91=E4=B8=8A=E6=99=82?= =?UTF-8?q?=EF=BC=8C=E5=B0=87=E6=9C=83=E5=81=8F=E7=A7=BB60px=EF=BC=8C=20?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E5=B0=8E=E8=88=AA=E6=AC=84=E9=81=AE=E6=93=8B?= =?UTF-8?q?=20close=20#317=206.=20feat:=20=E5=8F=AF=E9=85=8D=E7=BD=AEfont-?= =?UTF-8?q?size=207.=20refactor:=20=E5=8E=BB=E9=99=A4=E5=AD=97=E9=AB=94?= =?UTF-8?q?=E6=94=BE=E5=A4=A7=E3=80=81=E7=B8=AE=E5=B0=8F=E6=8C=89=E9=88=95?= =?UTF-8?q?=208.=20refactor:=20=E9=83=A8=E5=88=86css=E7=94=B1'px'=E6=94=B9?= =?UTF-8?q?=E7=82=BA'em'=EF=BC=8C=E9=81=A9=E6=87=89font-size=E5=80=BC?= =?UTF-8?q?=E6=94=B9=E8=AE=8A=E8=80=8C=E6=94=B9=E8=AE=8A=209.=20=20feat:?= =?UTF-8?q?=20pjax=E5=A2=9E=E5=8A=A0exclude=E9=85=8D=E7=BD=AE=2010.=20fix:?= =?UTF-8?q?=20=E4=BF=AE=E5=BE=A9canvas=5Fnest=20=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=B2=92=E6=9C=89=E7=94=9F=E6=95=88=E7=9A=84Bugs=2011.=20feat:?= =?UTF-8?q?=20=E6=96=B0=E5=A2=9E=20cnzz=5Fanalytics=2012.=20feat:=20?= =?UTF-8?q?=E9=A8=B0=E8=A8=8A=E5=88=86=E6=9E=90=E6=94=B9=E7=82=BA=E9=A8=B0?= =?UTF-8?q?=E8=A8=8A=E7=A7=BB=E5=8B=95=E5=88=86=E6=9E=90=EF=BC=8C=E4=B8=A6?= =?UTF-8?q?=E9=81=A9=E9=85=8Dpjax=2013.=20perf:=20=E6=94=B9cookies?= =?UTF-8?q?=E7=82=BAlocalstore=E5=AD=98=E5=84=B2=2014.=20feat:=20=E8=A8=AD?= =?UTF-8?q?=E7=BD=AE=E4=B8=AD=E7=9A=84theme=5Fcolor=E5=8F=AF=E5=96=AE?= =?UTF-8?q?=E7=8D=A8=E9=85=8D=E7=BD=AE=E5=B9=BE=E5=80=8B=EF=BC=8C=E8=80=8C?= =?UTF-8?q?=E4=B8=8D=E9=9C=80=E8=A6=81=E5=85=A8=E9=83=A8=E9=96=8B=E5=95=9F?= =?UTF-8?q?=20close=20#314=2015.=20feat:=20butterfly.yml=20=E6=8E=A1?= =?UTF-8?q?=E7=94=A8deepMerge,=20=E8=88=87=5Fconfig=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E9=80=B2=E8=A1=8C=E5=90=88=E4=BD=B5=E8=80=8C=E4=B8=8D=E6=98=AF?= =?UTF-8?q?=E6=9B=BF=E6=8F=9B=2016.=20fix:=20=E4=BF=AE=E5=BE=A9rightsize-b?= =?UTF-8?q?ottom=E9=85=8D=E7=BD=AE=E7=84=A1=E6=95=88=E7=9A=84bug=2017.=20r?= =?UTF-8?q?efactor:=20aside=E5=A2=9E=E5=8A=A0position=20sticky=2018.=20ref?= =?UTF-8?q?actor:=20=E8=AA=BF=E6=95=B4webinfo=20=E5=AF=AC=E5=BA=A6?= =?UTF-8?q?=E5=B0=8F=E6=99=82=E7=9A=84=E9=A1=AF=E7=A4=BA=2019.=20feat:=20?= =?UTF-8?q?=E6=9B=B4=E6=94=B9addtoany=20item=E7=9A=84=E5=AF=AB=E6=B3=95=20?= =?UTF-8?q?20.=20refactor:=20aside=20=E5=85=A7=E5=AE=B9=E9=82=8A=E8=B7=9D?= =?UTF-8?q?=E8=AA=BF=E6=95=B4=2021.=20feat:=20=E5=A2=9E=E5=8A=A0=E6=9C=80?= =?UTF-8?q?=E5=BE=8C=E6=9B=B4=E6=96=B0=E6=99=82=E9=96=93=E9=A1=AF=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/ISSUE_TEMPLATE/bug_report.md | 2 +- _config.yml | 43 +- languages/default.yml | 9 +- languages/en.yml | 9 +- languages/zh-CN.yml | 10 +- languages/zh-TW.yml | 9 +- layout/includes/additional-js.pug | 7 +- layout/includes/head.pug | 5 +- layout/includes/head/analytics.pug | 14 +- layout/includes/head/config.pug | 89 ++-- layout/includes/head/darkmode.pug | 103 +++-- layout/includes/head/noscript.pug | 2 +- layout/includes/head/preconnect.pug | 5 +- layout/includes/header/menu_item.pug | 5 +- layout/includes/header/nav.pug | 6 +- layout/includes/rightside.pug | 4 - layout/includes/third-party/chat/gitter.pug | 2 +- layout/includes/third-party/effect.pug | 4 +- layout/includes/third-party/math/mermaid.pug | 2 +- layout/includes/third-party/pjax.pug | 17 +- .../includes/third-party/share/addtoany.pug | 3 +- layout/includes/widget/card_recent_post.pug | 18 +- layout/includes/widget/card_tags.pug | 4 +- layout/includes/widget/card_webinfo.pug | 13 +- layout/includes/widget/index.pug | 29 +- layout/page.pug | 2 +- package.json | 4 +- scripts/events/check_hexo.js | 12 + scripts/events/replace_config.js | 8 +- scripts/events/{log.js => welcome.js} | 6 +- scripts/helpers/page.js | 6 +- scripts/tags/tabs.js | 2 +- source/css/_global/function.styl | 31 +- source/css/_global/index.styl | 20 +- source/css/_highlight/highlight.styl | 13 +- source/css/_layout/404.styl | 8 +- source/css/_layout/aside.styl | 149 ++++--- source/css/_layout/category.styl | 33 -- source/css/_layout/comments.styl | 6 +- source/css/_layout/flink.styl | 8 +- source/css/_layout/head.styl | 72 ++-- source/css/_layout/mobile-sidebar.styl | 17 +- source/css/_layout/page.styl | 382 ------------------ source/css/_layout/pagination.styl | 2 +- source/css/_layout/post.styl | 43 +- source/css/_layout/relatedposts.styl | 6 +- source/css/_layout/rightside.styl | 2 +- source/css/_layout/sidebar.styl | 10 +- source/css/_layout/third-party.styl | 2 + source/css/_mode/darkmode.styl | 1 + source/css/_page/archives.styl | 115 ++++++ source/css/_page/categories.styl | 60 +++ source/css/_page/common.styl | 51 +++ source/css/_page/homepage.styl | 143 +++++++ source/css/_page/tags.styl | 14 + source/css/_search/algolia.styl | 10 +- source/css/_search/index.styl | 6 +- source/css/_search/local-search.styl | 12 +- source/css/_tags/gallery.styl | 6 +- source/css/_tags/tabs.styl | 4 +- source/css/index.styl | 1 + source/css/var.styl | 47 +-- source/js/main.js | 178 ++++---- source/js/third-party/canvas-nest.js | 7 +- source/js/tw_cn.js | 9 +- source/js/utils.js | 33 +- 66 files changed, 1032 insertions(+), 923 deletions(-) create mode 100644 scripts/events/check_hexo.js rename scripts/events/{log.js => welcome.js} (85%) delete mode 100644 source/css/_layout/category.styl delete mode 100644 source/css/_layout/page.styl create mode 100644 source/css/_page/archives.styl create mode 100644 source/css/_page/categories.styl create mode 100644 source/css/_page/common.styl create mode 100644 source/css/_page/homepage.styl create mode 100644 source/css/_page/tags.styl diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index 00656fe..f9db5fb 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -12,7 +12,7 @@ IMPORTANT: Please follow the template to create a new issue. 重要:請依照該模板來提交。 If you upgrade from the old version, and an error occurs when running, please copy the new content in the config to the butterfly.yml -如果你是由舊版本升級到新版,執行時出現報錯,請首先把config裡新的內容複製到舊的butterfly設定去(如有) +如果你是由舊版本升級到新版,執行時出現報錯,請首先把config裡新的內容複製到舊的butterfly設定去(如有) If you are a problem when visit the website, please open your browser 'developer tools (shortcut F12)' and check the console if there is an error, include your website address in the feedback 如果你是線上訪問出現問題,請檢查瀏覽器‘開發人員工具(快捷鍵F12)’的console是否有報錯,反饋時附上你的網站 diff --git a/_config.yml b/_config.yml index 7f52dc6..0212fb6 100644 --- a/_config.yml +++ b/_config.yml @@ -17,6 +17,9 @@ menu: # Link: /link/ || fas fa-link # About: /about/ || fas fa-heart +# Hide the child menu items in mobile sidebar +hide_sidebar_menu_child: false + # Code Blocks (代碼相關) # -------------------------------------- @@ -125,7 +128,7 @@ cover: position: both # When cover is not set, the default cover is displayed (當沒有設置cover時,默認的封面顯示) default_cover: - - https://i.loli.net/2020/05/01/gkihqEjXxJ5UZ1C.jpg + # - https://i.loli.net/2020/05/01/gkihqEjXxJ5UZ1C.jpg # Replace Broken Images (替換無法顯示的圖片) error_img: @@ -232,14 +235,7 @@ sharejs: # https://www.addtoany.com/ addtoany: enable: false - item: - - facebook - - twitter - - wechat - - sina_weibo - - facebook_messenger - - email - - copy_link + item: facebook,twitter,wechat,sina_weibo,facebook_messenger,email,copy_link # Comments System # -------------------------------------- @@ -252,7 +248,7 @@ comments: # - Disqus text: true # Display the comment name next to the button # lazyload: The comment system will be load when comment element enters the browser's viewport. - # If you set it to false, the comment count will be invalid + # If you set it to true, the comment count will be invalid lazyload: false count: false # Display comment count in top_img @@ -374,7 +370,7 @@ footer: owner: enable: true since: 2020 - custom_text: + custom_text: copyright: true # Copyright of theme and framework ICP: # Chinese ICP License enable: false @@ -386,14 +382,21 @@ footer: # -------------------------------------- # Baidu Analytics +# https://tongji.baidu.com/web/welcome/login baidu_analytics: # Google Analytics +# https://analytics.google.com/analytics/web/ google_analytics: # Tencent Analytics ID +# https://mta.qq.com tencent_analytics: +# CNZZ Analytics +# https://www.umeng.com/ +cnzz_analytics: + # Advertisement # -------------------------------------- @@ -528,9 +531,9 @@ click_heart: false ClickShowText: enable: false text: - - I - - LOVE - - YOU + # - I + # - LOVE + # - YOU fontSize: 15px # Default display mode (網站默認的顯示模式) @@ -547,6 +550,7 @@ beautify: # Global font settings # Don't modify the following settings unless you know how they work (非必要不要修改) font: + global-font-size: # The unit must be 'px', default is 14px font-family: code-font: @@ -621,7 +625,10 @@ aside: format: MMMM YYYY # eg: YYYY年MM月 order: -1 # Sort of order. 1, asc for ascending; -1, desc for descending limit: 8 # if set 0 will show all - card_webinfo: true + card_webinfo: + enable: true + post_count: true + last_push_date: true # busuanzi count for PV / UV in site # 訪問人數 @@ -709,7 +716,11 @@ note: # Pjax [Beta] # It may contain bugs and unstable, give feedback when you find the bugs. # https://github.com/MoOx/pjax -pjax: false +pjax: + enable: false + exclude: + # - xxxx + # - xxxx # Inject the css and script (aplayer/meting) aplayerInject: diff --git a/languages/default.yml b/languages/default.yml index e3a5524..137ed36 100644 --- a/languages/default.yml +++ b/languages/default.yml @@ -65,7 +65,12 @@ aside: card_webinfo: headline: Info article_name: Article - runtime_name: Run time + runtime: + name: Run time + unit: days + last_push_date: + name: Last Push + suffix: days ago site_wordcount: Total Count site_uv_name: UV site_pv_name: PV @@ -87,8 +92,6 @@ rightside: scroll_to_comment: Scroll To Comments setting: Setting -runtime_unit: days - copy_copyright: author: Author link: Link diff --git a/languages/en.yml b/languages/en.yml index e3a5524..137ed36 100644 --- a/languages/en.yml +++ b/languages/en.yml @@ -65,7 +65,12 @@ aside: card_webinfo: headline: Info article_name: Article - runtime_name: Run time + runtime: + name: Run time + unit: days + last_push_date: + name: Last Push + suffix: days ago site_wordcount: Total Count site_uv_name: UV site_pv_name: PV @@ -87,8 +92,6 @@ rightside: scroll_to_comment: Scroll To Comments setting: Setting -runtime_unit: days - copy_copyright: author: Author link: Link diff --git a/languages/zh-CN.yml b/languages/zh-CN.yml index ae6b207..99ce029 100644 --- a/languages/zh-CN.yml +++ b/languages/zh-CN.yml @@ -67,7 +67,12 @@ aside: card_webinfo: headline: 网站资讯 article_name: 文章数目 - runtime_name: 已运行时间 + runtime: + name: 已运行时间 + unit: 天 + last_push_date: + name: 最后更新时间 + suffix: 天前 site_wordcount: 本站总字数 site_uv_name: 本站访客数 site_pv_name: 本站总访问量 @@ -89,8 +94,6 @@ rightside: scroll_to_comment: 直达评论 setting: 设置 -runtime_unit: 天 - copy_copyright: author: 作者 link: 链接 @@ -106,7 +109,6 @@ Snackbar: day_to_night: 你已切换为深色模式 night_to_day: 你已切换为浅色模式 -error_title: 页面没有找到 loading: 加载中... error404: diff --git a/languages/zh-TW.yml b/languages/zh-TW.yml index 74902a2..1eb98f6 100644 --- a/languages/zh-TW.yml +++ b/languages/zh-TW.yml @@ -67,7 +67,12 @@ aside: card_webinfo: headline: 網站資訊 article_name: 文章數目 - runtime_name: 已執行時間 + runtime: + name: 已運行時間 + unit: 天 + last_push_date: + name: 最後更新時間 + suffix: 天前 site_wordcount: 本站總字數 site_uv_name: 本站訪客數 site_pv_name: 本站總訪問量 @@ -89,8 +94,6 @@ rightside: scroll_to_comment: 直達評論 setting: 設定 -runtime_unit: 天 - copy_copyright: author: 作者 link: 連結 diff --git a/layout/includes/additional-js.pug b/layout/includes/additional-js.pug index 2e00ca6..263d805 100644 --- a/layout/includes/additional-js.pug +++ b/layout/includes/additional-js.pug @@ -51,11 +51,10 @@ div !=partial('includes/third-party/chat/index', {}, {cache:theme.fragment_cache}) if theme.aplayerInject && theme.aplayerInject.enable - if theme.pjax || theme.aplayerInject.per_page + if theme.pjax.enable || theme.aplayerInject.per_page include ./head/aplayer.pug else if page.aplayer include ./head/aplayer.pug - if theme.pjax - !=partial('includes/third-party/pjax', {}, {cache:theme.fragment_cache}) - + if theme.pjax.enable + !=partial('includes/third-party/pjax', {}, {cache:theme.fragment_cache}) \ No newline at end of file diff --git a/layout/includes/head.pug b/layout/includes/head.pug index ea3808f..b483de9 100644 --- a/layout/includes/head.pug +++ b/layout/includes/head.pug @@ -53,9 +53,6 @@ if theme.facebook_comments.user_id if (theme.pwa && theme.pwa.enable) !=partial('includes/head/pwa', {}, {cache:theme.fragment_cache}) -if theme.darkmode.enable - !=partial('includes/head/darkmode', {}, {cache:theme.fragment_cache}) - //- main css link(rel='stylesheet', href=url_for(theme.CDN.main_css)) link(rel='stylesheet', href=url_for(theme.CDN.fontawesome)) @@ -86,5 +83,7 @@ if theme.blog_title_font.font_link include ./head/config_site.pug include ./head/noscript.pug +!=partial('includes/head/darkmode', {}, {cache:theme.fragment_cache}) + !=fragment_cache('injectHead', function(){return injectHtml(theme.inject.head)}) diff --git a/layout/includes/head/analytics.pug b/layout/includes/head/analytics.pug index 01b0469..f4379b5 100644 --- a/layout/includes/head/analytics.pug +++ b/layout/includes/head/analytics.pug @@ -17,4 +17,16 @@ if theme.google_analytics gtag('config', '!{theme.google_analytics}'); if theme.tencent_analytics - script(src=`https://tajs.qq.com/stats?sId=${theme.tencent_analytics}` charset="UTF-8" data-pjax) + script. + var _mtac = {}; + (function() { + var mta = document.createElement("script"); + mta.src = "//pingjs.qq.com/h5/stats.js?v2.0.4"; + mta.setAttribute("name", "MTAH5"); + mta.setAttribute("sid", "!{theme.tencent_analytics}"); + var s = document.getElementsByTagName("script")[0]; + s.parentNode.insertBefore(mta, s); + })(); + +if theme.cnzz_analytics + script(async data-pjax src=`https://s4.cnzz.com/z_stat.php?id=${theme.cnzz_analytics}&web_id=${theme.cnzz_analytics}`) diff --git a/layout/includes/head/config.pug b/layout/includes/head/config.pug index 61c26ea..8237842 100644 --- a/layout/includes/head/config.pug +++ b/layout/includes/head/config.pug @@ -1,6 +1,6 @@ - - var algolia = 'undefined'; - var env = process.env; + let algolia = 'undefined'; + let env = process.env; if (theme.algolia_search.enable) { algolia = JSON.stringify({ appId: env.ALGOLIA_APP_ID || config.algolia.appId || config.algolia.applicationID, @@ -16,7 +16,7 @@ }) } - var localSearch = 'undefined'; + let localSearch = 'undefined'; if (theme.local_search && theme.local_search.enable) { localSearch = JSON.stringify({ path: config.search.path, @@ -26,7 +26,8 @@ } }) } - var translate = 'undefined'; + + let translate = 'undefined'; if (theme.translate && theme.translate.enable){ translate = JSON.stringify({ defaultEncoding: theme.translate.defaultEncoding, @@ -36,7 +37,7 @@ }) } - var copyright = 'undefined'; + let copyright = 'undefined'; if (theme.copy.enable && theme.copy.copyright.enable){ copyright = JSON.stringify({ limitCount: theme.copy.copyright.limit_count, @@ -49,7 +50,7 @@ }) } - var ClickShowText = 'undefined'; + let ClickShowText = 'undefined'; if (theme.ClickShowText && theme.ClickShowText.enable) { ClickShowText = JSON.stringify({ text: theme.ClickShowText.text.join(","), @@ -58,24 +59,24 @@ } - var Snackbar = 'undefined'; + let Snackbar = 'undefined'; if (theme.snackbar && theme.snackbar.enable) { - Snackbar = JSON.stringify({ - bookmark: { - message_prev: _p("Snackbar.bookmark.message_prev"), - message_next: _p("Snackbar.bookmark.message_next") - }, - chs_to_cht: _p("Snackbar.chs_to_cht"), - cht_to_chs: _p("Snackbar.cht_to_chs"), - day_to_night: _p("Snackbar.day_to_night"), - night_to_day: _p("Snackbar.night_to_day"), - bgLight: theme.snackbar.bg_light, - bgDark: theme.snackbar.bg_dark, - position: theme.snackbar.position, + Snackbar = JSON.stringify({ + bookmark: { + message_prev: _p("Snackbar.bookmark.message_prev"), + message_next: _p("Snackbar.bookmark.message_next") + }, + chs_to_cht: _p("Snackbar.chs_to_cht"), + cht_to_chs: _p("Snackbar.cht_to_chs"), + day_to_night: _p("Snackbar.day_to_night"), + night_to_day: _p("Snackbar.night_to_day"), + bgLight: theme.snackbar.bg_light, + bgDark: theme.snackbar.bg_dark, + position: theme.snackbar.position, }) } - var noticeOutdate = 'undefined'; + let noticeOutdate = 'undefined'; if (theme.noticeOutdate && theme.noticeOutdate.enable) { noticeOutdate = JSON.stringify({ limitDay: theme.noticeOutdate.limit_day, @@ -85,6 +86,15 @@ }) } + let highlight = 'undefined'; + if ((config.highlight && config.highlight.enable) || (config.prismjs && config.prismjs.enable)) { + highlight = JSON.stringify({ + plugin: config.highlight.enable ? 'highlighjs' : 'prismjs', + highlightCopy: theme.highlight_copy, + highlightLang: theme.highlight_lang + }) + } + script. var GLOBAL_CONFIG = { root: '!{config.root}', @@ -93,6 +103,7 @@ script. localSearch: !{localSearch}, translate: !{translate}, noticeOutdate: !{noticeOutdate}, + highlight: !{highlight}, copy: { success: '!{_p("copy.success")}', error: '!{_p("copy.error")}', @@ -102,8 +113,8 @@ script. message_prev: '!{_p("Snackbar.bookmark.message_prev")}', message_next: '!{_p("Snackbar.bookmark.message_next")}' }, - runtime_unit: '!{_p("runtime_unit")}', - runtime: !{theme.runtimeshow.enable}, + runtime: '!{theme.runtimeshow.enable ? _p("aside.card_webinfo.runtime.unit") : ""}', + last_push_date: '!{theme.aside.card_webinfo.last_push_date ? _p("aside.card_webinfo.last_push_date.suffix") : ""}', copyright: !{copyright}, ClickShowText: !{ClickShowText}, medium_zoom: !{theme.medium_zoom}, @@ -114,10 +125,38 @@ script. css: '!{theme.CDN.justifiedGallery_css}' }, baiduPush: !{theme.baidu_push}, - highlightCopy: !{theme.highlight_copy}, - highlightLang: !{theme.highlight_lang}, isPhotoFigcaption: !{theme.photofigcaption}, islazyload: !{theme.lazyload.enable}, - isanchor: !{theme.anchor} + isanchor: !{theme.anchor} + }; + + var saveToLocal = { + set: function setWithExpiry(key, value, ttl) { + const now = new Date() + const expiryDay = ttl * 86400000 + const item = { + value: value, + expiry: now.getTime() + expiryDay, + } + localStorage.setItem(key, JSON.stringify(item)) + }, + + get: function getWithExpiry(key) { + const itemStr = localStorage.getItem(key) + + if (!itemStr) { + return undefined + } + const item = JSON.parse(itemStr) + const now = new Date() + + if (now.getTime() > item.expiry) { + localStorage.removeItem(key) + return undefined + } + return item.value + } } + + diff --git a/layout/includes/head/darkmode.pug b/layout/includes/head/darkmode.pug index cc1a27b..51bf852 100644 --- a/layout/includes/head/darkmode.pug +++ b/layout/includes/head/darkmode.pug @@ -1,56 +1,51 @@ -script. - var activateDarkMode = function () { - document.documentElement.setAttribute('data-theme', 'dark') - if (document.querySelector('meta[name="theme-color"]') !== null) { - document.querySelector('meta[name="theme-color"]').setAttribute('content', '#000') - } - } - var activateLightMode = function () { - document.documentElement.setAttribute('data-theme', 'light') - if (document.querySelector('meta[name="theme-color"]') !== null) { - document.querySelector('meta[name="theme-color"]').setAttribute('content', '#fff') - } - } - - var getCookies = function (name) { - const value = `; ${document.cookie}` - const parts = value.split(`; ${name}=`) - if (parts.length === 2) return parts.pop().split(';').shift() - } - - var autoChangeMode = '#{theme.darkmode.autoChangeMode}' - var t = getCookies('theme') - if (autoChangeMode === '1') { - var isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches - var isLightMode = window.matchMedia('(prefers-color-scheme: light)').matches - var isNotSpecified = window.matchMedia('(prefers-color-scheme: no-preference)').matches - var hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified - - if (t === undefined) { - if (isLightMode) activateLightMode() - else if (isDarkMode) activateDarkMode() - else if (isNotSpecified || hasNoSupport) { - console.log('You specified no preference for a color scheme or your browser does not support it. I Schedule dark mode during night time.') - var now = new Date() - var hour = now.getHours() - var isNight = hour <= 6 || hour >= 18 - isNight ? activateDarkMode() : activateLightMode() +if theme.darkmode.enable + script. + var activateDarkMode = function () { + document.documentElement.setAttribute('data-theme', 'dark') + if (document.querySelector('meta[name="theme-color"]') !== null) { + document.querySelector('meta[name="theme-color"]').setAttribute('content', '#000') } - window.matchMedia('(prefers-color-scheme: dark)').addListener(function (e) { - if (Cookies.get('theme') === undefined) { - e.matches ? activateDarkMode() : activateLightMode() + } + var activateLightMode = function () { + document.documentElement.setAttribute('data-theme', 'light') + if (document.querySelector('meta[name="theme-color"]') !== null) { + document.querySelector('meta[name="theme-color"]').setAttribute('content', '#fff') + } + } + + var autoChangeMode = '#{theme.darkmode.autoChangeMode}' + var t = saveToLocal.get('theme') + if (autoChangeMode === '1') { + var isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches + var isLightMode = window.matchMedia('(prefers-color-scheme: light)').matches + var isNotSpecified = window.matchMedia('(prefers-color-scheme: no-preference)').matches + var hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified + + if (t === undefined) { + if (isLightMode) activateLightMode() + else if (isDarkMode) activateDarkMode() + else if (isNotSpecified || hasNoSupport) { + console.log('You specified no preference for a color scheme or your browser does not support it. I Schedule dark mode during night time.') + var now = new Date() + var hour = now.getHours() + var isNight = hour <= 6 || hour >= 18 + isNight ? activateDarkMode() : activateLightMode() } - }) - } else if (t === 'light') activateLightMode() - else activateDarkMode() - } else if (autoChangeMode === '2') { - now = new Date() - hour = now.getHours() - isNight = hour <= 6 || hour >= 18 - if (t === undefined) isNight ? activateDarkMode() : activateLightMode() - else if (t === 'light') activateLightMode() - else activateDarkMode() - } else { - if (t === 'dark') activateDarkMode() - else if (t === 'light') activateLightMode() - } + window.matchMedia('(prefers-color-scheme: dark)').addListener(function (e) { + if (saveToLocal.get('theme') === undefined) { + e.matches ? activateDarkMode() : activateLightMode() + } + }) + } else if (t === 'light') activateLightMode() + else activateDarkMode() + } else if (autoChangeMode === '2') { + now = new Date() + hour = now.getHours() + isNight = hour <= 6 || hour >= 18 + if (t === undefined) isNight ? activateDarkMode() : activateLightMode() + else if (t === 'light') activateLightMode() + else activateDarkMode() + } else { + if (t === 'dark') activateDarkMode() + else if (t === 'light') activateLightMode() + } diff --git a/layout/includes/head/noscript.pug b/layout/includes/head/noscript.pug index bf995d7..b0f6e1d 100644 --- a/layout/includes/head/noscript.pug +++ b/layout/includes/head/noscript.pug @@ -1,5 +1,5 @@ noscript. -