From f62a06b003d3d858ac149bc15ed3df297dbcc275 Mon Sep 17 00:00:00 2001 From: Mister-Hope Date: Sat, 21 Dec 2024 01:02:52 +0800 Subject: [PATCH] chore: prefer using siteData (#316) --- .../blog/plugin-feed/src/node/addFeedLinks.ts | 23 +++++++------------ .../blog/plugin-feed/src/node/feed/item.ts | 2 +- .../src/node/getFeedChannelOptions.ts | 3 +-- .../plugin-feed/src/node/getFeedFilenames.ts | 2 +- .../blog/plugin-feed/src/node/getFeedLinks.ts | 2 +- .../pwa/plugin-pwa/src/node/getManifest.ts | 19 ++++++--------- .../plugin-pwa/src/node/injectLinksToHead.ts | 8 +++---- plugins/pwa/plugin-pwa/src/node/pwaPlugin.ts | 5 ++-- .../tests/node/injectLinksToHead.spec.ts | 11 ++++++--- .../src/node/docsearchPlugin.ts | 2 +- .../src/node/generateDescription.ts | 2 +- plugins/seo/plugin-seo/src/node/getOGPInfo.ts | 19 ++++----------- .../seo/plugin-seo/src/node/utils/getLinks.ts | 2 +- .../seo/plugin-sitemap/src/node/getInfo.ts | 2 +- .../src/node/googleTagManagerPlugin.ts | 2 +- .../tools/plugin-redirect/src/cli/index.ts | 2 +- .../src/node/getRedirectLocaleConfig.ts | 2 +- .../src/node/handleRedirectTo.ts | 3 +-- .../src/node/bundler/customizeDevServer.ts | 2 +- tools/helper/src/node/locales/helpers.ts | 4 ++-- 20 files changed, 49 insertions(+), 68 deletions(-) diff --git a/plugins/blog/plugin-feed/src/node/addFeedLinks.ts b/plugins/blog/plugin-feed/src/node/addFeedLinks.ts index 9c3f7d7b37..8f4ceb7db4 100644 --- a/plugins/blog/plugin-feed/src/node/addFeedLinks.ts +++ b/plugins/blog/plugin-feed/src/node/addFeedLinks.ts @@ -9,8 +9,7 @@ export const addFeedLinks = ( app: App, options: ResolvedFeedOptionsMap, ): void => { - const { base } = app.options - const { siteData } = app + const { base, title, locales, head } = app.siteData const localePaths = keys(options) // there is only one language, so we append it to siteData @@ -31,28 +30,22 @@ export const addFeedLinks = ( href: getUrl(hostname, base, fileName), title: `${ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition - siteData.title || siteData.locales['/']?.title || '' + title || locales['/']?.title || '' } ${name} Feed`, }, ] // add atom link if (atom) - siteData.head.push( - getHeadItem('Atom', atomOutputFilename, 'application/atom+xml'), - ) + head.push(getHeadItem('Atom', atomOutputFilename, 'application/atom+xml')) // add json link if (json) - siteData.head.push( - getHeadItem('JSON', jsonOutputFilename, 'application/json'), - ) + head.push(getHeadItem('JSON', jsonOutputFilename, 'application/json')) // add rss link if (rss) - siteData.head.push( - getHeadItem('RSS', rssOutputFilename, 'application/rss+xml'), - ) + head.push(getHeadItem('RSS', rssOutputFilename, 'application/rss+xml')) } // there are multiple languages, so we should append to page else { @@ -76,10 +69,10 @@ export const addFeedLinks = ( href: getUrl(localeOptions.hostname, base, fileName), title: `${ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition - siteData.locales[pathLocale]?.title || - siteData.title || + locales[pathLocale]?.title || + title || // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition - siteData.locales['/']?.title || + locales['/']?.title || '' } ${name} Feed`, }, diff --git a/plugins/blog/plugin-feed/src/node/feed/item.ts b/plugins/blog/plugin-feed/src/node/feed/item.ts index 1102c2c5b6..e54d581564 100644 --- a/plugins/blog/plugin-feed/src/node/feed/item.ts +++ b/plugins/blog/plugin-feed/src/node/feed/item.ts @@ -41,7 +41,7 @@ export class FeedItem { private readonly page: FeedPage, private readonly hostname: string, ) { - this.base = this.app.options.base + this.base = this.app.siteData.base this.frontmatter = page.frontmatter this.getter = options.getter ?? {} // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing diff --git a/plugins/blog/plugin-feed/src/node/getFeedChannelOptions.ts b/plugins/blog/plugin-feed/src/node/getFeedChannelOptions.ts index e1a70359e7..ed9e74cc4f 100644 --- a/plugins/blog/plugin-feed/src/node/getFeedChannelOptions.ts +++ b/plugins/blog/plugin-feed/src/node/getFeedChannelOptions.ts @@ -9,8 +9,7 @@ export const getFeedChannelOptions = ( options: FeedPluginOptions, localePath = '', ): FeedChannelOptions => { - const { base } = app.options - const { title, description, lang, locales } = app.siteData + const { base, title, description, lang, locales } = app.siteData const { channel: { icon: channelIcon, image: channelImage, ...channel } = {}, hostname, diff --git a/plugins/blog/plugin-feed/src/node/getFeedFilenames.ts b/plugins/blog/plugin-feed/src/node/getFeedFilenames.ts index 050207310f..bbfcf5d9f4 100644 --- a/plugins/blog/plugin-feed/src/node/getFeedFilenames.ts +++ b/plugins/blog/plugin-feed/src/node/getFeedFilenames.ts @@ -49,7 +49,7 @@ export const getFeedLinks = ( options: ResolvedFeedOptions, localePath: string, ): FeedLinks => { - const { base } = app.options + const { base } = app.siteData const { hostname } = options const { atomOutputFilename, diff --git a/plugins/blog/plugin-feed/src/node/getFeedLinks.ts b/plugins/blog/plugin-feed/src/node/getFeedLinks.ts index 6415f8b139..aeb1e82b61 100644 --- a/plugins/blog/plugin-feed/src/node/getFeedLinks.ts +++ b/plugins/blog/plugin-feed/src/node/getFeedLinks.ts @@ -17,7 +17,7 @@ export const getFeedLinks = ( options: ResolvedFeedOptions, localePath: string, ): FeedLinks => { - const { base } = app.options + const { base } = app.siteData const { hostname } = options const { atomOutputFilename, diff --git a/plugins/pwa/plugin-pwa/src/node/getManifest.ts b/plugins/pwa/plugin-pwa/src/node/getManifest.ts index 8379dbcdde..50178f8d06 100644 --- a/plugins/pwa/plugin-pwa/src/node/getManifest.ts +++ b/plugins/pwa/plugin-pwa/src/node/getManifest.ts @@ -7,8 +7,9 @@ export const getManifest = async ( app: App, options: PwaPluginOptions, ): Promise => { - const { siteData } = app - const { base } = app.options + const { base, title, description, lang, locales } = app.siteData + + const rootLocale = locales['/'] ?? {} const userManifestPath = app.dir.source( '.vuepress/public/manifest.webmanifest', @@ -24,17 +25,11 @@ export const getManifest = async ( ) as AppManifest const manifest: AppManifest = { - // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition - name: siteData.title || siteData.locales['/']?.title || 'Site', - // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition - short_name: siteData.title || siteData.locales['/']?.title || 'Site', + name: title || rootLocale.title || 'Site', + short_name: title || rootLocale.title || 'Site', description: - siteData.description || - // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition - siteData.locales['/']?.description || - 'A site built with vuepress', - // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition - lang: app.siteData.locales['/']?.lang ?? app.siteData.lang, + description || rootLocale.description || 'A site built with vuepress', + lang: rootLocale.lang ?? lang, start_url: base, scope: base, diff --git a/plugins/pwa/plugin-pwa/src/node/injectLinksToHead.ts b/plugins/pwa/plugin-pwa/src/node/injectLinksToHead.ts index 50172b8f4c..2b6bf80f56 100644 --- a/plugins/pwa/plugin-pwa/src/node/injectLinksToHead.ts +++ b/plugins/pwa/plugin-pwa/src/node/injectLinksToHead.ts @@ -1,12 +1,12 @@ -import type { App, HeadConfig } from 'vuepress/core' +import type { App } from 'vuepress/core' import { isPlainObject } from 'vuepress/shared' import type { PwaPluginOptions } from './options.js' export const injectLinksToHead = ( app: App, { favicon, manifest, themeColor = '#46bd87', apple }: PwaPluginOptions, -): HeadConfig[] => { - const { base, head } = app.options +): void => { + const { base, head } = app.siteData const metaKeys: string[] = [] const linkKeys: string[] = [] @@ -75,6 +75,4 @@ export const injectLinksToHead = ( } else if (apple !== false && fallBackIcon) { setLink('apple-touch-icon', fallBackIcon) } - - return head } diff --git a/plugins/pwa/plugin-pwa/src/node/pwaPlugin.ts b/plugins/pwa/plugin-pwa/src/node/pwaPlugin.ts index b5da64f9a4..7ec3623613 100644 --- a/plugins/pwa/plugin-pwa/src/node/pwaPlugin.ts +++ b/plugins/pwa/plugin-pwa/src/node/pwaPlugin.ts @@ -18,7 +18,8 @@ export const pwaPlugin = (app) => { if (app.env.isDebug) logger.info('Options:', options) - const { base, shouldPrefetch = true } = app.options + const { shouldPrefetch = true } = app.options + const { base } = app.siteData if (options.appendBase) appendBase(base, options) @@ -27,7 +28,7 @@ export const pwaPlugin = 'The plugin will register service worker to handle assets, so we recommend you to set "shouldPrefetch: false" in VuePress config file.', ) - app.options.head = injectLinksToHead(app, options) + injectLinksToHead(app, options) return { name: PLUGIN_NAME, diff --git a/plugins/pwa/plugin-pwa/tests/node/injectLinksToHead.spec.ts b/plugins/pwa/plugin-pwa/tests/node/injectLinksToHead.spec.ts index e7bbf25e46..37e7d2e5b2 100644 --- a/plugins/pwa/plugin-pwa/tests/node/injectLinksToHead.spec.ts +++ b/plugins/pwa/plugin-pwa/tests/node/injectLinksToHead.spec.ts @@ -64,7 +64,9 @@ describe('Test head function', () => { }, }) - expect(injectLinksToHead(app, options)).toEqual([ + injectLinksToHead(app, options) + + expect(app.siteData.head).toEqual([ [ 'meta', { @@ -176,7 +178,9 @@ describe('Test head function', () => { }, } - expect(injectLinksToHead(app, optionsWithManifest)).toEqual([ + injectLinksToHead(app, optionsWithManifest) + + expect(app.siteData.head).toEqual([ [ 'meta', { @@ -240,7 +244,8 @@ describe('Test head function', () => { }, }) - expect(injectLinksToHead(app, options)).toEqual([ + injectLinksToHead(app, options) + expect(app.siteData.head).toEqual([ [ 'link', { diff --git a/plugins/search/plugin-docsearch/src/node/docsearchPlugin.ts b/plugins/search/plugin-docsearch/src/node/docsearchPlugin.ts index 01827f9501..7f7d84c206 100644 --- a/plugins/search/plugin-docsearch/src/node/docsearchPlugin.ts +++ b/plugins/search/plugin-docsearch/src/node/docsearchPlugin.ts @@ -26,7 +26,7 @@ export const docsearchPlugin = ({ name: PLUGIN_NAME, define: (app) => ({ - __DOCSEARCH_INDEX_BASE__: indexBase || app.options.base, + __DOCSEARCH_INDEX_BASE__: indexBase || app.siteData.base, __DOCSEARCH_OPTIONS__: options, }), diff --git a/plugins/seo/plugin-seo/src/node/generateDescription.ts b/plugins/seo/plugin-seo/src/node/generateDescription.ts index 7249fccf5a..73c1adf1fb 100644 --- a/plugins/seo/plugin-seo/src/node/generateDescription.ts +++ b/plugins/seo/plugin-seo/src/node/generateDescription.ts @@ -13,7 +13,7 @@ export const generateDescription = ( ? (page.data.excerpt ?? page.contentRendered) : page.contentRendered - const pageText = getText(content, app.options.base, { + const pageText = getText(content, app.siteData.base, { length: 180, singleLine: true, }) diff --git a/plugins/seo/plugin-seo/src/node/getOGPInfo.ts b/plugins/seo/plugin-seo/src/node/getOGPInfo.ts index 21fef202d5..94bafceb98 100644 --- a/plugins/seo/plugin-seo/src/node/getOGPInfo.ts +++ b/plugins/seo/plugin-seo/src/node/getOGPInfo.ts @@ -29,10 +29,7 @@ export const getOGPInfo = ( twitterID?: SeoPluginOptions['twitterID'] }, ): SeoContent => { - const { - options: { base }, - siteData, - } = app + const { base, title: siteTitle, locales } = app.siteData const { frontmatter: { author: pageAuthor, @@ -44,13 +41,6 @@ export const getOGPInfo = ( data: { git = {} }, } = page - const title = - // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition - siteData.locales[page.pathLocale]?.title || - siteData.title || - // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition - siteData.locales['/']?.title || - '' const author = getSEOAuthor(pageAuthor || globalAuthor) const modifiedTime = git.updatedTime ? new Date(git.updatedTime).toISOString() @@ -59,20 +49,21 @@ export const getOGPInfo = ( const articleTitle = page.title const cover = getCover(page, app, hostname) const images = getImages(page, app, hostname) - const locales = getAlternatePaths(page, app) + const alternateLocalePaths = getAlternatePaths(page, app) const publishedTime = getDate(date)?.toISOString() const ogImage = cover || images[0] || fallBackImage const defaultOGP: SeoContent = { 'og:url': getUrl(hostname, base, page.path), - 'og:site_name': title, + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition + 'og:site_name': locales[page.pathLocale]?.title || siteTitle, 'og:title': articleTitle, 'og:description': page.frontmatter.description || '', 'og:type': isArticle(page) ? 'article' : 'website', 'og:image': ogImage, 'og:locale': page.lang, - 'og:locale:alternate': locales.map(({ lang }) => lang), + 'og:locale:alternate': alternateLocalePaths.map(({ lang }) => lang), ...(modifiedTime ? { 'og:updated_time': modifiedTime } : {}), ...(restrictions ? { 'og:restrictions:age': restrictions } : {}), ...(twitterID ? { 'twitter:creator': twitterID } : {}), diff --git a/plugins/seo/plugin-seo/src/node/utils/getLinks.ts b/plugins/seo/plugin-seo/src/node/utils/getLinks.ts index bbe437d39f..52eab17e5a 100644 --- a/plugins/seo/plugin-seo/src/node/utils/getLinks.ts +++ b/plugins/seo/plugin-seo/src/node/utils/getLinks.ts @@ -23,5 +23,5 @@ export const getAlternateLinks = ( ): { lang: string; path: string }[] => getAlternatePaths(page, app).map(({ lang, path }) => ({ lang, - path: getUrl(hostname, app.options.base, path), + path: getUrl(hostname, app.siteData.base, path), })) diff --git a/plugins/seo/plugin-sitemap/src/node/getInfo.ts b/plugins/seo/plugin-sitemap/src/node/getInfo.ts index ea69916e0b..3e5bdc9696 100644 --- a/plugins/seo/plugin-sitemap/src/node/getInfo.ts +++ b/plugins/seo/plugin-sitemap/src/node/getInfo.ts @@ -51,7 +51,7 @@ export const getSitemapInfos = ( ? new Date(page.data.git.updatedTime).toISOString() : '', } = options - const { base, locales } = app.options + const { base, locales } = app.siteData const pageLocalesMap = getPagesLocaleMap(app) diff --git a/plugins/tools/plugin-google-tag-manager/src/node/googleTagManagerPlugin.ts b/plugins/tools/plugin-google-tag-manager/src/node/googleTagManagerPlugin.ts index baf4631e25..2134a8b9f5 100644 --- a/plugins/tools/plugin-google-tag-manager/src/node/googleTagManagerPlugin.ts +++ b/plugins/tools/plugin-google-tag-manager/src/node/googleTagManagerPlugin.ts @@ -29,7 +29,7 @@ export const googleTagManagerPlugin = ...plugin, onInitialized: () => { - app.options.head.push([ + app.siteData.head.push([ 'script', {}, `\ diff --git a/plugins/tools/plugin-redirect/src/cli/index.ts b/plugins/tools/plugin-redirect/src/cli/index.ts index 2890f82580..6fdb122e0a 100644 --- a/plugins/tools/plugin-redirect/src/cli/index.ts +++ b/plugins/tools/plugin-redirect/src/cli/index.ts @@ -108,7 +108,7 @@ cli await Promise.all( app.pages.map((page) => { const redirectUrl = `${removeEndingSlash(commandOptions.hostname)}${ - app.options.base + app.siteData.base }${removeLeadingSlash(page.path)}` const destLocation = path.join( outputFolder, diff --git a/plugins/tools/plugin-redirect/src/node/getRedirectLocaleConfig.ts b/plugins/tools/plugin-redirect/src/node/getRedirectLocaleConfig.ts index aaebc32807..93cfdbffe0 100644 --- a/plugins/tools/plugin-redirect/src/node/getRedirectLocaleConfig.ts +++ b/plugins/tools/plugin-redirect/src/node/getRedirectLocaleConfig.ts @@ -18,7 +18,7 @@ export const getRedirectBehaviorConfig = ( app: App, options: RedirectPluginOptions, ): RedirectBehaviorConfig => { - const { locales } = app.options + const { locales } = app.siteData const config = deepAssign( fromEntries( diff --git a/plugins/tools/plugin-redirect/src/node/handleRedirectTo.ts b/plugins/tools/plugin-redirect/src/node/handleRedirectTo.ts index e89247cba2..a056447cf8 100644 --- a/plugins/tools/plugin-redirect/src/node/handleRedirectTo.ts +++ b/plugins/tools/plugin-redirect/src/node/handleRedirectTo.ts @@ -4,8 +4,7 @@ import { normalizePath } from '../shared/normalizePath.js' import type { RedirectPluginFrontmatter } from './types/index.js' export const handleRedirectTo = ({ frontmatter }: Page, app: App): void => { - const { base } = app.options - + const { base } = app.siteData const { redirectTo } = frontmatter as RedirectPluginFrontmatter if (redirectTo) { diff --git a/tools/helper/src/node/bundler/customizeDevServer.ts b/tools/helper/src/node/bundler/customizeDevServer.ts index 2d14e72e94..8b7e7a2f1e 100644 --- a/tools/helper/src/node/bundler/customizeDevServer.ts +++ b/tools/helper/src/node/bundler/customizeDevServer.ts @@ -51,7 +51,7 @@ export const customizeDevServer = ( path, }: DevServerOptions, ): void => { - const { base } = app.options + const { base } = app.siteData const bundlerName = getBundlerName(app) // in dev diff --git a/tools/helper/src/node/locales/helpers.ts b/tools/helper/src/node/locales/helpers.ts index 6b6d417c02..91a514099f 100644 --- a/tools/helper/src/node/locales/helpers.ts +++ b/tools/helper/src/node/locales/helpers.ts @@ -122,10 +122,10 @@ export const getLocaleConfig = ({ .map<[string, T]>((localePath) => { const defaultLocaleData = (defaultLocalesConfig[localePath] as T | undefined) ?? - (inferLocalePath(app.options.locales[localePath].lang) === '/' + (inferLocalePath(app.siteData.locales[localePath].lang) === '/' ? null : defaultLocalesConfig[ - inferLocalePath(app.options.locales[localePath].lang) + inferLocalePath(app.siteData.locales[localePath].lang) ]) if (!defaultLocaleData)