Skip to content

Commit

Permalink
fix(theme): fix blog related issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Dec 9, 2022
1 parent a0e1740 commit c232fa4
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 38 deletions.
10 changes: 0 additions & 10 deletions packages/theme/src/node/page/extends.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/theme/src/node/page/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from "./extends.js";
export * from "./info.js";
3 changes: 3 additions & 0 deletions packages/theme/src/node/plugins/blog.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { blogPlugin } from "vuepress-plugin-blog2";
import { getDate, timeTransformer } from "vuepress-shared/node";
import { checkFrontmatter } from "../frontmatter/check.js";
import { ArticleInfoType, PageType } from "../../shared/index.js";

import type { App, Page, Plugin } from "@vuepress/core";
Expand Down Expand Up @@ -125,6 +126,8 @@ export const getBlogPlugin = (
);
const isSlide = isArticle && frontmatter.layout === "Slide";

checkFrontmatter(page, app.env.isDebug);

// save page type to routeMeta
info[ArticleInfoType.type] = frontmatter.home
? PageType.home
Expand Down
51 changes: 51 additions & 0 deletions packages/theme/src/node/plugins/pageConverter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { injectLocalizedDate } from "vuepress-shared";
import { convertFrontmatter } from "../compact/index.js";
import { checkFrontmatter } from "../frontmatter/check.js";
import { ArticleInfoType } from "../../shared/index.js";

import type { App, Page, PluginObject } from "@vuepress/core";
import type {
ThemeBlogHomePageFrontmatter,
ThemePageData,
ThemeProjectHomePageFrontmatter,
ThemeNormalPageFrontmatter,
} from "../../shared/index.js";

export const injectPageInfo = (page: Page<ThemePageData>): void => {
const { filePathRelative } = page;
const frontmatter = page.frontmatter as
| ThemeProjectHomePageFrontmatter
| ThemeBlogHomePageFrontmatter
| ThemeNormalPageFrontmatter;

// save relative file path into page data to generate edit link
page.data.filePathRelative = filePathRelative;

page.routeMeta["title"] = page.title;

if ("icon" in frontmatter)
page.routeMeta[ArticleInfoType.icon] = frontmatter.icon;

// resolve shortTitle
if ("shortTitle" in frontmatter)
page.routeMeta[ArticleInfoType.shortTitle] = frontmatter.shortTitle;
};

export const extendsPagePlugin = (legacy = false): PluginObject => ({
name: "vuepress-theme-hope-extends-page",
extendsPage: (page, app) => {
if (legacy)
page.frontmatter = convertFrontmatter(
page.frontmatter,
page.filePathRelative || ""
);

checkFrontmatter(page, app.env.isDebug);
injectLocalizedDate(page);
injectPageInfo(<Page<ThemePageData>>page);
},
});

export const useExtendsPagePlugin = (app: App, legacy = false) => {
app.use(extendsPagePlugin(legacy));
};
6 changes: 5 additions & 1 deletion packages/theme/src/node/plugins/use.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import { useGitPlugin } from "vuepress-shared/node";

import type { App } from "@vuepress/core";
import type { PluginsOptions } from "../../shared/index.js";
import { useExtendsPagePlugin } from "./pageConverter.js";

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

export const usePlugin = (
app: App,
plugins: PluginsOptions,
hotReload = false
legacy: boolean,
hotReload: boolean
): void => {
// respect git options
if ("git" in plugins)
Expand Down Expand Up @@ -50,4 +52,6 @@ export const usePlugin = (
palette: ".vuepress/styles/palette.scss",
style: ".vuepress/styles/index.scss",
});

useExtendsPagePlugin(app, legacy);
};
35 changes: 9 additions & 26 deletions packages/theme/src/node/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,12 @@ import { watch } from "chokidar";

import { resolveAlias } from "./alias.js";
import { extendsBundlerOptions } from "./bundler.js";
import {
checkStyle,
convertFrontmatter,
convertThemeOptions,
} from "./compact/index.js";
import { checkStyle, convertThemeOptions } from "./compact/index.js";
import {
checkSocialMediaIcons,
getStatus,
getThemeData,
} from "./config/index.js";
import { checkFrontmatter } from "./frontmatter/index.js";
import { extendsPage } from "./page/index.js";
import { getPluginConfig, usePlugin } from "./plugins/index.js";
import {
prepareConfigFile,
Expand All @@ -23,8 +17,8 @@ import {
prepareThemeColorScss,
} from "./prepare/index.js";

import type { Page, ThemeFunction } from "@vuepress/core";
import type { ThemeOptions, ThemePageData } from "../shared/index.js";
import type { ThemeFunction } from "@vuepress/core";
import type { ThemeOptions } from "../shared/index.js";

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

Expand Down Expand Up @@ -55,7 +49,7 @@ export const hopeTheme =
const themeConfig = getThemeData(app, themeOptions, status);
const icons = status.enableBlog ? checkSocialMediaIcons(themeConfig) : {};

usePlugin(app, plugins, hotReload);
usePlugin(app, plugins, legacy, hotReload);

if (isDebug) console.log("Theme plugin options:", plugins);

Expand All @@ -73,17 +67,6 @@ export const hopeTheme =

extendsBundlerOptions,

extendsPage: (page): void => {
if (legacy)
page.frontmatter = convertFrontmatter(
page.frontmatter,
page.filePathRelative || ""
);

checkFrontmatter(page, app.env.isDebug);
extendsPage(<Page<ThemePageData>>page);
},

onPrepared: (): Promise<void> =>
Promise.all([
prepareSidebarData(app, themeConfig, sidebarSorter),
Expand All @@ -94,22 +77,22 @@ export const hopeTheme =
onWatched: (app, watchers): void => {
if (hotReload) {
// this ensure the page is generated or updated
const searchIndexWatcher = watch("pages/**/*.vue", {
const structureSidebarWatcher = watch("pages/**/*.vue", {
cwd: app.dir.temp(),
ignoreInitial: true,
});

searchIndexWatcher.on("add", () => {
structureSidebarWatcher.on("add", () => {
void prepareSidebarData(app, themeConfig, sidebarSorter);
});
searchIndexWatcher.on("change", () => {
structureSidebarWatcher.on("change", () => {
void prepareSidebarData(app, themeConfig, sidebarSorter);
});
searchIndexWatcher.on("unlink", () => {
structureSidebarWatcher.on("unlink", () => {
void prepareSidebarData(app, themeConfig, sidebarSorter);
});

watchers.push(searchIndexWatcher);
watchers.push(structureSidebarWatcher);
}
},

Expand Down

0 comments on commit c232fa4

Please sign in to comment.