Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update links to subcommands #536

Merged
merged 4 commits into from
Aug 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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