diff --git a/packages/feed/src/node/compact/convert.ts b/packages/feed/src/node/compact/convert.ts new file mode 100644 index 000000000..bbacee73d --- /dev/null +++ b/packages/feed/src/node/compact/convert.ts @@ -0,0 +1,36 @@ +import { droppedLogger } from "./utils"; + +import type { FeedOptions } from "../../types"; + +/** @deprecated */ +export const covertOptions = ( + options: FeedOptions & Record +): void => { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + // eslint-disable-next-line + options.atom = options["output"]?.atom?.enable ?? true; + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + // eslint-disable-next-line + options.json = options["output"]?.json?.enable ?? true; + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + // eslint-disable-next-line + options.rss = options["output"]?.rss?.enable ?? true; + + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + // eslint-disable-next-line + options.atomOutputFilename = options["output"]?.atom?.path ?? "atom.xml"; + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + // eslint-disable-next-line + options.jsonOutputFilename = options["output"]?.json?.path ?? "feed.json"; + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + // eslint-disable-next-line + options.jsonOutputFilename = options["output"]?.rss?.path ?? "rss.xml"; + + droppedLogger(options, "output"); +}; diff --git a/packages/feed/src/node/compact/index.ts b/packages/feed/src/node/compact/index.ts new file mode 100644 index 000000000..9d3baa9e7 --- /dev/null +++ b/packages/feed/src/node/compact/index.ts @@ -0,0 +1 @@ +export * from "./convert"; diff --git a/packages/feed/src/node/compact/utils.ts b/packages/feed/src/node/compact/utils.ts new file mode 100644 index 000000000..65ed2fdde --- /dev/null +++ b/packages/feed/src/node/compact/utils.ts @@ -0,0 +1,57 @@ +export interface DeprecatedLoggerOptions { + options: Record; + deprecatedOption: string; + newOption: string; + msg?: string; + scope?: string; +} + +export const deprecatedLogger = ({ + options, + deprecatedOption, + newOption, + msg = "", +}: DeprecatedLoggerOptions): void => { + if (deprecatedOption in options) { + console.warn( + `"${deprecatedOption}" is deprecate in feed plugin, please use "${newOption}" instead.${ + msg ? `\n${msg}` : "" + }` + ); + + if (newOption.includes(".")) { + const keys = newOption.split("."); + let temp = options; + + keys.forEach((key, index) => { + if (index !== keys.length - 1) { + // ensure level exists + temp[key] = temp[key] || {}; + + temp = temp[key] as Record; + } else temp[key] = options[deprecatedOption]; + }); + } else options[newOption] = options[deprecatedOption]; + + delete options[deprecatedOption]; + } +}; + +export const droppedLogger = ( + options: Record, + droppedOption: string, + hint = "", + newOption = "" +): void => { + if (droppedOption in options) { + console.error( + `"${droppedOption}" is removed${ + newOption + ? `, please use ${newOption} instead.` + : " and no longer supported" + }${hint ? `\n${hint}` : ""}` + ); + + if (!newOption) delete options[droppedOption]; + } +}; diff --git a/packages/feed/src/node/index.ts b/packages/feed/src/node/index.ts index aedeb46dc..746fd19a0 100644 --- a/packages/feed/src/node/index.ts +++ b/packages/feed/src/node/index.ts @@ -1,7 +1,8 @@ import chalk from "chalk"; -import { checkOutput, ensureHostName, getFeedOptions } from "./options"; +import { covertOptions } from "./compact"; import { injectLinkstoHead } from "./injectHead"; import { FeedGenerator } from "./generator"; +import { checkOutput, ensureHostName, getFeedOptions } from "./options"; import type { Plugin, PluginOptionAPI } from "@mr-hope/vuepress-types"; import type { FeedOptions } from "../types"; @@ -12,6 +13,8 @@ const feedPlugin: Plugin = (options, context) => { name: "vuepress-plugin-feed2", }; + covertOptions(options as FeedOptions & Record); + if (!ensureHostName(options)) { error(`Option ${chalk.magenta("hostname")} is required!`);