diff --git a/src/Documentation/Markdown/Markdown.js b/src/Documentation/Markdown/Markdown.js index 30ee934449..fc759c8b73 100644 --- a/src/Documentation/Markdown/Markdown.js +++ b/src/Documentation/Markdown/Markdown.js @@ -1,4 +1,4 @@ -import React, { Component } from 'react' +import React from 'react' // components import ReactMarkdown from 'react-markdown' import { LightButton } from '../LightButton' @@ -75,7 +75,7 @@ const CodeBlock = ({ value, language }) => { ) } -export default class Markdown extends Component { +export default class Markdown extends React.PureComponent { constructor() { super() this.touchstartX = 0 diff --git a/src/Documentation/Markdown/utils/remark-linker.js b/src/Documentation/Markdown/utils/remark-linker.js index c0b14b2cc4..8b9ccfec0b 100644 --- a/src/Documentation/Markdown/utils/remark-linker.js +++ b/src/Documentation/Markdown/utils/remark-linker.js @@ -1,23 +1,41 @@ ;`use strict` import visit from 'unist-util-visit' +import { getItemByPath } from '../../SidebarMenu/helper' + +const DVC_REGEXP = /dvc\s+[a-z][a-z-.]*/ +const COMMAND_REGEXP = /^[a-z][a-z-]*$/ +const COMMAND_ROOT = '/doc/commands-reference/' function linker() { function transformer(tree) { visit(tree, 'inlineCode', function(node, index, parent) { - if (parent.type !== 'link' && /dvc\s+[a-z-.]+/.test(node.value)) { + if (parent.type !== 'link' && DVC_REGEXP.test(node.value)) { let parts = node.value.split(/\s+/) - let url = '/doc/commands-reference/' + parts[1] + let url + + const hasThirdSegment = parts[2] && COMMAND_REGEXP.test(parts[2]) + const isCommandPageExists = getItemByPath(`${COMMAND_ROOT}${parts[1]}`) + const isSubcommandPageExists = + isCommandPageExists && + hasThirdSegment && + getItemByPath(`${COMMAND_ROOT}${parts[1]}/${parts[2]}`) - if (parts.length > 2) { - url += '#' + parts[2] + if (isSubcommandPageExists) { + url = `${COMMAND_ROOT}${parts[1]}/${parts[2]}` + } else if (isCommandPageExists && hasThirdSegment) { + url = `${COMMAND_ROOT}${parts[1]}#${parts[2]}` + } else if (isCommandPageExists) { + url = `${COMMAND_ROOT}${parts[1]}` } - parent.children[index] = { - type: 'link', - url: url, - children: [node], - position: node.position + if (url) { + parent.children[index] = { + type: 'link', + url: url, + children: [node], + position: node.position + } } } })