Skip to content

Commit

Permalink
chore: prefer using siteData (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope authored Dec 20, 2024
1 parent 5308fb4 commit f62a06b
Show file tree
Hide file tree
Showing 20 changed files with 49 additions and 68 deletions.
23 changes: 8 additions & 15 deletions plugins/blog/plugin-feed/src/node/addFeedLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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`,
},
Expand Down
2 changes: 1 addition & 1 deletion plugins/blog/plugin-feed/src/node/feed/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions plugins/blog/plugin-feed/src/node/getFeedChannelOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion plugins/blog/plugin-feed/src/node/getFeedFilenames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion plugins/blog/plugin-feed/src/node/getFeedLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 7 additions & 12 deletions plugins/pwa/plugin-pwa/src/node/getManifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ export const getManifest = async (
app: App,
options: PwaPluginOptions,
): Promise<AppManifest> => {
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',
Expand All @@ -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,

Expand Down
8 changes: 3 additions & 5 deletions plugins/pwa/plugin-pwa/src/node/injectLinksToHead.ts
Original file line number Diff line number Diff line change
@@ -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[] = []

Expand Down Expand Up @@ -75,6 +75,4 @@ export const injectLinksToHead = (
} else if (apple !== false && fallBackIcon) {
setLink('apple-touch-icon', fallBackIcon)
}

return head
}
5 changes: 3 additions & 2 deletions plugins/pwa/plugin-pwa/src/node/pwaPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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,
Expand Down
11 changes: 8 additions & 3 deletions plugins/pwa/plugin-pwa/tests/node/injectLinksToHead.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ describe('Test head function', () => {
},
})

expect(injectLinksToHead(app, options)).toEqual([
injectLinksToHead(app, options)

expect(app.siteData.head).toEqual([
[
'meta',
{
Expand Down Expand Up @@ -176,7 +178,9 @@ describe('Test head function', () => {
},
}

expect(injectLinksToHead(app, optionsWithManifest)).toEqual([
injectLinksToHead(app, optionsWithManifest)

expect(app.siteData.head).toEqual([
[
'meta',
{
Expand Down Expand Up @@ -240,7 +244,8 @@ describe('Test head function', () => {
},
})

expect(injectLinksToHead(app, options)).toEqual([
injectLinksToHead(app, options)
expect(app.siteData.head).toEqual([
[
'link',
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),

Expand Down
2 changes: 1 addition & 1 deletion plugins/seo/plugin-seo/src/node/generateDescription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down
19 changes: 5 additions & 14 deletions plugins/seo/plugin-seo/src/node/getOGPInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()
Expand All @@ -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 } : {}),
Expand Down
2 changes: 1 addition & 1 deletion plugins/seo/plugin-seo/src/node/utils/getLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}))
2 changes: 1 addition & 1 deletion plugins/seo/plugin-sitemap/src/node/getInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const googleTagManagerPlugin =
...plugin,

onInitialized: () => {
app.options.head.push([
app.siteData.head.push([
'script',
{},
`\
Expand Down
2 changes: 1 addition & 1 deletion plugins/tools/plugin-redirect/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const getRedirectBehaviorConfig = (
app: App,
options: RedirectPluginOptions,
): RedirectBehaviorConfig => {
const { locales } = app.options
const { locales } = app.siteData

const config = deepAssign(
fromEntries(
Expand Down
3 changes: 1 addition & 2 deletions plugins/tools/plugin-redirect/src/node/handleRedirectTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion tools/helper/src/node/bundler/customizeDevServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const customizeDevServer = (
path,
}: DevServerOptions,
): void => {
const { base } = app.options
const { base } = app.siteData
const bundlerName = getBundlerName(app)

// in dev
Expand Down
4 changes: 2 additions & 2 deletions tools/helper/src/node/locales/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ export const getLocaleConfig = <T extends LocaleData>({
.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)
Expand Down

0 comments on commit f62a06b

Please sign in to comment.