-
Notifications
You must be signed in to change notification settings - Fork 394
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
docs: API auto-mentioning in docs (#910) #1305
Merged
shcheklein
merged 8 commits into
iterative:master
from
mvshmakov:feature/910-api-automentioning
May 19, 2020
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
800c98c
docs: api markdown code linker
mvshmakov c6a0881
docs: api and command markdown code tests
mvshmakov d4d5405
docs: manual links to api method pages removed
mvshmakov 71921db
docs: codeclimate fixes
mvshmakov ec953a3
docs: test fixes
mvshmakov a591ad0
docs-910: helpers refactoring
mvshmakov 2970bff
docs-910: bringing back formatting
mvshmakov ee558de
docs-910: indent fix and redundant url removal
mvshmakov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here I would actually prefer to link
[Python API]
manually for readability.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 036672d along with a couple more instances I found.