Automatically generate "Read Next" link

This commit is contained in:
Marcus Blättermann 2022-12-14 22:08:39 +01:00
parent 2a5d9a0d01
commit 8c61ddef76
No known key found for this signature in database
GPG Key ID: A1E1F04008AC450D
5 changed files with 48 additions and 18 deletions

View File

@ -0,0 +1,5 @@
import models from './languages.json'
export const languagesSorted = models.languages
.filter(({ models }) => models && models.length)
.sort((a, b) => a.name.localeCompare(b.name))

View File

@ -0,0 +1,5 @@
import sidebars from './sidebars.json'
export const sidebarUsageFlat = sidebars
.find((sidebar) => sidebar.section === 'usage')
.items.flatMap((item) => item.items)

View File

@ -7,6 +7,7 @@ import Layout from '../src/templates'
import remarkPlugins from '../plugins/index.mjs' import remarkPlugins from '../plugins/index.mjs'
import recordSection from '../meta/recordSections' import recordSection from '../meta/recordSections'
import { sidebarUsageFlat } from '../meta/sidebarFlat'
type ApiDetails = { type ApiDetails = {
stringName: string | null stringName: string | null
@ -117,16 +118,32 @@ export const getStaticProps: GetStaticProps<PropsPage, ParsedUrlQuery> = async (
trainable: mdx.frontmatter.api_trainable ?? null, trainable: mdx.frontmatter.api_trainable ?? null,
} }
const slug = `/${args.params.listPathPage.join('/')}`
const next =
section === 'usage'
? sidebarUsageFlat.find(
(item, index) =>
index > 0 && sidebarUsageFlat[index - 1].url === slug && item[0] === '/'
)
: undefined
return { return {
props: { props: {
...mdx.frontmatter, ...mdx.frontmatter,
slug: `/${args.params.listPathPage.join('/')}`, slug,
mdx, mdx,
sectionTitle: sectionMeta?.title ?? null, sectionTitle: sectionMeta?.title ?? null,
theme: sectionMeta?.theme ?? null, theme: sectionMeta?.theme ?? null,
section: section, section: section,
apiDetails: apiDetails, apiDetails: apiDetails,
isIndex, isIndex,
next: next
? {
slug: next.url,
title: next.text,
}
: null,
}, },
} }
} }

View File

@ -4,6 +4,7 @@ import recordSection from '../../meta/recordSections'
import recordLanguages from '../../meta/recordLanguages' import recordLanguages from '../../meta/recordLanguages'
import Layout from '../../src/templates' import Layout from '../../src/templates'
import { PropsPageBase } from '../[...listPathPage]' import { PropsPageBase } from '../[...listPathPage]'
import { languagesSorted } from '../../meta/languageSorted'
type PropsPageModel = PropsPageBase & { type PropsPageModel = PropsPageBase & {
next: { title: string; slug: string } | null next: { title: string; slug: string } | null
@ -31,23 +32,29 @@ export const getStaticProps: GetStaticProps<
slug: string slug: string
} }
> = async (args) => { > = async (args) => {
const getSlug = (languageCode: string) => `/${['models', languageCode].join('/')}`
if (args.params === undefined) { if (args.params === undefined) {
return { notFound: true } return { notFound: true }
} }
const language = recordLanguages[args.params.slug] const language = recordLanguages[args.params.slug]
const nextLanguage = languagesSorted.find(
(item, index) => index > 0 && languagesSorted[index - 1].code === language.code
)
return { return {
props: { props: {
id: language.code, id: language.code,
slug: `/${['models', language.code].join('/')}`, slug: getSlug(language.code),
isIndex: false, isIndex: false,
title: language.name, title: language.name,
section: 'models', section: 'models',
sectionTitle: recordSection.models.title, sectionTitle: recordSection.models.title,
theme: recordSection.models.theme, theme: recordSection.models.theme,
next: language.next next: nextLanguage
? { title: language.next.name, slug: `/models/${language.next.code}` } ? { title: nextLanguage.name, slug: getSlug(nextLanguage.code) }
: null, : null,
meta: { meta: {
models: language.models || null, models: language.models || null,

View File

@ -14,8 +14,8 @@ import { getCurrentSource, github } from '../components/util'
import siteMetadata from '../../meta/site.json' import siteMetadata from '../../meta/site.json'
import sidebars from '../../meta/sidebars.json' import sidebars from '../../meta/sidebars.json'
import models from '../../meta/languages.json'
import { nightly, legacy } from '../../meta/dynamicMeta.mjs' import { nightly, legacy } from '../../meta/dynamicMeta.mjs'
import { languagesSorted } from '../../meta/languageSorted'
const Docs = ({ pageContext, children }) => { const Docs = ({ pageContext, children }) => {
const { const {
@ -34,7 +34,6 @@ const Docs = ({ pageContext, children }) => {
apiDetails, apiDetails,
} = pageContext } = pageContext
const { modelsRepo } = siteMetadata const { modelsRepo } = siteMetadata
const { languages } = models
const isModels = section === 'models' const isModels = section === 'models'
const sidebar = pageContext.sidebar const sidebar = pageContext.sidebar
? { items: pageContext.sidebar } ? { items: pageContext.sidebar }
@ -42,18 +41,15 @@ const Docs = ({ pageContext, children }) => {
let pageMenu = menu ? menu.map(([text, id]) => ({ text, id })) : [] let pageMenu = menu ? menu.map(([text, id]) => ({ text, id })) : []
if (isModels) { if (isModels) {
sidebar.items[1].items = languages sidebar.items[1].items = languagesSorted.map((lang) => ({
.filter(({ models }) => models && models.length) text: lang.name,
.sort((a, b) => a.name.localeCompare(b.name)) url: `/models/${lang.code}`,
.map((lang) => ({ isActive: id === lang.code,
text: lang.name, menu: lang.models.map((model) => ({
url: `/models/${lang.code}`, text: model,
isActive: id === lang.code, id: model,
menu: lang.models.map((model) => ({ })),
text: model, }))
id: model,
})),
}))
} }
const sourcePath = source ? github(source) : null const sourcePath = source ? github(source) : null
const currentSource = getCurrentSource(slug, isIndex) const currentSource = getCurrentSource(slug, isIndex)