-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b32950d
commit af20d77
Showing
13 changed files
with
279 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
: {}), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
] | ||
}), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.