From 79452ddc16f3021f371e6f6ce7079fad13b6f8a6 Mon Sep 17 00:00:00 2001 From: Marika Marszalkowski Date: Tue, 25 Oct 2022 12:03:14 +0200 Subject: [PATCH] Fix link to latest --- docusaurus.config.js | 7 +++++++ src/components/LatestApiDoc.js | 13 +++++++++++++ src/pages/api/index.js | 8 -------- src/pages/api/latest.mdx | 17 ---------------- src/plugins/plugin-latest-api-doc.js | 29 ++++++++++++++++++++++++++++ 5 files changed, 49 insertions(+), 25 deletions(-) create mode 100644 src/components/LatestApiDoc.js delete mode 100644 src/pages/api/index.js delete mode 100644 src/pages/api/latest.mdx create mode 100644 src/plugins/plugin-latest-api-doc.js diff --git a/docusaurus.config.js b/docusaurus.config.js index 63e2ffce1a9..3eecc24be09 100644 --- a/docusaurus.config.js +++ b/docusaurus.config.js @@ -222,6 +222,13 @@ module.exports = { ], customFields: {}, plugins: [ + [ + './src/plugins/plugin-latest-api-doc', + { + latestBaseUrl: '/cloud-sdk/api/latest', + versions: '../static/api/versions.json' + } + ], [ '@docusaurus/plugin-content-docs', { diff --git a/src/components/LatestApiDoc.js b/src/components/LatestApiDoc.js new file mode 100644 index 00000000000..d5d0cf107ff --- /dev/null +++ b/src/components/LatestApiDoc.js @@ -0,0 +1,13 @@ +import React from 'react'; +import { useLocation } from '@docusaurus/router'; +import BrowserOnly from '@docusaurus/BrowserOnly'; + +export default function LatestApiDoc({ latestBaseUrl, versions }) { + const explicitLatestBaseUrl = [ + ...latestBaseUrl.split('/').slice(0, -1), + versions[0] + ].join('/'); + const loc = useLocation(); + const newPath = loc.pathname.replace(latestBaseUrl, explicitLatestBaseUrl); + return {() => (window.location.href = newPath)}; +} diff --git a/src/pages/api/index.js b/src/pages/api/index.js deleted file mode 100644 index 5cf1c6b6c61..00000000000 --- a/src/pages/api/index.js +++ /dev/null @@ -1,8 +0,0 @@ -import React from 'react'; -import { Redirect } from '@docusaurus/router'; - -function Api() { - return ; -} - -export default Api; diff --git a/src/pages/api/latest.mdx b/src/pages/api/latest.mdx deleted file mode 100644 index 133943c0de6..00000000000 --- a/src/pages/api/latest.mdx +++ /dev/null @@ -1,17 +0,0 @@ ---- -title: Redirecting -keywords: - - sap - - cloud - - sdk - - cloud native - - cloud sdk - - sap cloud sdk ---- - -import BrowserOnly from '@docusaurus/BrowserOnly'; -import jsSdkVersions from '../../../static/api/versions'; - - - {() => (window.location.href = `/cloud-sdk/api/${jsSdkVersions[0]}/`)} - diff --git a/src/plugins/plugin-latest-api-doc.js b/src/plugins/plugin-latest-api-doc.js new file mode 100644 index 00000000000..965e8d39e08 --- /dev/null +++ b/src/plugins/plugin-latest-api-doc.js @@ -0,0 +1,29 @@ +/** + * @prop latestBaseUrl - Base URL for the latest version. Must not have a trailing slash. + * @prop versions - Reference to the `versions.json` file relative to the `.docusaurus` directory. + */ +async function apiDocPlugin(context, { latestBaseUrl, versions }) { + return { + name: 'sap-cloud-sdk-api-doc-plugin', + + async contentLoaded({ content, actions }) { + const { createData, addRoute } = actions; + const baseUrlJson = await createData( + 'latestBaseUrl.json', + `"${latestBaseUrl}"` + ); + + addRoute({ + path: `${latestBaseUrl}*`, + component: '@site/src/components/LatestApiDoc.js', + modules: { + latestBaseUrl: baseUrlJson, + versions + }, + exact: false + }); + } + }; +} + +module.exports = apiDocPlugin;