From ea2b5209de22ad3a310ba20b0dd786a9b3427c04 Mon Sep 17 00:00:00 2001 From: tlyong1992 Date: Mon, 20 Jun 2022 10:34:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AD=97=E6=95=B0=E7=BB=9F=E8=AE=A1=E7=9B=B8?= =?UTF-8?q?=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/NotionPage.js | 4 ++-- themes/next/components/WordCount.js | 25 ++++++++++++++----------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/components/NotionPage.js b/components/NotionPage.js index 4d362180..e2e70971 100644 --- a/components/NotionPage.js +++ b/components/NotionPage.js @@ -10,11 +10,11 @@ const Code = dynamic(() => import('react-notion-x/build/third-party/code').then((m) => m.Code), { ssr: true } ) const Collection = dynamic(() => - import('react-notion-x/build/third-party/collection').then((m) => m.Collection), { ssr: false } + import('react-notion-x/build/third-party/collection').then((m) => m.Collection), { ssr: true } ) const Equation = dynamic(() => - import('react-notion-x/build/third-party/equation').then((m) => m.Equation), { ssr: false } + import('react-notion-x/build/third-party/equation').then((m) => m.Equation), { ssr: true } ) const Pdf = dynamic( diff --git a/themes/next/components/WordCount.js b/themes/next/components/WordCount.js index 7188d90c..bad2cf3a 100644 --- a/themes/next/components/WordCount.js +++ b/themes/next/components/WordCount.js @@ -7,7 +7,7 @@ import { useEffect } from 'react' export default function WordCount() { useEffect(() => { countWords() - }, []) + }) return
本文字数 0  |  阅读时长 ≈ 0 分钟 @@ -18,26 +18,29 @@ export default function WordCount() { * 更新字数统计和阅读时间 */ function countWords() { - const articleElement = document.getElementById('notion-article') - if (articleElement) { - const articleText = deleteHtmlTag(articleElement.innerHTML) - const wordCount = fnGetCpmisWords(articleText) - // 阅读速度 300-500每分钟 - document.getElementById('wordCount').innerHTML = wordCount - document.getElementById('readTime').innerHTML = Math.floor(wordCount / 400) + 1 - const wordCountWrapper = document.getElementById('wordCountWrapper') - wordCountWrapper.classList.remove('hidden') - } + const articleText = deleteHtmlTag(document.getElementById('notion-article')?.innerHTML) + const wordCount = fnGetCpmisWords(articleText) + // 阅读速度 300-500每分钟 + document.getElementById('wordCount').innerHTML = wordCount + document.getElementById('readTime').innerHTML = Math.floor(wordCount / 400) + 1 + const wordCountWrapper = document.getElementById('wordCountWrapper') + wordCountWrapper.classList.remove('hidden') } // 去除html标签 function deleteHtmlTag(str) { + if (!str) { + return '' + } str = str.replace(/<[^>]+>|&[^>]+;/g, '').trim()// 去掉所有的html标签和 之类的特殊符合 return str } // 用word方式计算正文字数 function fnGetCpmisWords(str) { + if (!str) { + return 0 + } let sLen = 0 try { // eslint-disable-next-line no-irregular-whitespace