Skip to content

Commit

Permalink
feat(sitemap2): add legacy option
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Jun 2, 2022
1 parent 30efc55 commit a9d413d
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
28 changes: 28 additions & 0 deletions packages/sitemap2/src/node/compact/convert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { deprecatedLogger } from "./utils";
import type { SitemapOptions } from "../../shared";

/** @deprecated */
export const covertOptions = (
options: SitemapOptions & Record<string, unknown>
): void => {
deprecatedLogger({
options,
deprecatedOption: "urls",
newOption: "extraUrls",
});
deprecatedLogger({
options,
deprecatedOption: "exclude",
newOption: "excludeUrls",
});
deprecatedLogger({
options,
deprecatedOption: "outFile",
newOption: "sitemapFilename",
});
deprecatedLogger({
options,
deprecatedOption: "dateFormatter",
newOption: "modifyTimeGetter",
});
};
1 change: 1 addition & 0 deletions packages/sitemap2/src/node/compact/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./convert";
41 changes: 41 additions & 0 deletions packages/sitemap2/src/node/compact/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { logger } from "../utils";

export interface DeprecatedLoggerOptions {
options: Record<string, unknown>;
deprecatedOption: string;
newOption: string;
msg?: string;
scope?: string;
}

export const deprecatedLogger = ({
options,
deprecatedOption,
newOption,
msg = "",
scope = "",
}: DeprecatedLoggerOptions): void => {
if (deprecatedOption in options) {
logger.warn(
`"${deprecatedOption}" is deprecated${
scope ? ` in ${scope}` : ""
}, 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<string, unknown>;
} else temp[key] = options[deprecatedOption];
});
} else options[newOption] = options[deprecatedOption];

delete options[deprecatedOption];
}
};
6 changes: 5 additions & 1 deletion packages/sitemap2/src/node/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { chalk } from "@vuepress/utils";
import { covertOptions } from "./compact";
import { generateSiteMap } from "./generateSitemap";
import { logger } from "./utils";

import type { PluginObject, PluginFunction } from "@vuepress/core";
import type { SitemapOptions } from "../shared";

export const sitemapPlugin =
(options: SitemapOptions): PluginFunction =>
(options: SitemapOptions, legacy = false): PluginFunction =>
(app) => {
// TODO: Remove it in v2 stable
if (legacy)
covertOptions(options as SitemapOptions & Record<string, unknown>);
if (app.env.isDebug) logger.info(`Options: ${options.toString()}`);

const plugin: PluginObject = {
Expand Down

0 comments on commit a9d413d

Please sign in to comment.