-
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
* added html to react * utility html to react parser * using code component and minor changes * added args linker plugin to add ids to options * removed hover css * fixed selection logic * removed html to react dependency * added anchor icon in front of list items * update to fix only use args from square brackets * show anchor icon on list hover * added outline * linkified whole arguments with parameters * allowed two level of square brackets nestings * made link icon visible on focus * updated command linker to add hash links for args * fix codeclimate * added id attribute to respective inline code block * added id to list and paragraph instead * updated code component to use all props * added ids for argument to respective code block * used lodash has property for key check * replaced custom component with prism hook * generalized the arg regex * updated command linker * reduced line for code climate fix * used same link icon source for consistency * added pathname params * code fixes * aligned the icons
- Loading branch information
Showing
10 changed files
with
2,463 additions
and
2,299 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
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,100 @@ | ||
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, getNode, markdownNode }, | ||
{ icon = '', className = 'anchor', isIconAfterHeader = false, pathname = '' } | ||
) => { | ||
if (!pathname) return markdownAST | ||
const parentNode = getNode(markdownNode.parent) | ||
let isPath = | ||
typeof pathname === 'string' | ||
? parentNode.relativeDirectory.startsWith(pathname) | ||
: Array.isArray(pathname) | ||
? pathname.some(p => parentNode.relativeDirectory.startsWith(p)) | ||
: false | ||
if (!isPath) return markdownAST | ||
visit( | ||
markdownAST, | ||
node => | ||
node.type === 'listItem' && | ||
node.children[0]?.type === 'paragraph' && | ||
node.children[0]?.children[0]?.type === 'inlineCode' && | ||
String(node.children[0].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) { | ||
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.