From c2dc5ba47c5f0b3392fa87267c1a2008b0d1cb9e Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Thu, 5 Sep 2024 11:28:13 +0800 Subject: [PATCH] =?UTF-8?q?OPEN=5FWRITE=E5=AF=BC=E6=B5=81=E6=8F=92?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- components/OpenWrite.js | 60 +++++++++++++++++++++++++++++++++++++++++ lib/utils/index.js | 2 +- pages/[prefix]/index.js | 7 ++++- styles/globals.css | 4 +++ 5 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 components/OpenWrite.js diff --git a/.gitignore b/.gitignore index 42744c4b2cf..b400f485732 100644 --- a/.gitignore +++ b/.gitignore @@ -45,7 +45,7 @@ yarn-error.log* /public/robots.txt /public/sitemap.xml /public/rss/* - +/sitemap.xml # yarn package-lock.json diff --git a/components/OpenWrite.js b/components/OpenWrite.js new file mode 100644 index 00000000000..f199e50eb59 --- /dev/null +++ b/components/OpenWrite.js @@ -0,0 +1,60 @@ +import { siteConfig } from '@/lib/config' +import { isBrowser, loadExternalResource } from '@/lib/utils' +import { useRouter } from 'next/router' +import { useEffect, useRef } from 'react' +/** + * OpenWrite公众号导流插件 + * 使用介绍:https://openwrite.cn/guide/readmore/readmore.html#%E4%BA%8C%E3%80%81%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8 + * 登录后台配置你的博客:https://readmore.openwrite.cn/ + * @returns + */ +const OpenWrite = () => { + const router = useRouter() + const qrcode = siteConfig('OPEN_WRITE_QRCODE', '请配置公众号二维码') + const blogId = siteConfig('OPEN_WRITE_BLOG_ID') + const name = siteConfig('OPEN_WRITE_NAME', '请配置公众号名') + const id = 'article-wrapper' + const keyword = siteConfig('OPEN_WRITE_KEYWORD', '请配置公众号关键词') + const btnText = siteConfig( + 'OPEN_WRITE_BTN_TEXT', + '原创不易,完成人机检测,阅读全文' + ) + + const hasLoaded = useRef(false) + + const loadOpenWrite = async () => { + try { + await loadExternalResource( + 'https://readmore.openwrite.cn/js/readmore-2.0.js', + 'js' + ) + const BTWPlugin = window?.BTWPlugin + + if (BTWPlugin) { + const btw = new BTWPlugin() + window.btw = btw + btw.init({ + qrcode, + id, + name, + btnText, + keyword, + blogId + }) + } + } catch (error) { + console.error('OpenWrite 加载异常', error) + } + } + + useEffect(() => { + if (isBrowser && blogId && !hasLoaded.current) { + loadOpenWrite() + hasLoaded.current = true + } + }, [router]) + + return <> +} + +export default OpenWrite diff --git a/lib/utils/index.js b/lib/utils/index.js index 27a6494f4b9..7817ed74972 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -146,7 +146,7 @@ export function getLastPartOfUrl(url) { * @param type js 或 css * @returns {Promise} */ -export function loadExternalResource(url, type) { +export function loadExternalResource(url, type = 'js') { // 检查是否已存在 const elements = type === 'js' diff --git a/pages/[prefix]/index.js b/pages/[prefix]/index.js index dc480fcc531..2bb175b6dc7 100644 --- a/pages/[prefix]/index.js +++ b/pages/[prefix]/index.js @@ -1,5 +1,6 @@ import BLOG from '@/blog.config' import useNotification from '@/components/Notification' +import OpenWrite from '@/components/OpenWrite' import { siteConfig } from '@/lib/config' import { getGlobalData, getPost, getPostBlocks } from '@/lib/db/getSiteData' import { useGlobal } from '@/lib/global' @@ -95,8 +96,12 @@ const Slug = props => { }) return ( <> + {/* 文章布局 */} + {/* 解锁密码提示框 */} {post?.password && post?.password !== '' && !lock && } + {/* 导流工具 */} + ) } @@ -129,7 +134,7 @@ export async function getStaticProps({ params: { prefix }, locale }) { fullSlug += '.html' } } - + // 在列表内查找文章 props.post = props?.allPages?.find(p => { return ( diff --git a/styles/globals.css b/styles/globals.css index bdc13fe97df..c3a4818e2cb 100644 --- a/styles/globals.css +++ b/styles/globals.css @@ -272,3 +272,7 @@ a.avatar-wrapper { margin-right: 20px; } } + +img { + display: unset; +}