From 5273fe2a9a79049e449c438faedfa8328e9beb54 Mon Sep 17 00:00:00 2001 From: dan Date: Sat, 1 Apr 2023 20:07:23 +0100 Subject: [PATCH] Add tag for search order (#5859) --- src/components/Layout/Page.tsx | 8 +++++++- src/components/Layout/getRouteMeta.tsx | 24 ++++++++++++++++++------ src/components/Seo.tsx | 5 +++++ 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/components/Layout/Page.tsx b/src/components/Layout/Page.tsx index 1c68db4d..967a7faf 100644 --- a/src/components/Layout/Page.tsx +++ b/src/components/Layout/Page.tsx @@ -35,7 +35,7 @@ interface PageProps { export function Page({children, toc, routeTree, meta, section}: PageProps) { const {asPath} = useRouter(); const cleanedPath = asPath.split(/[\?\#]/)[0]; - const {route, nextRoute, prevRoute, breadcrumbs} = getRouteMeta( + const {route, nextRoute, prevRoute, breadcrumbs, order} = getRouteMeta( cleanedPath, routeTree ); @@ -96,12 +96,18 @@ export function Page({children, toc, routeTree, meta, section}: PageProps) { showSidebar = false; } + let searchOrder; + if (section === 'learn' || (section === 'blog' && !isBlogIndex)) { + searchOrder = order; + } + return ( <> 0 ? breadcrumbs : [routeTree], }; } @@ -68,8 +79,10 @@ export function getRouteMeta(cleanedPath: string, routeTree: RouteItem) { function buildRouteMeta( searchPath: string, currentRoute: RouteItem, - ctx: RouteMeta -): RouteMeta { + ctx: TravesalContext +) { + ctx.currentIndex++; + const {routes} = currentRoute; if (ctx.route && !ctx.nextRoute) { @@ -78,6 +91,7 @@ function buildRouteMeta( if (currentRoute.path === searchPath) { ctx.route = currentRoute; + ctx.order = ctx.currentIndex; // If we've found a deeper match, reset the previously stored next route. // TODO: this only works reliably if deeper matches are first in the tree. // We should revamp all of this to be more explicit. @@ -89,14 +103,12 @@ function buildRouteMeta( } if (!routes) { - return ctx; + return; } for (const route of routes) { buildRouteMeta(searchPath, route, ctx); } - - return ctx; } // iterates the route tree from the current route to find its ancestors for breadcrumbs diff --git a/src/components/Seo.tsx b/src/components/Seo.tsx index 9ad1da0d..e6b82efd 100644 --- a/src/components/Seo.tsx +++ b/src/components/Seo.tsx @@ -13,6 +13,7 @@ export interface SeoProps { // jsonld?: JsonLDType | Array; children?: React.ReactNode; isHomePage: boolean; + searchOrder?: number; } export const Seo = withRouter( @@ -23,6 +24,7 @@ export const Seo = withRouter( router, children, isHomePage, + searchOrder, }: SeoProps & {router: Router}) => { const pageTitle = isHomePage ? 'React' : title + ' – React'; // Twitter's meta parser is not very good. @@ -96,6 +98,9 @@ export const Seo = withRouter( name="google-site-verification" content="sIlAGs48RulR4DdP95YSWNKZIEtCqQmRjzn-Zq-CcD0" /> + {searchOrder != null && ( + + )}