diff --git a/.gitignore b/.gitignore index da19b8a667cff2ef1aa963958872fde7c4cfb3b7..7011910bcc09a177a337782ef6b4671280cf0905 100644 --- a/.gitignore +++ b/.gitignore @@ -59,6 +59,7 @@ typings/ .cache/ .history public +product_test # Mac files .DS_Store diff --git a/gatsby-node.js b/gatsby-node.js index 65545a1aec748985ad7e13378e4e2e060962035d..0622986b5739062a427988a0d4aaded8e2aa098f 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -1,6 +1,6 @@ const path = require('path') const { remarkSyntaxDiagram } = require('./src/utils/remarkSyntaxDiagram') -const { allProductVersions } = require('./src/utils/version_config') +const { allProductVersions, convertVersionName } = require('./src/utils/version_config') // 获取已上线产品 const allProductKeys = Object.keys(allProductVersions).filter(product => allProductVersions[product]) @@ -13,6 +13,7 @@ exports.onCreateNode = ({ node, actions, getNode }) => { const lang = relativePath.split('/')[0] const docType = relativePath.split('/')[1].substring(5, relativePath.split('/')[1].length) const version = relativePath.split('/')[2] + const urlVersion = convertVersionName(docType, version) const fileName = relativePath.substr(relativePath.lastIndexOf('/') + 1, relativePath.length).replace('.md', '') createNodeField({ @@ -25,7 +26,14 @@ exports.onCreateNode = ({ node, actions, getNode }) => { createNodeField({ node, name: 'slug', - value: `/${lang}/${docType}/${version}/${fileName}`, + value: `/${lang}/${docType}/${urlVersion}/${fileName}`, + }) + + // 文档类型,如mtk mogdb ha + createNodeField({ + node, + name: 'redirectUrl', + value: urlVersion === 'latest' ? `/${lang}/${docType}/${version}/${fileName}` : '', }) // 文件语言,如en @@ -68,6 +76,7 @@ exports.createPages = async ({ graphql, actions }) => { fields { docType fileName + redirectUrl lang slug version @@ -96,7 +105,8 @@ exports.createPages = async ({ graphql, actions }) => { // 创建文档页面 const allDocsNodes = docs.data.allMarkdownRemark.nodes allDocsNodes.forEach((node) => { - const { slug, lang, version, docType, fileName, relativePath } = node.fields + const { slug, lang, version, redirectUrl, docType, fileName, relativePath } = node.fields + const urlVersion = convertVersionName(docType, version) // 处理语法树 const domStr = node.rawMarkdownBody @@ -112,69 +122,63 @@ exports.createPages = async ({ graphql, actions }) => { diagramTree.push(remarkSyntaxDiagram(codeNode, slug)) }) } - + createPage({ path: slug, component: path.resolve('./src/templates/doc.js'), context: { - tocPath: `/${lang}/${docType}/${version}/toc`, + tocPath: `/${lang}/${docType}/${urlVersion}/toc`, slug, lang, version, docType, fileName, relativePath, diagramTree } }) - // 判断是否为最新版本,如果是创建最新版本URL - const pLatestVersion = allProductVersions[docType].latestVersion - if ( pLatestVersion === version ) { - createPageRedirect(`/${lang}/${docType}/latest/${fileName}`, `/${lang}/${docType}/${pLatestVersion}/${fileName}`) + // create redirects + if (redirectUrl) { + createPageRedirect(redirectUrl, slug) } // 处理中文版本存在,英文版本不存在的情况,创建中文页面 if (lang === 'zh') { - const enPath = `/en/${docType}/${version}/${fileName}` + const enPath = `/en/${docType}/${urlVersion}/${fileName}` const isExits = allDocsNodes.find(item => item.fields.slug === enPath) if (!isExits) { createPage({ path: enPath, component: path.resolve('./src/templates/doc.js'), context: { - tocPath: `/en/${docType}/${version}/toc`, + tocPath: `/en/${docType}/${urlVersion}/toc`, slug, lang: 'en', realSlug: enPath, version, docType, fileName, relativePath, diagramTree } }) - // 同时创建最新版本URL - if ( pLatestVersion === version ) { - createPageRedirect(`/en/${docType}/latest/${fileName}`, enPath) - } } } }) - // 创建首页面重定向 ,/ /zh /en 等URL,重定向到mogdb产品最新版本的首页 - const mogdbLatestVersion = allProductVersions.mogdb.latestVersion - createPageRedirect(`/zh`, `/zh/mogdb/${mogdbLatestVersion}/overview`) - createPageRedirect(`/zh/`, `/zh/mogdb/${mogdbLatestVersion}/overview`) - createPageRedirect(`/zh/mogdb/latest`, `/zh/mogdb/${mogdbLatestVersion}/overview`) - createPageRedirect(`/en`, `/en/mogdb/${mogdbLatestVersion}/overview`) - createPageRedirect(`/en/`, `/en/mogdb/${mogdbLatestVersion}/overview`) - createPageRedirect(`/en/mogdb/latest`, `/en/mogdb/${mogdbLatestVersion}/overview`) + createPageRedirect(`/zh`, `/zh/mogdb/latest/overview`) + createPageRedirect(`/zh/`, `/zh/mogdb/latest/overview`) + createPageRedirect(`/zh/mogdb/latest`, `/zh/mogdb/latest/overview`) + createPageRedirect(`/en`, `/en/mogdb/latest/overview`) + createPageRedirect(`/en/`, `/en/mogdb/latest/overview`) + createPageRedirect(`/en/mogdb/latest`, `/en/mogdb/latest/overview`) // 创建版本首页重定向 如:/zh/mogdb 重定向到最新版本的首页 allProductKeys.forEach(p => { - const { latestVersion, versions } = allProductVersions[p] - createPageRedirect(`/zh/${p}`, `/zh/${p}/${latestVersion}/overview`) - createPageRedirect(`/en/${p}`, `/en/${p}/${latestVersion}/overview`) - createPageRedirect(`/zh/${p}/`, `/zh/${p}/${latestVersion}/overview`) - createPageRedirect(`/en/${p}/`, `/en/${p}/${latestVersion}/overview`) + const { versions } = allProductVersions[p] + createPageRedirect(`/zh/${p}`, `/zh/${p}/latest/overview`) + createPageRedirect(`/en/${p}`, `/en/${p}/latest/overview`) + createPageRedirect(`/zh/${p}/`, `/zh/${p}/latest/overview`) + createPageRedirect(`/en/${p}/`, `/en/${p}/latest/overview`) const productAllVersions = Object.keys(versions).filter(v => !versions[v].disabled) productAllVersions.forEach(v => { - createPageRedirect(`/zh/${p}/${v}`, `/zh/${p}/${v}/overview`) - createPageRedirect(`/en/${p}/${v}`, `/en/${p}/${v}/overview`) - createPageRedirect(`/zh/${p}/${v}/`, `/zh/${p}/${v}/overview`) - createPageRedirect(`/en/${p}/${v}/`, `/en/${p}/${v}/overview`) + const urlVersion = convertVersionName(p, v) + createPageRedirect(`/zh/${p}/${urlVersion}`, `/zh/${p}/${urlVersion}/overview`) + createPageRedirect(`/en/${p}/${urlVersion}`, `/en/${p}/${urlVersion}/overview`) + createPageRedirect(`/zh/${p}/${urlVersion}/`, `/zh/${p}/${urlVersion}/overview`) + createPageRedirect(`/en/${p}/${urlVersion}/`, `/en/${p}/${urlVersion}/overview`) }) }) -} \ No newline at end of file +} diff --git a/src/pages/404.js b/src/pages/404.js index 4d3bfad722cc34de8132549c06478789f93ba932..adb89b6c7152afef78706dac017b539726cd07e8 100644 --- a/src/pages/404.js +++ b/src/pages/404.js @@ -1,6 +1,7 @@ import * as React from "react" -import { Link } from 'gatsby' -import '../styles/notfound.scss' +import { Link } from "gatsby" +import "../styles/notfound.scss" + const ErrorPage = () => { return (
diff --git a/src/templates/components/docDirectoryMenu.js b/src/templates/components/docDirectoryMenu.js index d7a89cc13ed613ff8e9fe1ceb7c4f7788a1697b0..209f77391647f5cf38e981e6ef6198de4c8bff09 100644 --- a/src/templates/components/docDirectoryMenu.js +++ b/src/templates/components/docDirectoryMenu.js @@ -5,7 +5,7 @@ import { FormattedMessage } from 'react-intl' // 文档目录菜单 const DocDirectoryMenu = ({ tableOfContents, pageContext }) => { - const { lang, slug, relativePath, realSlug } = pageContext + const { slug, relativePath, realSlug } = pageContext const docMenu = tableOfContents.replace(/\/#/g, '#') const [scrollTop, setScrollTop] = useState(0) const issueHref = `https://gitee.com/enmotech/mogdb-docs/issues/new?&description=原始文件:[/${relativePath}](https://docs.mogdb.io${realSlug || slug})` @@ -70,4 +70,4 @@ const DocDirectoryMenu = ({ tableOfContents, pageContext }) => { ) } -export default DocDirectoryMenu +export default DocDirectoryMenu \ No newline at end of file diff --git a/src/templates/components/docPageMenu.js b/src/templates/components/docPageMenu.js index fa41140cb4e3506901c59ce593e62ee771a01d8a..713828bc6664581a954a28e40bcac97849940ca4 100644 --- a/src/templates/components/docPageMenu.js +++ b/src/templates/components/docPageMenu.js @@ -1,8 +1,10 @@ import * as React from "react" +import { convertVersionName } from '../../utils/version_config' // 文档底部上一页下一页切换菜单 const DocPageMenu = ({ tocNodes, pageContext }) => { const { lang, version, docType, realSlug, slug } = pageContext || {} + const urlVersion = convertVersionName(docType, version) const pageMenuArr = [] @@ -11,7 +13,7 @@ const DocPageMenu = ({ tocNodes, pageContext }) => { tocList.push(name) }) - const urlPrefix = `/${lang}/${docType}/${version}/` + const urlPrefix = `/${lang}/${docType}/${urlVersion}/` const pageSlug = realSlug || slug const activeIdx = tocList.findIndex(i => urlPrefix + i === pageSlug) @@ -47,3 +49,4 @@ const DocPageMenu = ({ tocNodes, pageContext }) => { ) } export default DocPageMenu + diff --git a/src/templates/components/docToc.js b/src/templates/components/docToc.js index 00a16d4bba4a0b91f5060b5740453354cb9ffeb8..19b9e070b48a773745fea1568b7ef86484e5bd8c 100644 --- a/src/templates/components/docToc.js +++ b/src/templates/components/docToc.js @@ -4,13 +4,15 @@ import '../../styles/components/toc.scss' import React, { useRef, useLayoutEffect } from "react" import { remark } from 'remark' import html from './remark-html' +import { convertVersionName } from '../../utils/version_config' // 版本切换 const DocToc = (props) => { const { tocNodes, pageContext } = props; const { lang, version, docType, slug, realSlug } = pageContext + const urlVersion = convertVersionName(docType, version) const tocRef = useRef(null) - const tocLinkPrefix = `/${lang}/${docType}/${version}/` + const tocLinkPrefix = `/${lang}/${docType}/${urlVersion}/` const _html = remark() .use(html, { sanitize: true, tocLinkPrefix, slug: realSlug || slug }) .processSync(tocNodes) @@ -64,3 +66,4 @@ const DocToc = (props) => { ) } export default DocToc + diff --git a/src/templates/components/productVersion.js b/src/templates/components/productVersion.js index a8562b149680a78dc2d659fb09b37ce26da61cc4..2a7487c63911dbae7b63867484e4c040b4369678 100644 --- a/src/templates/components/productVersion.js +++ b/src/templates/components/productVersion.js @@ -7,7 +7,9 @@ const { Option } = Select // 版本切换 const ProductVersion = ({ pageContext }) => { - const { lang, version, docType } = pageContext || {} + const { lang, version: urlVersion, docType } = pageContext || {} + const latestVersion = allProductVersions[docType].latestVersion + const version = urlVersion === 'latest' ? latestVersion : urlVersion let productVersions = (allProductVersions[docType] || {}).versions productVersions = Object.values(productVersions).filter(v => !v.disabled) @@ -23,4 +25,4 @@ const ProductVersion = ({ pageContext }) => { ) } -export default ProductVersion +export default ProductVersion \ No newline at end of file diff --git a/src/utils/version_config.js b/src/utils/version_config.js index 5ad9a894f4d663804d949d07066946d31c2c96a6..c64d6a0842a21754947afa993b9f75aabab617f9 100644 --- a/src/utils/version_config.js +++ b/src/utils/version_config.js @@ -135,6 +135,15 @@ const allProductVersions = { } } +// 获取产品URL中的版本 +function convertVersionName (product, version) { + const latestVersion = allProductVersions[product].latestVersion + if (version === latestVersion) return 'latest' + return version +} + module.exports = { - allProductVersions + allProductVersions, + convertVersionName } +