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

Add functionality to link arbitrary strings in code blocks #1576

Merged
merged 7 commits into from
Jul 15, 2020
6 changes: 6 additions & 0 deletions content/linked-terms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be extracted to a simple JSON or YAML config file so we can easily edit the list later? I mean... It's already easy but would be even easier 🙂

Also, should a test be written for this fn?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could, but using .js allows us to use RegExp literal matchers and dynamic replacement values in a future. It's possible to switch to JSON by simply converting what's there now if we decide we don't want those features.

{
matches: 'dvc.yaml',
url: '/doc/user-guide/dvc-files-and-directories#dvcyaml-file'
}
jorgeorpinel marked this conversation as resolved.
Show resolved Hide resolved
]
3 changes: 2 additions & 1 deletion plugins/gatsby-remark-dvc-linker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ const visit = require('unist-util-visit')

const apiLinker = require('./apiLinker')
const commandLinker = require('./commandLinker')
const simpleLinker = require('./simpleLinker')

// 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',
flow([Array, commandLinker, apiLinker, constant(undefined)])
flow([Array, commandLinker, apiLinker, simpleLinker, constant(undefined)])
)
return markdownAST
}
32 changes: 32 additions & 0 deletions plugins/gatsby-remark-dvc-linker/simpleLinker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* eslint-env node */

const { createLinkNode } = require('./helpers')

const entries = require('../../content/linked-terms')

const useMatcher = (matcher, item) => {
switch (typeof matcher) {
case 'string':
return item === matcher
case 'object':
if (Array.isArray(matcher))
return matcher.find(submatcher => useMatcher(submatcher, item))
if (matcher instanceof RegExp) return matcher.match(item)
default:
throw `gatsby-remark-dvc-linker simpleLinker given bad matcher of type "${typeof matcher}"`
shcheklein marked this conversation as resolved.
Show resolved Hide resolved
}
}

module.exports = astNode => {
const node = astNode[0]
const parent = astNode[2]

if (parent.type !== 'link') {
const entry = entries.find(({ matches }) => useMatcher(matches, node.value))
if (entry) {
createLinkNode(entry.url, astNode)
}
}

return astNode
}