wgt-node-utils 1.2.70 → 1.2.71

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wgt-node-utils",
3
- "version": "1.2.70",
3
+ "version": "1.2.71",
4
4
  "description": "WGT工具类包",
5
5
  "main": "dist/bundle.js",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -882,7 +882,7 @@ class wgtNodeUtils {
882
882
  seoFun = ({ url, name, image, author, aggregateRating, description, hostname, languageList, lang, title, type, applicationCategory, genre, time, logo }) => {
883
883
  const pathWithoutLang = url ? url.replace(/(https?:\/\/[^/]+)\/en\b/, "$1") : '/';
884
884
  const seoRenderLdJson = this.renderLdJson({ pathWithoutLang, name, image, author, aggregateRating, description, hostname, type, languageList, applicationCategory, genre, time, logo });
885
- const seoRenderAlternateLink = this.renderAlternateLink({ languageList, url, pathWithoutLang });
885
+ const seoRenderAlternateLink = this.renderAlternateLink({ languageList, url, lang });
886
886
  const seoraphAndTwitterFun = this.graphAndTwitterFun({ image, url, title, hostname, description, lang, pathWithoutLang });
887
887
  const seoContent = {
888
888
  seoRenderLdJson,
@@ -987,20 +987,42 @@ class wgtNodeUtils {
987
987
  * @param {Array} languageList - 语言列表
988
988
  * @param {string} url - 页面URL
989
989
  */
990
- renderAlternateLink = ({ languageList, url, pathWithoutLang, lang }) => {
991
- const languageLinkList = languageList.map(item => ({
992
- rel: 'alternate',
993
- hrefLang: item.code === 'en' ? 'en' : item.code,
994
- href: item.code === 'en' ? pathWithoutLang : url.replace(/(https?:\/\/[^/]+)\/en(?=\/|$)/, `$1/${item.code}`)
995
- }));
996
- languageLinkList.push({ rel: 'alternate', hrefLang: 'x-default', href: pathWithoutLang });
997
- // canonical 指向当前语言的实际 URL,非英语语言需替换为对应语言路径
998
- const canonicalUrl = (lang && lang !== 'en')
999
- ? url.replace(/(https?:\/\/[^/]+)\/en(?=\/|$)/, `$1/${lang}`)
1000
- : pathWithoutLang;
990
+ renderAlternateLink = ({ languageList, url, lang }) => {
991
+ // 提取所有语言 code
992
+ const languageCodes = languageList.map(item => item.code).join('|');
993
+ const langRegex = new RegExp(`^/(${languageCodes})(?=/|$)`);
994
+
995
+ // 提取域名和路径
996
+ const originMatch = url.match(/^(https?:\/\/[^/]+)(.*)$/);
997
+ const origin = originMatch ? originMatch[1] : '';
998
+ const pathname = originMatch ? originMatch[2] : '/';
999
+
1000
+ // 去掉已有语言前缀
1001
+ const cleanPath = pathname.replace(langRegex, '') || '/';
1002
+
1003
+ // 构建不同语言的链接
1004
+ const languageLinkList = languageList.map(item => {
1005
+ const href = item.code === 'en'
1006
+ ? `${origin}${cleanPath}` // 英文不加前缀
1007
+ : `${origin}/${item.code}${cleanPath === '/' ? '' : cleanPath}`; // 其他语言加前缀
1008
+ return {
1009
+ rel: 'alternate',
1010
+ hrefLang: item.code,
1011
+ href
1012
+ };
1013
+ });
1014
+
1015
+ // x-default 指向英文(默认路径)
1016
+ languageLinkList.push({ rel: 'alternate', hrefLang: 'x-default', href: `${origin}${cleanPath}` });
1017
+
1018
+ // canonical 指向当前语言的 URL
1019
+ const canonicalUrl = lang && lang !== 'en'
1020
+ ? `${origin}/${lang}${cleanPath === '/' ? '' : cleanPath}`
1021
+ : `${origin}${cleanPath}`;
1001
1022
  languageLinkList.push({ rel: 'canonical', href: canonicalUrl });
1002
- return languageLinkList
1003
- }
1023
+
1024
+ return languageLinkList;
1025
+ };
1004
1026
  // Open Graph 和 Twitter Card 站外引流触点
1005
1027
  graphAndTwitterFun = ({ image, title, hostname, description, lang, pathWithoutLang }) => {
1006
1028