Skip to content

Commit

Permalink
refactor: better frontmatter code (#1892)
Browse files Browse the repository at this point in the history
* refactor: better frontmatter code

* fix: render loop
  • Loading branch information
MadCcc authored Sep 14, 2023
1 parent ff343de commit a42dfb2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
19 changes: 12 additions & 7 deletions src/client/theme-api/useRouteMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ const useAsyncRouteMeta = (id: string) => {
return use<ReturnType<getRouteMetaById>>(cache.get(id));
};

const emptyMeta = {
frontmatter: {},
toc: [],
texts: [],
};

/**
* hook for get matched route meta
*/
Expand All @@ -38,14 +44,13 @@ export const useRouteMeta = () => {
}
}, [clientRoutes.length, pathname]);

const meta: IRouteMeta = useAsyncRouteMeta((curRoute as any)?.id) || {
frontmatter: {},
toc: [],
texts: [],
};
const meta: IRouteMeta =
useAsyncRouteMeta((curRoute as any)?.id) || emptyMeta;

if (curRoute && 'meta' in curRoute && (curRoute.meta as any)._atom_route) {
meta._atom_route = true;
if (curRoute && 'meta' in curRoute && typeof curRoute.meta === 'object') {
Object.keys(curRoute.meta as IRouteMeta).forEach((key) => {
(meta as any)[key] ??= (curRoute as any).meta[key];
});
}

return meta;
Expand Down
4 changes: 1 addition & 3 deletions src/templates/meta-frontmatter.ts.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { frontmatter as fm{{{index}}} } from '{{{file}}}?type=frontmatter';

export const filesFrontmatter = {
{{#metaFiles}}
'{{{id}}}': {
frontmatter: fm{{{index}}},
},
'{{{id}}}': fm{{{index}}},
{{/metaFiles}}
}
2 changes: 1 addition & 1 deletion src/templates/meta-route.ts.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const getRouteMetaById = async (id: string) => {
return {
texts: text?.texts,
toc: text?.toc,
frontmatter: frontmatter?.frontmatter,
frontmatter,
tabs: tabsMeta,
};
}
8 changes: 4 additions & 4 deletions src/templates/meta-runtime.ts.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ function wrapEmpty(meta, fieldName, defaultValue) {
export const patchRoutes = ({ routes }) => {
Object.values(routes).forEach((route) => {
if (filesFrontmatter[route.id]) {
if (process.env.NODE_ENV === 'production' && (route.meta?.frontmatter?.debug || filesFrontmatter[route.id].frontmatter.debug)) {
if (process.env.NODE_ENV === 'production' && (route.meta?.frontmatter?.debug || filesFrontmatter[route.id].debug)) {
// hide route in production which set hide frontmatter
delete routes[route.id];
} else {
// merge meta to route object
route.meta = deepmerge(route.meta, filesFrontmatter[route.id]);
route.meta = deepmerge(route.meta, { frontmatter: filesFrontmatter[route.id] });

wrapEmpty(route.meta, 'demos', {});
wrapEmpty(route.meta, 'texts', []);

// apply real tab data from id
route.meta.tabs = route.meta.tabs?.map((id) => {
const meta = filesFrontmatter[id] || {
frontmatter: { title: tabs[id].title },
const meta = {
frontmatter: filesFrontmatter[id] || { title: tabs[id].title },
toc: [],
texts: [],
}
Expand Down

0 comments on commit a42dfb2

Please sign in to comment.