Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: better frontmatter code #1892

Merged
merged 2 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/client/theme-api/useRouteMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ export const useRouteMeta = () => {
}
}, [clientRoutes.length, pathname]);

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

if (curRoute && 'meta' in curRoute && (curRoute.meta as any)._atom_route) {
meta._atom_route = true;
if (curRoute && 'meta' in curRoute) {
meta = {
...(curRoute.meta as any),
...meta,
};
}

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
Loading