Skip to content

Commit cad6874

Browse files
committed
update subpage back button logic
1 parent 6f69d9e commit cad6874

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

packages/dev/s2-docs/src/Layout.tsx

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,13 @@ const components = {
6464
ExampleList
6565
};
6666

67-
const subPageComponents = (previousPage?: Page) => ({
67+
const subPageComponents = (previousPage?: Page, href?: string) => ({
6868
...components,
6969
h1: ({children, ...props}) => (
7070
<div className={style({display: 'flex', flexDirection: 'column', gap: 4})}>
7171
<div className={style({display: 'flex', alignItems: 'center', gap: 8})}>
72-
<TitleLink href="./index.html">{previousPage?.exports?.title}</TitleLink>
72+
{/* see title in Layout.tsx for this logic for extracting the title of a page */}
73+
<TitleLink href={href ?? './index.html'}>{previousPage?.exports?.title ?? previousPage?.tableOfContents?.[0]?.title ?? previousPage?.name}</TitleLink>
7374
<ChevronRightIcon styles={iconStyle({size: 'M'})} />
7475
</div>
7576
<h1 {...props} id="top" style={{'--width-per-em': getTextWidth(children)} as any} className={h1}>{children}</h1>
@@ -148,9 +149,27 @@ export function Layout(props: PageProps & {children: ReactElement<any>}) {
148149
let title = getTitle(currentPage);
149150
let description = getDescription(currentPage);
150151
let isSubpage = currentPage.exports?.isSubpage;
151-
let parentPage = pages.find(p => {
152-
return p.url === currentPage.url.replace(/\/[^/]+\.html$/, '/index.html');
153-
});
152+
153+
let parentUrl;
154+
let parentPage;
155+
if (isSubpage) {
156+
let pathParts = currentPage.url.split('/');
157+
let fileName = pathParts.pop();
158+
159+
if (fileName === 'testing.html') {
160+
// for testing pages like /CheckboxGroup/testing.html, parent is /CheckboxGroup.html
161+
let parentDir = pathParts.pop();
162+
parentUrl = `../${parentDir}.html`;
163+
164+
let parentPageUrl = pathParts.join('/') + `/${parentDir}.html`;
165+
parentPage = pages.find(p => p.url === parentPageUrl);
166+
} else {
167+
// for release subpages like releases/2024-01-15.html, parent is just the same but with the end replaced with index.html
168+
parentUrl = './index.html';
169+
let parentIndexUrl = pathParts.join('/') + '/index.html';
170+
parentPage = pages.find(p => p.url === parentIndexUrl);
171+
}
172+
}
154173

155174
return (
156175
<Provider elementType="html" locale="en" background="layer-1" styles={style({scrollPaddingTop: {default: 64, lg: 0}})}>
@@ -264,7 +283,7 @@ export function Layout(props: PageProps & {children: ReactElement<any>}) {
264283
{currentPage.exports?.version && <VersionBadge version={currentPage.exports.version} />}
265284
{React.cloneElement(children, {
266285
components: isSubpage ?
267-
subPageComponents(parentPage) :
286+
subPageComponents(parentPage, parentUrl) :
268287
components,
269288
pages
270289
})}

0 commit comments

Comments
 (0)