Skip to content

Commit becced4

Browse files
dcavalcanteovflowd
andauthored
feat: restore previous and next article navigation (#150)
* feat: add previous and next article navigation * test: cover article navigation edge cases --------- Co-authored-by: Claudio Wunder <cwunder@gnome.org>
1 parent 2508ff4 commit becced4

4 files changed

Lines changed: 200 additions & 22 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import CrossLink from '@node-core/ui-components/Common/BaseCrossLink';
2+
3+
import { sidebar } from '../../site.json' with { type: 'json' };
4+
5+
import styles from './index.module.css';
6+
7+
const flattenItems = items =>
8+
items.flatMap(item =>
9+
item.items?.length ? flattenItems(item.items) : [item]
10+
);
11+
12+
const articles = sidebar.flatMap(({ items }) => flattenItems(items));
13+
14+
/**
15+
* Previous/next article navigation following the same order as the sidebar.
16+
*
17+
* @param {{ pathname: string }} props
18+
*/
19+
const ArticleNavigation = ({ pathname }) => {
20+
const currentIndex = articles.findIndex(({ link }) => link === pathname);
21+
22+
if (currentIndex === -1) {
23+
return null;
24+
}
25+
26+
const previous = articles[currentIndex - 1];
27+
const next = articles[currentIndex + 1];
28+
29+
if (!previous && !next) {
30+
return null;
31+
}
32+
33+
return (
34+
<nav className={styles.crossLinks} aria-label="Article navigation">
35+
{(previous && (
36+
<CrossLink
37+
type="previous"
38+
label="Previous"
39+
text={previous.label}
40+
link={previous.link}
41+
/>
42+
)) || <div />}
43+
44+
{next && (
45+
<CrossLink
46+
type="next"
47+
label="Next"
48+
text={next.label}
49+
link={next.link}
50+
/>
51+
)}
52+
</nav>
53+
);
54+
};
55+
56+
export default ArticleNavigation;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.crossLinks {
2+
margin-top: 1rem;
3+
display: grid;
4+
width: 100%;
5+
grid-template-columns: repeat(2, minmax(0, 1fr));
6+
gap: 1rem;
7+
}
8+
9+
@media (max-width: 767px) {
10+
.crossLinks {
11+
grid-template-columns: minmax(0, 1fr);
12+
}
13+
14+
.crossLinks > :empty {
15+
display: none;
16+
}
17+
}

‎components/Layout/index.jsx‎

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import NavBar from '../Navigation';
88
import MetaBar from '../Metabar';
99
import SideBar from '../Sidebar';
1010
import Footer from '../Footer';
11+
import ArticleNavigation from '../ArticleNavigation';
1112

1213
/**
1314
* @typedef {Object} Props
@@ -20,27 +21,32 @@ import Footer from '../Footer';
2021
/**
2122
* @param {Props} props
2223
*/
23-
export default ({ metadata, headings, readingTime, children }) => (
24-
<>
25-
<Banner />
26-
<Analytics basePath="/learn/_vercel" />
27-
<SpeedInsights basePath="/learn/_vercel" />
28-
<NavBar metadata={metadata} />
29-
<Article>
30-
<SideBar pathname={`/learn${metadata.path.replace('/index', '')}`} />
31-
<div>
24+
export default ({ metadata, headings, readingTime, children }) => {
25+
const pathname = `/learn${metadata.path.replace('/index', '')}`;
26+
27+
return (
28+
<>
29+
<Banner />
30+
<Analytics basePath="/learn/_vercel" />
31+
<SpeedInsights basePath="/learn/_vercel" />
32+
<NavBar metadata={metadata} />
33+
<Article>
34+
<SideBar pathname={pathname} />
3235
<div>
33-
<TableOfContents headings={headings} summaryTitle="On this page" />
34-
<br />
35-
<main>{children}</main>
36+
<div>
37+
<TableOfContents headings={headings} summaryTitle="On this page" />
38+
<br />
39+
<main>{children}</main>
40+
<ArticleNavigation pathname={pathname} />
41+
</div>
42+
<MetaBar
43+
metadata={metadata}
44+
headings={headings}
45+
readingTime={readingTime}
46+
/>
3647
</div>
37-
<MetaBar
38-
metadata={metadata}
39-
headings={headings}
40-
readingTime={readingTime}
41-
/>
42-
</div>
43-
</Article>
44-
<Footer metadata={metadata} />
45-
</>
46-
);
48+
</Article>
49+
<Footer metadata={metadata} />
50+
</>
51+
);
52+
};

‎tests/e2e/interactions.spec.mjs‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { test, expect } from '@playwright/test';
33
import { waitForIsland } from './helpers.mjs';
44

55
const ARTICLE = '/learn/getting-started/introduction-to-nodejs';
6+
const EVENT_LOOP_ARTICLE =
7+
'/learn/asynchronous-work/event-loop-timers-and-nexttick';
68

79
/**
810
* The theme is applied in an effect after the menu item is clicked, so use a
@@ -54,6 +56,63 @@ test.describe('Navigation', () => {
5456
await expect(page.getByRole('heading', { level: 1 })).toBeVisible();
5557
});
5658

59+
test('renders previous and next article links in sidebar order', async ({
60+
page,
61+
}) => {
62+
await page.goto(EVENT_LOOP_ARTICLE);
63+
64+
const navigation = page.getByRole('navigation', {
65+
name: 'Article navigation',
66+
});
67+
const previous = navigation.getByRole('link', { name: /Previous/ });
68+
const next = navigation.getByRole('link', { name: /Next/ });
69+
70+
await expect(previous).toContainText(
71+
'Overview of Blocking vs Non-Blocking'
72+
);
73+
await expect(previous).toHaveAttribute(
74+
'href',
75+
'/learn/asynchronous-work/overview-of-blocking-vs-non-blocking'
76+
);
77+
await expect(next).toContainText('The Node.js Event Emitter');
78+
await expect(next).toHaveAttribute(
79+
'href',
80+
'/learn/asynchronous-work/the-nodejs-event-emitter'
81+
);
82+
});
83+
84+
test('continues article navigation across sidebar groups', async ({
85+
page,
86+
}) => {
87+
await page.goto('/learn/asynchronous-work/dont-block-the-event-loop');
88+
89+
const navigation = page.getByRole('navigation', {
90+
name: 'Article navigation',
91+
});
92+
const next = navigation.getByRole('link', { name: /Next/ });
93+
94+
await expect(next).toContainText('Comparing Node.js concurrency models');
95+
await expect(next).toHaveAttribute(
96+
'href',
97+
'/learn/concurrency/comparing-nodejs-concurrency-models'
98+
);
99+
});
100+
101+
test('uses leaf article order for nested sidebar sections', async ({
102+
page,
103+
}) => {
104+
await page.goto('/learn/diagnostics/memory');
105+
106+
const navigation = page.getByRole('navigation', {
107+
name: 'Article navigation',
108+
});
109+
const previous = navigation.getByRole('link', { name: /Previous/ });
110+
const next = navigation.getByRole('link', { name: /Next/ });
111+
112+
await expect(previous).toContainText('User Journey');
113+
await expect(next).toContainText('Understanding and Tuning Memory');
114+
});
115+
57116
test('unknown pages respond with 404', async ({ page }) => {
58117
// Only the status is checked: which 404 page renders depends on the host
59118
// (nodejs.org proxies /learn, previews and `serve` use their own).
@@ -124,6 +183,46 @@ test.describe('Small screens', () => {
124183
await expect(page).toHaveURL(new RegExp(`${ARTICLE}$`));
125184
});
126185

186+
test('the first article shows next without an empty mobile slot', async ({
187+
page,
188+
}) => {
189+
await page.goto(ARTICLE);
190+
191+
const navigation = page.getByRole('navigation', {
192+
name: 'Article navigation',
193+
});
194+
await navigation.scrollIntoViewIfNeeded();
195+
196+
await expect(
197+
navigation.getByRole('link', { name: /Previous/ })
198+
).toHaveCount(0);
199+
await expect(navigation.getByRole('link', { name: /Next/ })).toContainText(
200+
'How much JavaScript do you need to know to use Node.js?'
201+
);
202+
await expect(navigation.locator(':scope > div:empty')).toBeHidden();
203+
});
204+
205+
test('article navigation moves to the next article from the bottom', async ({
206+
page,
207+
}) => {
208+
await page.goto(EVENT_LOOP_ARTICLE);
209+
210+
const navigation = page.getByRole('navigation', {
211+
name: 'Article navigation',
212+
});
213+
await navigation.scrollIntoViewIfNeeded();
214+
215+
const next = navigation.getByRole('link', { name: /Next/ });
216+
await expect(next).toBeVisible();
217+
await expect(next).toContainText('The Node.js Event Emitter');
218+
219+
await next.click();
220+
221+
await expect(page).toHaveURL(
222+
/\/learn\/asynchronous-work\/the-nodejs-event-emitter$/
223+
);
224+
});
225+
127226
test('the menu button reveals the site links', async ({ page }) => {
128227
await page.goto(ARTICLE);
129228

0 commit comments

Comments
 (0)