Skip to content

Commit

Permalink
chore: tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Jan 30, 2024
1 parent b32950d commit af20d77
Show file tree
Hide file tree
Showing 13 changed files with 279 additions and 232 deletions.
8 changes: 4 additions & 4 deletions plugins/plugin-feed/src/node/addFeedLinks.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { keys } from '@vuepress/helper/node'
import type { App } from 'vuepress/core'
import type { HeadConfig } from 'vuepress/shared'
import type { ResolvedFeedOptionsMap } from './options.js'
import { getFilename } from './options.js'
import { getFeedFilenames } from './getFeedFilenames.js'
import type { ResolvedFeedOptionsMap } from './getFeedOptions.js'
import { getUrl } from './utils/index.js'

export const addFeedLinks = (
Expand All @@ -16,7 +16,7 @@ export const addFeedLinks = (
// there is only one language, so we append it to siteData
if (localePaths.length === 1) {
const { atomOutputFilename, jsonOutputFilename, rssOutputFilename } =
getFilename(options['/'])
getFeedFilenames(options['/'])
const { atom, json, rss, hostname } = options['/']

const getHeadItem = (
Expand Down Expand Up @@ -64,7 +64,7 @@ export const addFeedLinks = (

if (localePaths.includes(pathLocale)) {
const { atomOutputFilename, jsonOutputFilename, rssOutputFilename } =
getFilename(localeOptions, pathLocale)
getFeedFilenames(localeOptions, pathLocale)

const getHeadItem = (
name: string,
Expand Down
2 changes: 1 addition & 1 deletion plugins/plugin-feed/src/node/feed/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import type {
FeedGetter,
FeedPluginFrontmatter,
} from '../../typings/index.js'
import type { ResolvedFeedOptions } from '../options.js'
import type { ResolvedFeedOptions } from '../getFeedOptions.js'
import {
getFeedAuthor,
getFeedCategory,
Expand Down
12 changes: 7 additions & 5 deletions plugins/plugin-feed/src/node/feed/store.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { App } from 'vuepress/core'
import type {
FeedCategory,
FeedChannelOption,
FeedChannelOptions,
FeedContributor,
} from '../../typings/index.js'
import type { FeedLinks, ResolvedFeedOptions } from '../options.js'
import { getFeedChannelOption, getFeedLinks } from '../options.js'
import { getFeedChannelOptions } from '../getFeedChannelOptions.js'
import type { FeedLinks } from '../getFeedLinks.js'
import { getFeedLinks } from '../getFeedLinks.js'
import type { ResolvedFeedOptions } from '../getFeedOptions.js'
import type { FeedItem } from './item.js'

export class FeedStore {
Expand All @@ -14,14 +16,14 @@ export class FeedStore {
public items: FeedItem[] = []

private _contributorKeys = new Set<string>()
public channel: FeedChannelOption
public channel: FeedChannelOptions
public links: FeedLinks
constructor(
app: App,
localeOptions: ResolvedFeedOptions,
localePath: string,
) {
this.channel = getFeedChannelOption(app, localeOptions, localePath)
this.channel = getFeedChannelOptions(app, localeOptions, localePath)
this.links = getFeedLinks(app, localeOptions, localePath)
}

Expand Down
6 changes: 3 additions & 3 deletions plugins/plugin-feed/src/node/feedPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { isLinkHttp, removeEndingSlash } from 'vuepress/shared'
import { colors } from 'vuepress/utils'
import type { FeedPluginOptions } from '../typings/index.js'
import { addFeedLinks } from './addFeedLinks.js'
import { getFeedFiles } from './getFeed.js'
import { getAtomTemplates, getRSSTemplates } from './getTemplate.js'
import { getFeedOptions } from './options.js'
import { getFeedFiles } from './getFeedFiles.js'
import { getFeedOptions } from './getFeedOptions.js'
import { getAtomTemplates, getRSSTemplates } from './getTemplates.js'
import { writeFiles } from './output.js'
import { FEED_GENERATOR, logger } from './utils/index.js'

Expand Down
62 changes: 62 additions & 0 deletions plugins/plugin-feed/src/node/getFeedChannelOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { isArray } from '@vuepress/helper/node'
import type { App } from 'vuepress/core'
import { isLinkHttp } from 'vuepress/shared'
import type { FeedChannelOptions, FeedPluginOptions } from '../typings/index.js'
import { getUrl } from './utils/index.js'

export const getFeedChannelOptions = (
app: App,
options: FeedPluginOptions,
localePath = '',
): FeedChannelOptions => {
const { base } = app.options
const { title, description, lang, locales } = app.siteData
const {
channel: { icon: channelIcon, image: channelImage, ...channel } = {},
hostname,
icon,
image,
} = options
const authorName = isArray(options.channel?.author)
? options.channel?.author[0]?.name
: options.channel?.author?.name

const defaultChannelOption: FeedChannelOptions = {
title: locales[localePath]?.title || title || locales['/']?.title || '',
link: getUrl(hostname, base, localePath),
description:
locales[localePath]?.description ||
description ||
locales['/']?.description ||
'',
language: locales[localePath]?.lang || lang,
copyright: authorName ? `Copyright by ${authorName}` : '',
pubDate: new Date(),
lastUpdated: new Date(),
...(icon
? { icon: isLinkHttp(icon) ? icon : getUrl(hostname, base, icon) }
: {}),
...(image
? { image: isLinkHttp(image) ? image : getUrl(hostname, base, image) }
: {}),
}

return {
...defaultChannelOption,
...channel,
...(channelIcon
? {
icon: isLinkHttp(channelIcon)
? channelIcon
: getUrl(hostname, base, channelIcon),
}
: {}),
...(channelImage
? {
image: isLinkHttp(channelImage)
? channelImage
: getUrl(hostname, base, channelImage),
}
: {}),
}
}
70 changes: 70 additions & 0 deletions plugins/plugin-feed/src/node/getFeedFilenames.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import type { App } from 'vuepress/core'
import { removeLeadingSlash } from 'vuepress/shared'
import type { FeedPluginOptions } from '../typings/index.js'
import type { ResolvedFeedOptions } from './getFeedOptions.js'
import { getUrl } from './utils/index.js'

export const getFeedFilenames = (
options: ResolvedFeedOptions,
prefix = '/',
): Required<
Pick<
FeedPluginOptions,
| 'atomOutputFilename'
| 'atomXslFilename'
| 'jsonOutputFilename'
| 'rssOutputFilename'
| 'rssXslFilename'
>
> => ({
atomOutputFilename: `${removeLeadingSlash(prefix)}${
options.atomOutputFilename || 'atom.xml'
}`,
atomXslFilename: `${removeLeadingSlash(prefix)}${
options.atomXslFilename || 'atom.xsl'
}`,

jsonOutputFilename: `${removeLeadingSlash(prefix)}${
options.jsonOutputFilename || 'feed.json'
}`,
rssOutputFilename: `${removeLeadingSlash(prefix)}${
options.rssOutputFilename || 'rss.xml'
}`,
rssXslFilename: `${removeLeadingSlash(prefix)}${
options.rssXslFilename || 'rss.xsl'
}`,
})

export interface FeedLinks {
localePath: string
atom: string
atomXsl: string
json: string
rss: string
rssXsl: string
}

export const getFeedLinks = (
app: App,
options: ResolvedFeedOptions,
localePath: string,
): FeedLinks => {
const { base } = app.options
const { hostname } = options
const {
atomOutputFilename,
atomXslFilename,
jsonOutputFilename,
rssOutputFilename,
rssXslFilename,
} = getFeedFilenames(options, localePath)

return {
localePath,
atom: getUrl(hostname, base, atomOutputFilename),
atomXsl: getUrl(hostname, base, atomXslFilename),
json: getUrl(hostname, base, jsonOutputFilename),
rss: getUrl(hostname, base, rssOutputFilename),
rssXsl: getUrl(hostname, base, rssXslFilename),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { FeedItem, FeedStore } from './feed/index.js'
import { getAtomFeed } from './generator/atom/index.js'
import { getJSONFeed } from './generator/json/index.js'
import { getRssFeed } from './generator/rss/index.js'
import type { ResolvedFeedOptionsMap } from './options.js'
import { getFilename } from './options.js'
import { getFeedFilenames } from './getFeedFilenames.js'
import type { ResolvedFeedOptionsMap } from './getFeedOptions.js'
import { logger } from './utils/index.js'

export const getFeedFiles = (
Expand Down Expand Up @@ -67,7 +67,7 @@ export const getFeedFiles = (
)

const { atomOutputFilename, jsonOutputFilename, rssOutputFilename } =
getFilename(localeOptions, localePath)
getFeedFilenames(localeOptions, localePath)
const results: FeedConfig[] = []

// generate feed
Expand Down
38 changes: 38 additions & 0 deletions plugins/plugin-feed/src/node/getFeedLinks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { App } from 'vuepress/core'
import { getFeedFilenames } from './getFeedFilenames.js'
import type { ResolvedFeedOptions } from './getFeedOptions.js'
import { getUrl } from './utils/index.js'

export interface FeedLinks {
localePath: string
atom: string
atomXsl: string
json: string
rss: string
rssXsl: string
}

export const getFeedLinks = (
app: App,
options: ResolvedFeedOptions,
localePath: string,
): FeedLinks => {
const { base } = app.options
const { hostname } = options
const {
atomOutputFilename,
atomXslFilename,
jsonOutputFilename,
rssOutputFilename,
rssXslFilename,
} = getFeedFilenames(options, localePath)

return {
localePath,
atom: getUrl(hostname, base, atomOutputFilename),
atomXsl: getUrl(hostname, base, atomXslFilename),
json: getUrl(hostname, base, jsonOutputFilename),
rss: getUrl(hostname, base, rssOutputFilename),
rssXsl: getUrl(hostname, base, rssXslFilename),
}
}
84 changes: 84 additions & 0 deletions plugins/plugin-feed/src/node/getFeedOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import {
dateSorter,
fromEntries,
isArray,
isFunction,
keys,
} from '@vuepress/helper/node'
import type { GitData } from '@vuepress/plugin-git'
import type { App, Page } from 'vuepress/core'
import type {
BaseFeedPluginOptions,
FeedPluginOptions,
} from '../typings/index.js'

export interface ResolvedFeedOptions
extends Omit<
BaseFeedPluginOptions,
'sorter' | 'filter' | 'preservedElements'
>,
Required<Pick<BaseFeedPluginOptions, 'sorter' | 'filter'>> {
hostname: string
isPreservedElement: (tagName: string) => boolean
}

export type ResolvedFeedOptionsMap = Record<string, ResolvedFeedOptions>

export const getFeedOptions = (
{ siteData }: App,
options: FeedPluginOptions,
): ResolvedFeedOptionsMap =>
fromEntries(
keys({
// root locale must exists
'/': {},
...siteData.locales,
}).map((localePath) => {
const preservedElements =
options.locales?.[localePath]?.preservedElements ||
options.preservedElements
const { hostname, devServer, locales, ...rest } = options

return [
localePath,
{
// default values
filter: ({ frontmatter, filePathRelative }: Page): boolean =>
!(
frontmatter.home ||
!filePathRelative ||
frontmatter.article === false ||
frontmatter.feed === false
),
sorter: (
pageA: Page<{ git?: GitData }, Record<string, never>>,
pageB: Page<{ git?: GitData }, Record<string, never>>,
): number =>
dateSorter(
pageA.data.git?.createdTime
? new Date(pageA.data.git?.createdTime)
: pageA.frontmatter.date,
pageB.data.git?.createdTime
? new Date(pageB.data.git?.createdTime)
: pageB.frontmatter.date,
),

...rest,
...options.locales?.[localePath],

// make sure these are not overrode
hostname,
isPreservedElement: isArray(preservedElements)
? (tagName: string): boolean =>
preservedElements.some((item) =>
item instanceof RegExp
? item.test(tagName)
: item === tagName,
)
: isFunction(preservedElements)
? preservedElements
: (): boolean => false,
} as ResolvedFeedOptions,
]
}),
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { entries } from '@vuepress/helper/node'
import { ensureEndingSlash } from 'vuepress/shared'
import { fs, getDirname, path } from 'vuepress/utils'
import type { FeedConfig } from '../typings/index.js'
import type { ResolvedFeedOptionsMap } from './options.js'
import { getFilename } from './options.js'
import { getFeedFilenames } from './getFeedFilenames.js'
import type { ResolvedFeedOptionsMap } from './getFeedOptions.js'

const __dirname = getDirname(import.meta.url)

Expand All @@ -30,7 +30,7 @@ export const getAtomTemplates = (
// write template
.map(([localePath, localeOptions]) => {
const { atomXslTemplate = DEFAULT_ATOM_XML_TEMPLATE } = localeOptions
const { atomXslFilename } = getFilename(localeOptions, localePath)
const { atomXslFilename } = getFeedFilenames(localeOptions, localePath)

return [atomXslFilename, atomXslTemplate]
})
Expand All @@ -45,7 +45,7 @@ export const getRSSTemplates = (
.map(([localePath, localeOptions]) => {
const { rssXslTemplate = DEFAULT_RSS_XML_TEMPLATE } = localeOptions

const { rssXslFilename } = getFilename(localeOptions, localePath)
const { rssXslFilename } = getFeedFilenames(localeOptions, localePath)

return [rssXslFilename, rssXslTemplate]
})
Loading

0 comments on commit af20d77

Please sign in to comment.