-
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
Interactive synopsis Fix #2024 #3140
Merged
Merged
Changes from 26 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
fe55d51
added html to react
yathomasi 7eb7a3c
utility html to react parser
yathomasi d2491c4
using code component and minor changes
yathomasi 0357010
added args linker plugin to add ids to options
yathomasi 2db56e1
removed hover css
yathomasi 551aae5
fixed selection logic
yathomasi 65cd894
removed html to react dependency
yathomasi 0897dbe
added anchor icon in front of list items
yathomasi aced399
update to fix only use args from square brackets
yathomasi 532c3ec
show anchor icon on list hover
yathomasi 5ab5723
added outline
yathomasi f433cc9
linkified whole arguments with parameters
yathomasi fee75df
allowed two level of square brackets nestings
yathomasi 1ff0a76
made link icon visible on focus
yathomasi 1d98d06
updated command linker to add hash links for args
yathomasi 8392d9f
fix codeclimate
yathomasi 9ed8ed8
added id attribute to respective inline code block
yathomasi 27960ac
added id to list and paragraph instead
yathomasi b8d44a4
updated code component to use all props
yathomasi ce1530f
added ids for argument to respective code block
yathomasi d083248
used lodash has property for key check
yathomasi 6515b73
replaced custom component with prism hook
yathomasi 6013dd2
generalized the arg regex
yathomasi 3debb34
updated command linker
yathomasi aebacd4
reduced line for code climate fix
yathomasi ee52a0e
used same link icon source for consistency
yathomasi 301b0aa
added pathname params
yathomasi b99fd5c
code fixes
yathomasi 59363d3
aligned the icons
yathomasi 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
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,94 @@ | ||
const visit = require('unist-util-visit') | ||
const _ = require('lodash') | ||
|
||
const argsRegex = new RegExp(/\-{1,2}[a-zA-Z-]*/, 'ig') | ||
|
||
function patch(context, key, value) { | ||
if (!_.has(context, key)) { | ||
context[key] = value | ||
} | ||
|
||
return context[key] | ||
} | ||
|
||
const addIdAttrToNode = (node, id) => { | ||
const data = patch(node, `data`, {}) | ||
|
||
patch(data, `id`, id) | ||
patch(data, `htmlAttributes`, {}) | ||
patch(data, `hProperties`, {}) | ||
patch(data.htmlAttributes, `id`, id) | ||
patch(data.hProperties, `id`, id) | ||
} | ||
|
||
module.exports = ( | ||
{ markdownAST }, | ||
{ icon = '', className = 'anchor', isIconAfterHeader = false } | ||
) => { | ||
visit( | ||
markdownAST, | ||
node => | ||
node.type === 'listItem' && | ||
node.children.some( | ||
item => | ||
item.type === 'paragraph' && | ||
item.children[0]?.type === 'inlineCode' && | ||
String(item.children[0]?.value).startsWith('-') | ||
), | ||
listItemNode => { | ||
const isParagraphNode = listItemNode.children?.[0].type === 'paragraph' | ||
if (!isParagraphNode) return | ||
const paragraphNode = listItemNode.children[0] | ||
const isFirstArgNode = | ||
paragraphNode.children[0]?.type === 'inlineCode' && | ||
String(paragraphNode.children[0]?.value).startsWith('-') | ||
if (isFirstArgNode) { | ||
const firstArgNode = paragraphNode.children[0] | ||
const value = firstArgNode.value | ||
const id = value.match(argsRegex)[0] | ||
addIdAttrToNode(firstArgNode, id) | ||
|
||
const data = patch(listItemNode, `data`, {}) | ||
patch(data, `htmlAttributes`, {}) | ||
patch(data, `hProperties`, {}) | ||
if (icon) { | ||
yathomasi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
patch(data.hProperties, `style`, `position:relative;`) | ||
const label = id.split(`-`).join(` `) | ||
const method = isIconAfterHeader ? `push` : `unshift` | ||
listItemNode.children[method]({ | ||
type: `link`, | ||
url: `#${id}`, | ||
title: null, | ||
children: [], | ||
data: { | ||
hProperties: { | ||
'aria-label': `option ${label.trim()} permalink`, | ||
class: `${className} ${isIconAfterHeader ? `after` : `before`}` | ||
}, | ||
hChildren: [ | ||
{ | ||
type: `raw`, | ||
// The Octicon link icon is the default. But users can set their own icon via the "icon" option. | ||
value: icon | ||
} | ||
] | ||
} | ||
}) | ||
} | ||
|
||
const isSecondArgNode = | ||
String(paragraphNode.children[1]?.value).trim() === ',' && | ||
paragraphNode.children[2]?.type === 'inlineCode' && | ||
String(paragraphNode.children[2].value).startsWith('-') | ||
if (isSecondArgNode) { | ||
const secondArgNode = paragraphNode.children[2] | ||
const value = secondArgNode.value | ||
const id = value.match(argsRegex)[0] | ||
addIdAttrToNode(secondArgNode, id) | ||
} | ||
} | ||
} | ||
) | ||
// Manipulate AST | ||
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,12 @@ | ||
{ | ||
"name": "gatsby-remark-args-linker", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC" | ||
} |
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,4 @@ | ||
const svgIcon = `<svg aria-hidden="true" height="16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"/></svg>` | ||
module.exports = { | ||
linkIcon: svgIcon | ||
} |
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
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.
will it match lists like "- some text
--flag
"? also does it mean that we math lists outside theOptions
section?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.
it matches list with 1st line start with inline Code block i.e `` and that starts with
-
. Yes, If it exactly matches this then it's possible outside options too. We can work on this more if it collide but I found that unique for my quick overlook over the docs.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.
We do have a list that matches the pattern in our DVCLive docs: https://dvc-org-interactive-syn-zo8hwd.herokuapp.com/doc/dvclive/dvclive-with-dvc#--live-no-cache. Not sure if it does any harm to have links there... Though if we wanted, we could have
gatsby-remark-args-linker
only look through pages in our/command-reference/
directory 🤔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.
thanks, Julie and Thomas. q: how hard it is to make it strict and well-defined?
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.
The possible way I could think of was to make it strict or well defined is by checking heading Options and looking into a consecutive list and to the list item.
But, I found it difficult to do so with Markdown AST. We could easily achieve that if we had something easy/well-defined APIs/functions as in DOM manipulation. Otherwise, we need to come up with some hacky way to do so.
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.
sounds good, but it's also about replacing
node.children.some
with something likenode.children.first
'docs/command-reference' - we need to make it a param? since it might be different across different websites 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.
This would check the first child in the list, right? While this would work for almost all use cases
/repro
options starts withtargets
:It is the only one as far as I know though :)
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.
Oops sorry, I missed this conversation.
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.
I have added the params for paths which can be a string(path) or an array of paths. For now, I have set it to
docs/command-reference
.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.
I have made some changes and pushed.