From 49c9e8e6cb46f200d1f7c4e16e037bc9e9f85b20 Mon Sep 17 00:00:00 2001 From: tlyong1992 Date: Mon, 6 Jun 2022 14:04:04 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A4=9C=E9=97=B4?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E5=88=87=E6=8D=A2Code=E9=87=8D=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/NotionPage.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/components/NotionPage.js b/components/NotionPage.js index 61b7fc56..957e9764 100644 --- a/components/NotionPage.js +++ b/components/NotionPage.js @@ -101,7 +101,12 @@ function addWatch4Dom(element) { switch (type) { case 'childList': if (mutation.target.className === 'notion-code-copy') { - fixCopy(mutation) + fixCopy(mutation.target) + } else if (mutation.target.className?.indexOf('language-') > -1) { + const copyCode = mutation.target.parentElement?.firstElementChild + if (copyCode) { + fixCopy(copyCode) + } } // console.log('A child node has been added or removed.') break @@ -128,12 +133,11 @@ function addWatch4Dom(element) { } /** - * 复制代码后,会重复 @see https://github.com/tangly1024/NotionNext/issues/165 - * @param {*} e - */ -function fixCopy(e) { - const codeE = e.target.parentElement.lastElementChild - // console.log('2', codeE) + * 复制代码后,会重复 @see https://github.com/tangly1024/NotionNext/issues/165 + * @param {*} e + */ +function fixCopy(codeCopy) { + const codeE = codeCopy.parentElement.lastElementChild const codeEnd = codeE.lastChild if (codeEnd.nodeName === '#text' && codeE.childNodes.length > 1) { codeEnd.nodeValue = null From 6c2d960fbb86116abef5bbfe754e4c236f6431a8 Mon Sep 17 00:00:00 2001 From: tlyong1992 Date: Mon, 6 Jun 2022 17:23:07 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E5=BC=95=E5=85=A5mongodb?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/cache/cache_manager.js | 8 ++++-- lib/cache/mongo_db_cache.js | 49 +++++++++++++++++++++++++++++++++++++ package.json | 1 + 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 lib/cache/mongo_db_cache.js diff --git a/lib/cache/cache_manager.js b/lib/cache/cache_manager.js index 4a667df4..ec921177 100644 --- a/lib/cache/cache_manager.js +++ b/lib/cache/cache_manager.js @@ -1,9 +1,13 @@ import MemoryCache from './memory_cache' import FileCache from './local_file_cache' +import MongoCache from './mongo_db_cache' const enableCache = true +const enableMongoCache = true let api -if (process.env.ENABLE_FILE_CACHE) { +if (enableMongoCache) { + api = MongoCache +} else if (process.env.ENABLE_FILE_CACHE) { api = FileCache } else { api = MemoryCache @@ -22,7 +26,7 @@ export async function getDataFromCache(key) { if (JSON.stringify(dataFromCache) === '[]') { return null } - return dataFromCache + return MongoCache.getCache(key) } export async function setDataToCache(key, data) { diff --git a/lib/cache/mongo_db_cache.js b/lib/cache/mongo_db_cache.js new file mode 100644 index 00000000..b3996470 --- /dev/null +++ b/lib/cache/mongo_db_cache.js @@ -0,0 +1,49 @@ +const MongoClient = require('mongodb').MongoClient + +const DB_URL = process.env.MONGO_DB_URL // e.g. mongodb+srv://mongo_user:[password]@xxx.mongodb.net//?retryWrites=true&w=majority +const DB_NAME = process.env.MONGO_DB_NAME // e.g. tangly1024 +const DB_COLLECTION = 'posts' + +export async function getCache (key) { + const client = await MongoClient.connect(DB_URL).catch(err => { console.error(err) }) + const dbo = client.db(DB_NAME) + const query = { block_id: key } + const res = await dbo.collection('posts').findOne(query).catch(err => { console.error(err) }) + await client.close() + return res +} + +/** + * 并发请求写文件异常; Vercel生产环境不支持写文件。 + * @param key + * @param data + * @returns {Promise} + */ +export async function setCache (key, data) { + const client = await MongoClient.connect(DB_URL).catch(err => { console.error(err) }) + const dbo = client.db(DB_NAME) + data.block_id = key + const query = { block_id: key } + const jsonObj = JSON.parse(JSON.stringify(data)) + + const updRes = await dbo.collection(DB_COLLECTION).updateOne(query, { $set: jsonObj }).catch(err => { console.error(err) }) + console.log('更新结果', key, updRes) + if (updRes.matchedCount === 0) { + const insertRes = await dbo.collection(DB_COLLECTION).insertOne(jsonObj).catch(err => { console.error(err) }) + console.log('插入结果', key, insertRes) + } + await client.close() + return data +} + +export async function delCache (key, data) { + const client = await MongoClient.connect(DB_URL).catch(err => { console.error(err) }) + const dbo = client.db(DB_NAME) + const query = { block_id: key } + const res = await dbo.collection('posts').deleteOne(query).catch(err => { console.error(err) }) + console.log('删除结果', key, res) + await client.close() + return null +} + +export default { getCache, setCache, delCache } diff --git a/package.json b/package.json index ab5eb052..22ab8bc5 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "localStorage": "^1.0.4", "lodash.throttle": "^4.1.1", "memory-cache": "^0.2.0", + "mongodb": "^4.6.0", "next": "^12.0.5", "notion-client": "6.12.9", "notion-utils": "6.10.0", From efdb2d084e49b3bf8d11eae724b4602dbad484b8 Mon Sep 17 00:00:00 2001 From: tlyong1992 Date: Mon, 6 Jun 2022 17:23:25 +0800 Subject: [PATCH 3/8] =?UTF-8?q?NotionPage=20image=20Link=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/NotionPage.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/components/NotionPage.js b/components/NotionPage.js index 61b7fc56..3fb9ae23 100644 --- a/components/NotionPage.js +++ b/components/NotionPage.js @@ -3,6 +3,8 @@ import dynamic from 'next/dynamic' import mediumZoom from 'medium-zoom' import React from 'react' import { isBrowser } from '@/lib/utils' +import Image from 'next/image' +import Link from 'next/link' const Code = dynamic(() => import('react-notion-x/build/third-party/code').then((m) => m.Code), { ssr: false } @@ -75,7 +77,9 @@ const NotionPage = ({ post }) => { Collection, Equation, Modal, - Pdf + Pdf, + nextImage: Image, + nextLink: Link }} /> } From a741007ddc6ba243e04cb8167b07bbb7b54a7fce Mon Sep 17 00:00:00 2001 From: tlyong1992 Date: Mon, 6 Jun 2022 17:24:26 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E5=9B=BE=E7=89=87=E7=BC=A9=E6=94=BEbug?= =?UTF-8?q?=E3=80=81=E5=9B=BE=E7=89=87=E7=BC=A9=E7=95=A5=E3=80=81=E7=9B=B8?= =?UTF-8?q?=E5=86=8Clink=E6=A0=B7=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- styles/globals.css | 3 ++- styles/notion.css | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/styles/globals.css b/styles/globals.css index 8d4459e1..af6c3ac6 100644 --- a/styles/globals.css +++ b/styles/globals.css @@ -137,6 +137,7 @@ nav { .medium-zoom-overlay{ background: none !important; + /* background: rgba(0, 0, 0, 0.01) none repeat scroll 0% 0% !important; */ } .shadow-text{ @@ -156,7 +157,7 @@ nav { } .medium-zoom-image--opened{ - width: auto !important; + /* width: auto !important; */ } [data-waline] p { diff --git a/styles/notion.css b/styles/notion.css index 76807da7..200d14a8 100644 --- a/styles/notion.css +++ b/styles/notion.css @@ -537,7 +537,7 @@ color: inherit; word-break: break-word; text-decoration: inherit; - border-bottom: 0.05em solid; + border-bottom: .05em solid !important; border-color: var(--fg-color-2); opacity: 0.7; transition: border-color 100ms ease-in, opacity 100ms ease-in; @@ -673,7 +673,7 @@ svg.notion-page-icon { .notion-asset-wrapper img { width: 100%; - height: 100%; + /* height: 100%; */ max-height: 100%; } From 3f2551285deb13acf7cf5fa79873d22084ad7f0e Mon Sep 17 00:00:00 2001 From: tlyong1992 Date: Tue, 7 Jun 2022 13:17:06 +0800 Subject: [PATCH 5/8] =?UTF-8?q?mongo=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/cache/cache_manager.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/cache/cache_manager.js b/lib/cache/cache_manager.js index ec921177..2943b233 100644 --- a/lib/cache/cache_manager.js +++ b/lib/cache/cache_manager.js @@ -2,10 +2,9 @@ import MemoryCache from './memory_cache' import FileCache from './local_file_cache' import MongoCache from './mongo_db_cache' const enableCache = true -const enableMongoCache = true let api -if (enableMongoCache) { +if (process.env.MONGO_DB_URL && process.env.MONGO_DB_NAME) { api = MongoCache } else if (process.env.ENABLE_FILE_CACHE) { api = FileCache From a6948117f846352866ef9899df2e167e84d1d1da Mon Sep 17 00:00:00 2001 From: tlyong1992 Date: Tue, 7 Jun 2022 14:50:46 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=97=A5=E6=9C=9F?= =?UTF-8?q?=E4=B8=BA=E7=A9=BA=E6=97=B6=E7=BC=96=E8=AF=91=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- themes/example/LayoutArchive.js | 8 ++++---- themes/fukasawa/LayoutArchive.js | 6 +++--- themes/fukasawa/components/BlogPostArchive.js | 2 +- themes/hexo/LayoutArchive.js | 6 +++--- themes/hexo/components/BlogPostArchive.js | 2 +- themes/hexo/components/BlogPostCard.js | 2 +- themes/medium/LayoutArchive.js | 8 ++++---- themes/medium/components/BlogPostCard.js | 2 +- themes/next/LayoutArchive.js | 6 +++--- themes/next/components/BlogPostArchive.js | 2 +- themes/next/components/BlogPostCard.js | 2 +- 11 files changed, 23 insertions(+), 23 deletions(-) diff --git a/themes/example/LayoutArchive.js b/themes/example/LayoutArchive.js index 1b3d1e34..a8bc68ee 100644 --- a/themes/example/LayoutArchive.js +++ b/themes/example/LayoutArchive.js @@ -7,15 +7,15 @@ export const LayoutArchive = props => { const postsSortByDate = Object.create(posts) postsSortByDate.sort((a, b) => { - const dateA = new Date(a?.date.start_date || a.createdTime) - const dateB = new Date(b?.date.start_date || b.createdTime) + const dateA = new Date(a?.date?.start_date || a.createdTime) + const dateB = new Date(b?.date?.start_date || b.createdTime) return dateB - dateA }) const archivePosts = {} postsSortByDate.forEach(post => { - const date = post.date.start_date.slice(0, 7) + const date = post.date?.start_date.slice(0, 7) if (archivePosts[date]) { archivePosts[date].push(post) } else { @@ -39,7 +39,7 @@ export const LayoutArchive = props => { >
- {post.date.start_date} + {post.date?.start_date} {' '}   { // 时间排序 postsSortByDate.sort((a, b) => { - const dateA = new Date(a?.date.start_date || a.createdTime) - const dateB = new Date(b?.date.start_date || b.createdTime) + const dateA = new Date(a?.date?.start_date || a.createdTime) + const dateB = new Date(b?.date?.start_date || b.createdTime) return dateB - dateA }) const archivePosts = {} postsSortByDate.forEach(post => { - const date = post.date.start_date.slice(0, 7) + const date = post.date?.start_date.slice(0, 7) if (archivePosts[date]) { archivePosts[date].push(post) } else { diff --git a/themes/fukasawa/components/BlogPostArchive.js b/themes/fukasawa/components/BlogPostArchive.js index 90bc400a..a5f9e286 100644 --- a/themes/fukasawa/components/BlogPostArchive.js +++ b/themes/fukasawa/components/BlogPostArchive.js @@ -27,7 +27,7 @@ const BlogArchiveItem = ({ posts = [], archiveTitle }) => { className="border-l-2 p-1 text-xs md:text-base items-center hover:scale-x-105 hover:border-gray-500 dark:hover:border-gray-300 dark:border-gray-400 transform duration-500" >
- {post.date.start_date}{' '} + {post.date?.start_date}{' '}   diff --git a/themes/hexo/LayoutArchive.js b/themes/hexo/LayoutArchive.js index ed0e97fe..5f30f76e 100644 --- a/themes/hexo/LayoutArchive.js +++ b/themes/hexo/LayoutArchive.js @@ -10,15 +10,15 @@ export const LayoutArchive = (props) => { // 时间排序 postsSortByDate.sort((a, b) => { - const dateA = new Date(a?.date.start_date || a.createdTime) - const dateB = new Date(b?.date.start_date || b.createdTime) + const dateA = new Date(a?.date?.start_date || a.createdTime) + const dateB = new Date(b?.date?.start_date || b.createdTime) return dateB - dateA }) const archivePosts = {} postsSortByDate.forEach(post => { - const date = post.date.start_date.slice(0, 7) + const date = post.date?.start_date.slice(0, 7) if (archivePosts[date]) { archivePosts[date].push(post) } else { diff --git a/themes/hexo/components/BlogPostArchive.js b/themes/hexo/components/BlogPostArchive.js index f7d06014..b7e55b3e 100644 --- a/themes/hexo/components/BlogPostArchive.js +++ b/themes/hexo/components/BlogPostArchive.js @@ -27,7 +27,7 @@ const BlogPostArchive = ({ posts = [], archiveTitle }) => { className="border-l-2 p-1 text-xs md:text-base items-center hover:scale-x-105 hover:border-indigo-500 dark:hover:border-indigo-300 dark:border-indigo-400 transform duration-500" > diff --git a/themes/medium/LayoutArchive.js b/themes/medium/LayoutArchive.js index 9edb9c24..487734ba 100644 --- a/themes/medium/LayoutArchive.js +++ b/themes/medium/LayoutArchive.js @@ -7,15 +7,15 @@ export const LayoutArchive = props => { const postsSortByDate = Object.create(posts) postsSortByDate.sort((a, b) => { - const dateA = new Date(a?.date.start_date || a.createdTime) - const dateB = new Date(b?.date.start_date || b.createdTime) + const dateA = new Date(a?.date?.start_date || a.createdTime) + const dateB = new Date(b?.date?.start_date || b.createdTime) return dateB - dateA }) const archivePosts = {} postsSortByDate.forEach(post => { - const date = post.date.start_date.slice(0, 7) + const date = post.date?.start_date.slice(0, 7) if (archivePosts[date]) { archivePosts[date].push(post) } else { @@ -41,7 +41,7 @@ export const LayoutArchive = props => { >
- {post.date.start_date} + {post.date?.start_date} {' '}   { 'flex mt-2 items-center justify-start flex-wrap space-x-3 text-gray-400' } > -
{post.date.start_date}
+
{post.date?.start_date}
{CONFIG_MEDIUM.POST_LIST_CATEGORY && ( )} diff --git a/themes/next/LayoutArchive.js b/themes/next/LayoutArchive.js index 1b322fd0..ce66e25f 100644 --- a/themes/next/LayoutArchive.js +++ b/themes/next/LayoutArchive.js @@ -9,15 +9,15 @@ export const LayoutArchive = (props) => { // 时间排序 postsSortByDate.sort((a, b) => { - const dateA = new Date(a?.date.start_date || a.createdTime) - const dateB = new Date(b?.date.start_date || b.createdTime) + const dateA = new Date(a?.date?.start_date || a.createdTime) + const dateB = new Date(b?.date?.start_date || b.createdTime) return dateB - dateA }) const archivePosts = {} postsSortByDate.forEach(post => { - const date = post.date.start_date.slice(0, 7) + const date = post.date?.start_date.slice(0, 7) if (archivePosts[date]) { archivePosts[date].push(post) } else { diff --git a/themes/next/components/BlogPostArchive.js b/themes/next/components/BlogPostArchive.js index 0ef8ee86..d2d6f857 100644 --- a/themes/next/components/BlogPostArchive.js +++ b/themes/next/components/BlogPostArchive.js @@ -27,7 +27,7 @@ const BlogPostArchive = ({ posts = [], archiveTitle }) => { className="border-l-2 p-1 text-xs md:text-base items-center hover:scale-x-105 hover:border-gray-500 dark:hover:border-gray-300 dark:border-gray-400 transform duration-500" > From b400246059cf195c602a1eb2f4f207196a170b44 Mon Sep 17 00:00:00 2001 From: tlyong1992 Date: Tue, 7 Jun 2022 16:25:38 +0800 Subject: [PATCH 7/8] =?UTF-8?q?BlogCard=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- themes/hexo/components/BlogPostCard.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/themes/hexo/components/BlogPostCard.js b/themes/hexo/components/BlogPostCard.js index 22dbe428..95bfcfbf 100644 --- a/themes/hexo/components/BlogPostCard.js +++ b/themes/hexo/components/BlogPostCard.js @@ -33,7 +33,7 @@ const BlogPostCard = ({ post, showSummary }) => { > - {post.date?.start_date} + {post.date?.start_date || post.lastEditedTime}
From b101944732b678c82d6231b5766115fb768032a5 Mon Sep 17 00:00:00 2001 From: tlyong1992 Date: Wed, 8 Jun 2022 09:48:00 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E7=85=A7=E7=89=87=E9=9B=86=E7=82=B9?= =?UTF-8?q?=E5=87=BB=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/NotionPage.js | 10 +++++++++- styles/notion.css | 5 +++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/components/NotionPage.js b/components/NotionPage.js index 13cbe2b7..ead0d0ba 100644 --- a/components/NotionPage.js +++ b/components/NotionPage.js @@ -61,12 +61,20 @@ const NotionPage = ({ post }) => { const cards = document.getElementsByClassName('notion-collection-card') for (const e of cards) { e.removeAttribute('href') + const links = e.querySelectorAll('.notion-link') + if (links && links.length > 0) { + for (const l of links) { + l.onclick = function() { + window.open('http://' + l.innerText) + } + } + } } } }, 800) addWatch4Dom() - }, []) + }) return