-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* docs: api markdown code linker * docs: api and command markdown code tests * docs: manual links to api method pages removed * docs: codeclimate fixes * docs: test fixes * docs-910: helpers refactoring * docs-910: bringing back formatting * docs-910: indent fix and redundant url removal
- Loading branch information
Showing
13 changed files
with
732 additions
and
563 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,7 @@ typings/ | |
*.log | ||
.idea | ||
.vscode | ||
.history | ||
|
||
# Mac finder artifacts | ||
.DS_Store | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* eslint-env node */ | ||
|
||
const { createLinkNode } = require('./helpers') | ||
const { getItemByPath } = require('../../src/utils/shared/sidebar') | ||
|
||
const DVC_API_REGEXP = /dvc.api([a-z-._]*\(\)$)?/ | ||
const METHOD_REGEXP = /^[a-z-._]*\(\)$/ | ||
const API_ROOT = '/doc/api-reference/' | ||
|
||
module.exports = astNode => { | ||
const node = astNode[0] | ||
const parent = astNode[2] | ||
|
||
if (parent.type !== 'link' && DVC_API_REGEXP.test(node.value)) { | ||
const parts = node.value.split('.') | ||
let url | ||
|
||
const isMethod = parts[2] && METHOD_REGEXP.test(parts[2]) | ||
const method = isMethod && parts[2].slice(0, -2) | ||
const isRoot = parts[0] === 'dvc' && parts[1] === 'api' && !parts[2] | ||
|
||
if (isRoot) { | ||
url = `${API_ROOT}` | ||
} else { | ||
url = `${API_ROOT}${method}` | ||
} | ||
|
||
const isMethodPageExists = getItemByPath(url) | ||
if (isMethodPageExists) { | ||
createLinkNode(url, astNode) | ||
} | ||
} | ||
|
||
return astNode | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* eslint-env node */ | ||
|
||
const { createLinkNode } = require('./helpers') | ||
const { getItemByPath } = require('../../src/utils/shared/sidebar') | ||
|
||
const DVC_REGEXP = /dvc\s+[a-z][a-z-.]*/ | ||
const COMMAND_REGEXP = /^[a-z][a-z-]*$/ | ||
const COMMAND_ROOT = '/doc/command-reference/' | ||
|
||
module.exports = astNode => { | ||
const node = astNode[0] | ||
const parent = astNode[2] | ||
|
||
if (parent.type !== 'link' && DVC_REGEXP.test(node.value)) { | ||
const parts = node.value.split(/\s+/) | ||
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 (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]}` | ||
} | ||
|
||
createLinkNode(url, astNode) | ||
} | ||
|
||
return astNode | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const unified = require('unified') | ||
const remarkHtml = require('remark-html') | ||
const remarkParse = require('remark-parse') | ||
const removePosition = require('unist-util-remove-position') | ||
|
||
// We do not need to consider the position of the AST nodes | ||
const buildAst = mdToBuild => | ||
removePosition(unified().use(remarkHtml).use(remarkParse).parse(mdToBuild)) | ||
|
||
const createLinkNode = (url, [node, index, parent]) => | ||
url && | ||
(parent.children[index] = { | ||
type: 'link', | ||
url, | ||
title: null, | ||
children: [node], | ||
position: node.position | ||
}) | ||
|
||
module.exports = { buildAst, createLinkNode } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,19 @@ | ||
/* eslint-env node */ | ||
|
||
const flow = require('lodash/flow') | ||
const constant = require('lodash/constant') | ||
const visit = require('unist-util-visit') | ||
const { getItemByPath } = require('../../src/utils/shared/sidebar') | ||
|
||
const DVC_REGEXP = /dvc\s+[a-z][a-z-.]*/ | ||
const COMMAND_REGEXP = /^[a-z][a-z-]*$/ | ||
const COMMAND_ROOT = '/doc/command-reference/' | ||
const apiLinker = require('./apiLinker') | ||
const commandLinker = require('./commandLinker') | ||
|
||
// Lifting up the AST visitor in order not to repeat the | ||
// calculations times the amount of linkers we have | ||
module.exports = ({ markdownAST }) => { | ||
visit(markdownAST, 'inlineCode', function (node, index, parent) { | ||
if (parent.type !== 'link' && DVC_REGEXP.test(node.value)) { | ||
const parts = node.value.split(/\s+/) | ||
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 (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) { | ||
parent.children[index] = { | ||
type: 'link', | ||
url: url, | ||
children: [node], | ||
position: node.position | ||
} | ||
} | ||
} | ||
}) | ||
|
||
visit( | ||
markdownAST, | ||
'inlineCode', | ||
flow([Array, commandLinker, apiLinker, constant(undefined)]) | ||
) | ||
return markdownAST | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const flow = require('lodash/flow') | ||
const constant = require('lodash/constant') | ||
const visit = require('unist-util-visit') | ||
|
||
const gatsbyRemarkDvcLinker = require('.') | ||
const apiLinker = require('./apiLinker') | ||
const commandLinker = require('./commandLinker') | ||
|
||
const { buildAst } = require('./helpers') | ||
|
||
describe('gatsby-remark-dvc-linker', () => { | ||
api = { | ||
inlineCode: '`dvc.api.get_url()`', | ||
url: '[`dvc.api.get_url()`](/doc/api-reference/get_url)' | ||
} | ||
|
||
apiRoot = { | ||
inlineCode: '`dvc.api`', | ||
url: '[`dvc.api`](/doc/api-reference/)' | ||
} | ||
|
||
command = { | ||
inlineCode: '`dvc get`', | ||
url: '[`dvc get`](/doc/command-reference/get)' | ||
} | ||
|
||
it('composes apiLinker and commandLinker', () => { | ||
const ast = buildAst(`${api.inlineCode} ${command.inlineCode}`) | ||
gatsbyRemarkDvcLinker({ markdownAST: ast }) | ||
expect(ast).toEqual(buildAst(`${api.url} ${command.url}`)) | ||
}) | ||
|
||
describe('apiLinker', () => { | ||
it('transforms API reference to a link', () => { | ||
const ast = buildAst(api.inlineCode) | ||
visit(ast, 'inlineCode', flow([Array, apiLinker, constant(undefined)])) | ||
expect(ast).toEqual(buildAst(api.url)) | ||
}) | ||
|
||
it('transforms root API reference to a link', () => { | ||
const ast = buildAst(apiRoot.inlineCode) | ||
visit(ast, 'inlineCode', flow([Array, apiLinker, constant(undefined)])) | ||
expect(ast).toEqual(buildAst(apiRoot.url)) | ||
}) | ||
}) | ||
|
||
describe('commandLinker', () => { | ||
it('transforms command reference to a link', () => { | ||
const ast = buildAst(command.inlineCode) | ||
visit( | ||
ast, | ||
'inlineCode', | ||
flow([Array, commandLinker, constant(undefined)]) | ||
) | ||
expect(ast).toEqual(buildAst(command.url)) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.