Skip to content

Commit c5c88a0

Browse files
authored
Merge pull request #3326 from tangly1024/fix/iconfont
Fix/iconfont
2 parents f3278fa + 37b6c5b commit c5c88a0

File tree

6 files changed

+43
-63
lines changed

6 files changed

+43
-63
lines changed

components/ExternalPlugins.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import { initGoogleAdsense } from './GoogleAdsense'
1010
import Head from 'next/head'
1111
import ExternalScript from './ExternalScript'
1212
import WebWhiz from './Webwhiz'
13+
import { useGlobal } from '@/lib/global'
1314
import IconFont from './IconFont'
1415

16+
1517
/**
1618
* 各种插件脚本
1719
* @param {*} props
@@ -20,6 +22,7 @@ import IconFont from './IconFont'
2022
const ExternalPlugin = props => {
2123
// 读取自Notion的配置
2224
const { NOTION_CONFIG } = props
25+
const {lang} = useGlobal()
2326
const DISABLE_PLUGIN = siteConfig('DISABLE_PLUGIN', null, NOTION_CONFIG)
2427
const THEME_SWITCH = siteConfig('THEME_SWITCH', null, NOTION_CONFIG)
2528
const DEBUG = siteConfig('DEBUG', null, NOTION_CONFIG)
@@ -168,8 +171,8 @@ const ExternalPlugin = props => {
168171
}
169172

170173
setTimeout(() => {
171-
// 将notion-id格式的url转成自定义slug
172-
convertInnerUrl(props?.allNavPages)
174+
// 映射url
175+
convertInnerUrl({ allPages:props?.allNavPages, lang:lang })
173176
}, 500)
174177
}, [router])
175178

components/IconFont.js

+24-22
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,34 @@ export default function IconFont() {
1010
const router = useRouter()
1111

1212
useEffect(() => {
13-
loadExternalResource('/webfonts/iconfont.js').then(u => {
14-
console.log('iconfont loaded')
13+
loadExternalResource('/webfonts/iconfont.js')
14+
.then(u => {
15+
console.log('iconfont loaded');
1516

16-
// 查找所有 <i> 标签且 class 包含 'icon-'
17-
const iElements = document.querySelectorAll('i[class*="icon-"]');
18-
iElements.forEach(element => {
19-
const className = Array.from(element.classList).find(cls => cls.startsWith('icon-'));
20-
if (className) {
21-
// 创建新的 <svg> 元素
22-
const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
23-
svgElement.setAttribute('class', 'icon');
24-
svgElement.setAttribute('aria-hidden', 'true');
17+
// 查找所有 <i> 标签且 class 包含 'icon-'
18+
const iElements = document.querySelectorAll('i[class*="icon-"]');
19+
iElements.forEach(element => {
20+
const className = Array.from(element.classList).find(cls => cls.startsWith('icon-'));
21+
if (className) {
22+
// 创建新的 <svg> 元素
23+
const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
24+
svgElement.setAttribute('class', 'icon');
25+
svgElement.setAttribute('aria-hidden', 'true');
2526

26-
const useElement = document.createElementNS('http://www.w3.org/2000/svg', 'use');
27-
useElement.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', `#${className}`);
28-
svgElement.appendChild(useElement);
27+
const useElement = document.createElementNS('http://www.w3.org/2000/svg', 'use');
28+
useElement.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', `#${className}`);
29+
svgElement.appendChild(useElement);
2930

30-
// 替换原来的 <i> 元素
31-
element.replaceWith(svgElement);
32-
console.log(`Replaced <i> with class "${className}" to <svg>`);
33-
}
31+
// 替换原来的 <i> 元素
32+
element.replaceWith(svgElement);
33+
console.log(`Replaced <i> with class "${className}" to <svg>`);
34+
}
35+
});
36+
})
37+
.catch(error => {
38+
console.warn('Failed to load iconfont.js:', error);
3439
});
35-
})
36-
}, [router])
40+
}, [router]);
3741

3842
return <style jsx global>
3943
{`
@@ -43,12 +47,10 @@ export default function IconFont() {
4347
vertical-align: -0.15em;
4448
fill: currentColor;
4549
overflow: hidden;
46-
4750
}
4851
4952
svg.icon {
5053
display: inline;
5154
}
52-
5355
`}</style>
5456
}

lib/global.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ import {
88
import { useUser } from '@clerk/nextjs'
99
import { useRouter } from 'next/router'
1010
import { createContext, useContext, useEffect, useState } from 'react'
11-
import {
12-
generateLocaleDict,
13-
initLocale,
14-
redirectUserLang,
15-
saveLangToLocalStorage
16-
} from './lang'
11+
import { generateLocaleDict, initLocale, redirectUserLang } from './lang'
1712

1813
/**
1914
* 全局上下文
@@ -81,7 +76,6 @@ export function GlobalContextProvider(props) {
8176

8277
function changeLang(lang) {
8378
if (lang) {
84-
saveLangToLocalStorage(lang)
8579
updateLang(lang)
8680
updateLocale(generateLocaleDict(lang))
8781
}

lib/lang.js

+3-24
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import zhCN from './lang/zh-CN'
88
import zhHK from './lang/zh-HK'
99
import zhTW from './lang/zh-TW'
1010
import { extractLangPrefix } from './utils/pageId'
11+
import { useRouter } from 'next/router'
1112

1213
/**
1314
* 在这里配置所有支持的语言
@@ -70,15 +71,9 @@ export function generateLocaleDict(langString) {
7071
*/
7172
export function initLocale(locale, changeLang, updateLocale) {
7273
if (isBrowser) {
73-
// 根据router中的locale对象判断当前语言:表现为前缀中包含 zh、en 等。
74-
let pathLocaleLang = null
75-
if (locale === 'en' || locale === 'zh') {
76-
pathLocaleLang = locale === 'en' ? 'en-US' : 'zh-CN'
77-
}
7874
// 如果有query参数切换语言则优先
7975
const queryLang =
80-
getQueryVariable('locale') || getQueryVariable('lang') || pathLocaleLang
81-
76+
getQueryVariable('locale') || getQueryVariable('lang') || locale
8277
if (queryLang) {
8378
const match = queryLang.match(/[a-zA-Z]{2}(?:-[a-zA-Z]{2})?/)
8479
if (match) {
@@ -91,22 +86,6 @@ export function initLocale(locale, changeLang, updateLocale) {
9186
}
9287
}
9388

94-
/**
95-
* 读取语言
96-
* @returns {*}
97-
*/
98-
export const loadLangFromLocalStorage = () => {
99-
return localStorage.getItem('lang')
100-
}
101-
102-
/**
103-
* 保存语言
104-
* @param newTheme
105-
*/
106-
export const saveLangToLocalStorage = lang => {
107-
localStorage.setItem('lang', lang)
108-
}
109-
11089
/**
11190
* 检测用户的预研偏好,跳转至对应的多语言网站
11291
* @param {*} lang
@@ -142,4 +121,4 @@ export const redirectUserLang = (lang, pageId) => {
142121
}
143122
}
144123
}
145-
}
124+
}

lib/notion/convertInnerUrl.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { loadLangFromLocalStorage } from '@/lib/lang'
21
import { idToUuid } from 'notion-utils'
32
import { checkStrIsNotionId, getLastPartOfUrl, isBrowser } from '../utils'
43

@@ -7,7 +6,7 @@ import { checkStrIsNotionId, getLastPartOfUrl, isBrowser } from '../utils'
76
* 1.若是本站域名,则在当前窗口打开、不开新窗口
87
* 2.url是notion-id,转成站内文章链接
98
*/
10-
export const convertInnerUrl = allPages => {
9+
export const convertInnerUrl = ({ allPages, lang }) => {
1110
if (!isBrowser) {
1211
return
1312
}
@@ -18,11 +17,9 @@ export const convertInnerUrl = allPages => {
1817
if (!allAnchorTags) {
1918
return
2019
}
21-
2220
const { origin, pathname } = window.location
2321
const currentURL = origin + pathname
2422
const currentPathLang = pathname.split('/').filter(Boolean)[0]
25-
const lang = loadLangFromLocalStorage().split(/[-_]/)[0]
2623
const langPrefix = lang === currentPathLang ? '/' + lang : ''
2724
for (const anchorTag of allAnchorTags) {
2825
// url替换成slug
@@ -56,6 +53,7 @@ export const convertInnerUrl = allPages => {
5653
anchorTag.target = '_blank'
5754
}
5855
}
56+
5957
for (const anchorTag of allAnchorTags) {
6058
const slug = getLastPartOfUrl(anchorTag.href)
6159
const slugPage = allPages?.find(page => {

next.config.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const themes = scanSubdirectories(path.resolve(__dirname, 'themes'))
1515
const locales = (function () {
1616
// 根据BLOG_NOTION_PAGE_ID 检查支持多少种语言数据.
1717
// 支持如下格式配置多个语言的页面id xxx,zh:xxx,en:xxx
18-
const langs = [BLOG.LANG.slice(0, 2)]
18+
const langs = [BLOG.LANG]
1919
if (BLOG.NOTION_PAGE_ID.indexOf(',') > 0) {
2020
const siteIds = BLOG.NOTION_PAGE_ID.split(',')
2121
for (let index = 0; index < siteIds.length; index++) {
@@ -84,15 +84,19 @@ const nextConfig = {
8484
eslint: {
8585
ignoreDuringBuilds: true
8686
},
87-
output: process.env.EXPORT ? 'export' : process.env.NEXT_BUILD_STANDALONE === 'true' ? 'standalone' : undefined,
87+
output: process.env.EXPORT
88+
? 'export'
89+
: process.env.NEXT_BUILD_STANDALONE === 'true'
90+
? 'standalone'
91+
: undefined,
8892
staticPageGenerationTimeout: 120,
8993
// 多语言, 在export时禁用
9094
i18n: process.env.EXPORT
9195
? undefined
9296
: {
93-
defaultLocale: BLOG.LANG.slice(0, 2),
97+
defaultLocale: BLOG.LANG,
9498
// 支持的所有多语言,按需填写即可
95-
locales
99+
locales: locales
96100
},
97101
images: {
98102
// 图片压缩

0 commit comments

Comments
 (0)