From 19f7f30fe3bee7034327506ed11c34edcadd734a Mon Sep 17 00:00:00 2001 From: Yugo Ogura Date: Sun, 26 Jul 2020 13:24:59 +0900 Subject: [PATCH] feat: hide next/prev links when themeConfig expressly set false --- src/node/server.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/node/server.ts b/src/node/server.ts index bdaaa5ed..644d041a 100644 --- a/src/node/server.ts +++ b/src/node/server.ts @@ -106,7 +106,7 @@ function createVitePressPlugin({ const pageDataWithLinks = { ...pageData, - ...getNextAndPrev(siteData.themeConfig.sidebar, ctx.path) + ...getNextAndPrev(siteData.themeConfig, ctx.path) } await next() @@ -131,7 +131,11 @@ function createVitePressPlugin({ } } -function getNextAndPrev(sidebar: { [key: string]: any }, pagePath: string) { +function getNextAndPrev(themeConfig: any, pagePath: string) { + if (!themeConfig.sidebar) { + return + } + const sidebar = themeConfig.sidebar let candidates: { text: string; link: string }[] = [] Object.keys(sidebar).forEach((k) => { if (!pagePath.startsWith(k)) { @@ -149,12 +153,14 @@ function getNextAndPrev(sidebar: { [key: string]: any }, pagePath: string) { const path = pagePath.replace(/\.(md|html)$/, '') const currentLinkIndex = candidates.findIndex((v) => v.link === path) + const hideNextLink = themeConfig.nextLinks === false + const hidePrevLink = themeConfig.prevLinks === false return { - ...(currentLinkIndex !== -1 + ...(currentLinkIndex !== -1 && !hideNextLink ? { next: candidates[currentLinkIndex + 1] } : {}), - ...(currentLinkIndex !== -1 + ...(currentLinkIndex !== -1 && !hidePrevLink ? { prev: candidates[currentLinkIndex - 1] } : {}) }