Skip to content

Commit

Permalink
fix(feed2): use compareDate utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Oct 9, 2023
1 parent fd9494b commit 1837738
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 37 deletions.
26 changes: 2 additions & 24 deletions packages/feed2/src/node/generator/generator.ts
Expand Up @@ -8,7 +8,7 @@ import { FeedInfo } from "../extractor/index.js";
import type { ResolvedFeedOptionsMap } from "../options.js";
import { getFeedChannelOption, getFeedLinks, getFilename } from "../options.js";
import type { FeedPluginFrontmatter } from "../typings/index.js";
import { compareDate, logger } from "../utils/index.js";
import { logger } from "../utils/index.js";

export class FeedGenerator {
/** feed 生成器 */
Expand All @@ -32,32 +32,10 @@ export class FeedGenerator {
addPages(localePath: string): void {
const feed = this.feedMap[localePath];
const localeOption = this.options[localePath];
const {
count: feedCount = 100,
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 =>
compareDate(
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,
),
} = localeOption;
const { count: feedCount = 100, filter, sorter } = localeOption;
const pages = this.app.pages
.filter((page) => page.pathLocale === localePath)
.filter(filter)
// @ts-ignore
.sort(sorter)
.slice(0, feedCount);

Expand Down
11 changes: 8 additions & 3 deletions packages/feed2/src/node/options.ts
Expand Up @@ -2,6 +2,7 @@ import type { App, Page } from "@vuepress/core";
import type { GitData } from "@vuepress/plugin-git";
import { getDirname, path } from "@vuepress/utils";
import {
compareDate,
deepAssign,
ensureEndingSlash,
fromEntries,
Expand All @@ -19,15 +20,19 @@ import type {
FeedLinks,
FeedOptions,
} from "./typings/index.js";
import { compareDate, resolveUrl } from "./utils/index.js";
import { resolveUrl } from "./utils/index.js";

const __dirname = getDirname(import.meta.url);

const TEMPLATE_FOLDER = ensureEndingSlash(
path.resolve(__dirname, "../../templates"),
);

export type ResolvedFeedOptions = BaseFeedOptions & { hostname: string };
export interface ResolvedFeedOptions
extends Omit<BaseFeedOptions, "sorter" | "filter">,
Required<Pick<BaseFeedOptions, "sorter" | "filter">> {
hostname: string;
}

export type ResolvedFeedOptionsMap = Record<string, ResolvedFeedOptions>;

Expand Down Expand Up @@ -181,7 +186,7 @@ export const getFilename = (

export const getFeedLinks = (
{ options: { base } }: App,
options: FeedOptions,
options: ResolvedFeedOptions,
localePath: string,
): FeedLinks => {
const { hostname } = options;
Expand Down
10 changes: 0 additions & 10 deletions packages/feed2/src/node/utils/helper.ts
Expand Up @@ -9,16 +9,6 @@ export const FEED_GENERATOR = "vuepress-plugin-feed2";

export const logger = new Logger(FEED_GENERATOR);

export const compareDate = (
dateA: Date | string | undefined,
dateB: Date | string | undefined,
): number => {
if (!dateA || !(dateA instanceof Date)) return 1;
if (!dateB || !(dateB instanceof Date)) return -1;

return dateB.getTime() - dateA.getTime();
};

export const resolveUrl = (hostname: string, base = "", path = ""): string =>
`${
isLinkHttp(hostname)
Expand Down

0 comments on commit 1837738

Please sign in to comment.