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: 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
28 changes: 24 additions & 4 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,31 @@ 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')

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

const text = filteredChildren[0].props.children[0]
const headingChildren: ReactNode[] = filteredChildren[0].props
.children as ReactNode[]

// Remove header auto-link if present
rogermparent marked this conversation as resolved.
Show resolved Hide resolved
const finalHeadingChild = headingChildren[headingChildren.length - 1] as {
rogermparent marked this conversation as resolved.
Show resolved Hide resolved
props: {
className: string
}
}

const triggerChildren: unknown =
rogermparent marked this conversation as resolved.
Show resolved Hide resolved
finalHeadingChild.props !== undefined &&
rogermparent marked this conversation as resolved.
Show resolved Hide resolved
finalHeadingChild.props.className === 'anchor after'
? headingChildren.slice(0, headingChildren.length - 1)
: headingChildren

return (
<Collapsible trigger={text} transitionTime={200}>
<Collapsible trigger={triggerChildren as ReactElement} transitionTime={200}>
{filteredChildren.slice(1)}
</Collapsible>
)
Expand Down