Skip to content

Commit

Permalink
Fix bug with wrong 2-segment link validation
Browse files Browse the repository at this point in the history
  • Loading branch information
iAdramelk committed Aug 9, 2019
1 parent 721e478 commit 1d1b9e8
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/Documentation/Markdown/utils/remark-linker.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,30 @@
import visit from 'unist-util-visit'
import { getItemByPath } from '../../SidebarMenu/helper'

const DVC_REGEXP = /dvc\s+[a-z][a-z-.]*\s/
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 tmpUrl = '/doc/commands-reference/'
let url

if (getItemByPath(tmpUrl + parts[1])) {
url = tmpUrl = tmpUrl + parts[1]
}

if (parts.length > 2) {
if (getItemByPath(tmpUrl + parts[2])) {
url += '/' + parts[2]
} else {
url += '#' + parts[2]
}
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 (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]}`
}

if (url) {
Expand Down

0 comments on commit 1d1b9e8

Please sign in to comment.