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

Fix details triggers that contain markup #1736

Merged
merged 10 commits into from
Aug 31, 2020
4 changes: 2 additions & 2 deletions content/docs/user-guide/external-dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ The command above creates the <abbr>import stage</abbr> (DVC-file)

<details>

### Expand to see resulting DVC-file
### Expand to see resulting `.dvc` file

```yaml
# ...
Expand Down Expand Up @@ -193,7 +193,7 @@ specified (with the `repo` field).

<details>

### Expand to see resulting DVC-file
### Expand to see resulting `.dvc` file

```yaml
# ...
Expand Down
4 changes: 3 additions & 1 deletion plugins/gatsby-remark-dvc-linker/simpleLinker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const { createLinkNode } = require('./helpers')

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

const excludedParentTypes = ['link', 'heading']

const useMatcher = (matcher, item) => {
switch (typeof matcher) {
case 'string':
Expand All @@ -21,7 +23,7 @@ module.exports = astNode => {
const node = astNode[0]
const parent = astNode[2]

if (parent.type !== 'link') {
if (!excludedParentTypes.includes(parent.type)) {
const entry = entries.find(({ matches }) => useMatcher(matches, node.value))
if (entry) {
createLinkNode(entry.url, astNode)
Expand Down
39 changes: 31 additions & 8 deletions src/components/Documentation/Markdown/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import React, { useCallback, useEffect, useRef } from 'react'
import React, {
useCallback,
useEffect,
useRef,
ReactNode,
ReactElement
} from 'react'
import cn from 'classnames'
import { navigate } from '@reach/router'
import rehypeReact from 'rehype-react'
Expand Down Expand Up @@ -30,17 +36,34 @@ const isInsideCodeBlock = (node: Element): boolean => {
}

const Details: React.FC<{
children: Array<{ props: { children: Array<string> } } | string>
children: Array<{ props: { children: ReactNode } } | string>
}> = ({ children }) => {
const filteredChildren = children.filter(child => child !== '\n')
const filteredChildren: ReactNode[] = children.filter(child => child !== '\n')
const firstChild = filteredChildren[0] as JSX.Element

if (!filteredChildren.length) return null
if (typeof filteredChildren[0] === 'string') return null

const text = filteredChildren[0].props.children[0]
if (!/^h.$/.test(firstChild.type)) {
throw new Error('The first child of a details element must be a heading!')
}

/*
To work around auto-linked headings, the last child of the heading node
must be removed. The only way around this is the change the autolinker,
which we currently have as an external package.
*/
const triggerChildren: ReactNode[] = firstChild.props.children.slice(
0,
firstChild.props.children.length - 1
) as ReactNode[]

/*
Collapsible's trigger type wants ReactElement, so we force a TS cast from
ReactNode here.
*/
return (
<Collapsible trigger={text} transitionTime={200}>
<Collapsible
trigger={(triggerChildren as unknown) as ReactElement}
transitionTime={200}
>
{filteredChildren.slice(1)}
</Collapsible>
)
Expand Down
28 changes: 0 additions & 28 deletions src/gatsby/models/github/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,6 @@
alone does.
*/

async function createStaticGithubDataNode(api, fieldData = {}) {
Copy link
Member

Choose a reason for hiding this comment

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

is it unused now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, it gets picked up by lint-ts.

Copy link
Member

Choose a reason for hiding this comment

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

hmm ... why didn't it pick it up earlier?

const {
actions: { createNode },
createNodeId,
createContentDigest
} = api

const fields = {
stars: 8888,
parent: null,
...fieldData
}

const node = {
id: createNodeId('DVCGithubStaticData'),
children: [],
...fields,
internal: {
type: 'StaticGithubData',
contentDigest: createContentDigest(fields)
}
}

await createNode(node)

return node
}

module.exports = {
async createSchemaCustomization({ actions: { createTypes }, schema }) {
createTypes(
Expand Down