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 (