Skip to content

Commit

Permalink
Merge pull request #536 from iAdramelk/gf-fix#392
Browse files Browse the repository at this point in the history
Update links to subcommands
  • Loading branch information
shcheklein authored Aug 9, 2019
2 parents f40150b + 15cc034 commit 967a5af
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/Documentation/Markdown/Markdown.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Component } from 'react'
import React from 'react'
// components
import ReactMarkdown from 'react-markdown'
import { LightButton } from '../LightButton'
Expand Down Expand Up @@ -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
Expand Down
36 changes: 27 additions & 9 deletions src/Documentation/Markdown/utils/remark-linker.js
Original file line number Diff line number Diff line change
@@ -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
}
}
}
})
Expand Down

0 comments on commit 967a5af

Please sign in to comment.