Skip to content

Commit c2b1c73

Browse files
committed
Add AnchorLink for smooth scrolling
1 parent 7a4ff26 commit c2b1c73

File tree

2 files changed

+57
-46
lines changed

2 files changed

+57
-46
lines changed

src/assets/css/_css/code.less

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
display: flex;
113113
flex-direction: row;
114114
align-items: flex-end;
115+
color: #000;
115116
.article_title {
116117
text-align: left;
117118
font-size: 24px;

src/pages/code.tsx

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { AnchorLink } from "gatsby-plugin-anchor-links"
12
import React from "react"
23
import Layout from "../components/Layout"
34
import Marked from "../components/Marked"
@@ -6,14 +7,16 @@ import { toSlug } from "../utils/slug"
67
export function buildLanguagesMenu(pageContext: any) {
78
let lastRow: string[]
89
const rows: string[][] = []
9-
pageContext.languageList.forEach(({ name: languageName }: any, index: number) => {
10-
if (index % 6 === 0) {
11-
lastRow = [languageName]
12-
rows.push(lastRow)
13-
} else {
14-
lastRow.push(languageName)
10+
pageContext.languageList.forEach(
11+
({ name: languageName }: any, index: number) => {
12+
if (index % 6 === 0) {
13+
lastRow = [languageName]
14+
rows.push(lastRow)
15+
} else {
16+
lastRow.push(languageName)
17+
}
1518
}
16-
})
19+
)
1720
return (
1821
<div>
1922
{rows.map(row => (
@@ -22,11 +25,13 @@ export function buildLanguagesMenu(pageContext: any) {
2225
{row.map(languageName => {
2326
const slug = toSlug(languageName)
2427
return (
25-
<div className="article language-box">
26-
<a href={`#${slug}`} className="article_title">
27-
{languageName}
28-
</a>
29-
</div>
28+
<AnchorLink
29+
to={`#${slug}`}
30+
className="article language-box"
31+
title={languageName}
32+
>
33+
<span className="article_title">{languageName}</span>
34+
</AnchorLink>
3035
)
3136
})}
3237
</div>
@@ -143,32 +148,35 @@ const categorySlugMap = [
143148
export function buildLanguagesContent(pageContext: any) {
144149
const elements = []
145150
for (const languageObj of pageContext.languageList) {
146-
const languageName = languageObj.name;
147-
const libraryCategories = languageObj.categoryMap;
151+
const languageName = languageObj.name
152+
const libraryCategories = languageObj.categoryMap
148153
const filteredCategorySlugMap = categorySlugMap.filter(
149154
([libraryCategoryName]) =>
150155
libraryCategories[libraryCategoryName as any]?.length
151-
);
152-
const languageSlug = toSlug(languageName);
156+
)
157+
const languageSlug = toSlug(languageName)
153158
elements.push(
154159
<div className="language-content" id={languageSlug}>
155160
<div className="language-header">
156161
<h2 className="language-title">{languageName}</h2>
157-
{filteredCategorySlugMap.length > 1 && <p className="language-categories-permalinks">
158-
{filteredCategorySlugMap.map(
159-
([libraryCategoryName, categorySlug], i) => (
160-
<>
161-
<a
162-
className="language-category-permalink"
163-
href={`#${languageSlug}-${categorySlug}`}
164-
>
165-
{libraryCategoryName}
166-
</a>
167-
{i < filteredCategorySlugMap.length - 1 && " / "}
168-
</>
169-
)
170-
)}
171-
</p>}
162+
{filteredCategorySlugMap.length > 1 && (
163+
<p className="language-categories-permalinks">
164+
{filteredCategorySlugMap.map(
165+
([libraryCategoryName, categorySlug], i) => (
166+
<>
167+
<AnchorLink
168+
title={`${languageSlug} ${categorySlug}`}
169+
className="language-category-permalink"
170+
to={`#${languageSlug}-${categorySlug}`}
171+
>
172+
{libraryCategoryName}
173+
</AnchorLink>
174+
{i < filteredCategorySlugMap.length - 1 && " / "}
175+
</>
176+
)
177+
)}
178+
</p>
179+
)}
172180
</div>
173181
<div className="library-categories">
174182
{filteredCategorySlugMap.map(([categoryName, categorySlug]) =>
@@ -207,47 +215,49 @@ export default ({ pageContext }: any) => {
207215
<div className="goto-section">
208216
<p>Go to</p>
209217
<div className="sections">
210-
<a href="#language-support">
218+
<AnchorLink to="#language-support" title="Language Support">
211219
<h3>Language Support</h3>
212-
</a>
213-
<a href="#generic-tools">
220+
</AnchorLink>
221+
<AnchorLink to="#generic-tools" title="Tools">
214222
<h3>Tools</h3>
215-
</a>
216-
<a href="#services">
223+
</AnchorLink>
224+
<AnchorLink to="#services" title="Services">
217225
<h3>Services</h3>
218-
</a>
219-
<a href="#more-stuff">
226+
</AnchorLink>
227+
<AnchorLink to="#more-stuff" title="More Stuff">
220228
<h3>More Stuff</h3>
221-
</a>
229+
</AnchorLink>
222230
</div>
223231
</div>
224232
</div>
225233

226-
<p className="languages-title">Language Support</p>
234+
<p id="language-support" className="languages-title">
235+
Language Support
236+
</p>
227237
{buildLanguagesMenu(pageContext)}
228238
{buildLanguagesContent(pageContext)}
229239
<h2>
230240
<a className="anchor" id="generic-tools"></a>
231241
Tools
232-
<a className="hash-link" href="#generic-tools">
242+
<AnchorLink className="hash-link" to="#generic-tools">
233243
#
234-
</a>
244+
</AnchorLink>
235245
</h2>
236246
{buildLibraryList(pageContext.otherLibraries.Tools, pageContext)}
237247
<h2>
238248
<a className="anchor" id="services"></a>
239249
Services
240-
<a className="hash-link" href="#services">
250+
<AnchorLink className="hash-link" to="#services">
241251
#
242-
</a>
252+
</AnchorLink>
243253
</h2>
244254
{buildLibraryList(pageContext.otherLibraries.Services, pageContext)}
245255
<h2>
246256
<a className="anchor" id="more-stuff"></a>
247257
More Stuff
248-
<a className="hash-link" href="#more-stuff">
258+
<AnchorLink className="hash-link" to="#more-stuff">
249259
#
250-
</a>
260+
</AnchorLink>
251261
</h2>
252262
{buildLibraryList(
253263
pageContext.otherLibraries["More Stuff"],

0 commit comments

Comments
 (0)