From a01d15d883b454e122013eb747342e77c43eeb48 Mon Sep 17 00:00:00 2001 From: anime Date: Sat, 12 Apr 2025 02:50:09 +0800 Subject: [PATCH] feat(email encrypt): implement base64 encoding for contact email config, only supports HEO theme --- conf/contact.config.js | 7 ++++++- lib/notion/getNotionConfig.js | 11 ++++++++--- lib/plugins/mailEncrypt.js | 12 ++++++++++++ themes/heo/components/SocialButton.js | 13 +++++++++---- 4 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 lib/plugins/mailEncrypt.js diff --git a/conf/contact.config.js b/conf/contact.config.js index d2a53274..2784e360 100644 --- a/conf/contact.config.js +++ b/conf/contact.config.js @@ -3,7 +3,12 @@ */ module.exports = { // 社交链接,不需要可留空白,例如 CONTACT_WEIBO:'' - CONTACT_EMAIL: process.env.NEXT_PUBLIC_CONTACT_EMAIL || '', // 邮箱地址 例如mail@tangly1024.com + CONTACT_EMAIL: + (process.env.NEXT_PUBLIC_CONTACT_EMAIL && + btoa( + unescape(encodeURIComponent(process.env.NEXT_PUBLIC_CONTACT_EMAIL)) + )) || + '', // 邮箱地址 例如mail@tangly1024.com CONTACT_WEIBO: process.env.NEXT_PUBLIC_CONTACT_WEIBO || '', // 你的微博个人主页 CONTACT_TWITTER: process.env.NEXT_PUBLIC_CONTACT_TWITTER || '', // 你的twitter个人主页 CONTACT_GITHUB: process.env.NEXT_PUBLIC_CONTACT_GITHUB || '', // 你的github个人主页 例如 https://github.com/tangly1024 diff --git a/lib/notion/getNotionConfig.js b/lib/notion/getNotionConfig.js index 029c7b96..3e111fcd 100644 --- a/lib/notion/getNotionConfig.js +++ b/lib/notion/getNotionConfig.js @@ -157,9 +157,14 @@ export async function getConfigMapFromConfigPage(allPages) { // 只导入生效的配置 if (config.enable) { // console.log('[Notion配置]', config.key, config.value) - notionConfig[config.key] = - parseTextToJson(config.value) || config.value || null - // 配置不能是undefined,至少是null + if (config.key === 'CONTACT_EMAIL') { + notionConfig[config.key] = + (config.value && btoa(unescape(encodeURIComponent(config.value)))) || null + } else { + notionConfig[config.key] = + parseTextToJson(config.value) || config.value || null + // 配置不能是undefined,至少是null + } } } } diff --git a/lib/plugins/mailEncrypt.js b/lib/plugins/mailEncrypt.js new file mode 100644 index 00000000..091c39b3 --- /dev/null +++ b/lib/plugins/mailEncrypt.js @@ -0,0 +1,12 @@ +export const handleEmailClick = (e, emailIcon, CONTACT_EMAIL) => { + if (CONTACT_EMAIL && emailIcon && !emailIcon.current.href) { + e.preventDefault() + try { + const email = decodeURIComponent(escape(atob(CONTACT_EMAIL))) + emailIcon.current.href = `mailto:${email}` + emailIcon.current.click() + } catch (error) { + console.error('解密邮箱失败:', error) + } + } +} diff --git a/themes/heo/components/SocialButton.js b/themes/heo/components/SocialButton.js index 2b18b467..d6f86562 100644 --- a/themes/heo/components/SocialButton.js +++ b/themes/heo/components/SocialButton.js @@ -1,4 +1,6 @@ import { siteConfig } from '@/lib/config' +import { useRef } from 'react' +import { handleEmailClick } from '@/lib/plugins/mailEncrypt' /** * 社交联系方式按钮组 @@ -16,6 +18,9 @@ const SocialButton = () => { const ENABLE_RSS = siteConfig('ENABLE_RSS') const CONTACT_BILIBILI = siteConfig('CONTACT_BILIBILI') const CONTACT_YOUTUBE = siteConfig('CONTACT_YOUTUBE') + + const emailIcon = useRef(null) + return (
@@ -75,10 +80,10 @@ const SocialButton = () => { )} {CONTACT_EMAIL && ( + onClick={e => handleEmailClick(e, emailIcon, CONTACT_EMAIL)} + title='email' + className='cursor-pointer' + ref={emailIcon}> )}