diff --git a/package.json b/package.json index b5c7a35a53..1cea16a95c 100644 --- a/package.json +++ b/package.json @@ -100,6 +100,7 @@ "@babel/core": "^7.16.0", "@svgr/webpack": "^6.0.0", "@types/classnames": "^2.2.10", + "@types/github-slugger": "^1.3.0", "@types/isomorphic-fetch": "^0.0.35", "@types/promise-polyfill": "^6.0.3", "@types/react": "^17.0.37", diff --git a/plugins/gatsby-theme-iterative-docs/src/components/Documentation/Markdown/Main/styles.module.css b/plugins/gatsby-theme-iterative-docs/src/components/Documentation/Markdown/Main/styles.module.css index d4324c4701..5f6864bfb6 100644 --- a/plugins/gatsby-theme-iterative-docs/src/components/Documentation/Markdown/Main/styles.module.css +++ b/plugins/gatsby-theme-iterative-docs/src/components/Documentation/Markdown/Main/styles.module.css @@ -79,15 +79,18 @@ content: unset; } - li .anchor svg { + li, + .collapsableDiv .anchor svg { visibility: hidden; } - li:hover .anchor svg { + li, + .collapsableDiv:hover .anchor svg { visibility: visible; } - li .anchor:focus svg { + li, + .collapsableDiv .anchor:focus svg { visibility: visible; } @@ -95,6 +98,10 @@ line-height: unset; } + .collapsableDiv .anchor { + line-height: 2.5; + } + .anchor { margin-left: -24px; } diff --git a/plugins/gatsby-theme-iterative-docs/src/components/Documentation/Markdown/index.tsx b/plugins/gatsby-theme-iterative-docs/src/components/Documentation/Markdown/index.tsx index 650ae47a0b..b7a274dbcf 100644 --- a/plugins/gatsby-theme-iterative-docs/src/components/Documentation/Markdown/index.tsx +++ b/plugins/gatsby-theme-iterative-docs/src/components/Documentation/Markdown/index.tsx @@ -18,8 +18,18 @@ import Tooltip from './Tooltip' import * as styles from './styles.module.css' import { TogglesContext, TogglesProvider } from './ToggleProvider' +import { linkIcon } from '../../../../../../static/icons' +import { useLocation } from '@reach/router' + +import GithubSlugger from 'github-slugger' + +const Details: React.FC<{ slugger: GithubSlugger }> = ({ + slugger, + children +}) => { + const [isOpen, setIsOpen] = useState(false) + const location = useLocation() -const Details: React.FC> = ({ children }) => { const filteredChildren: ReactNode[] = ( children as Array<{ props: { children: ReactNode } } | string> ).filter(child => child !== '\n') @@ -39,17 +49,43 @@ const Details: React.FC> = ({ children }) => { firstChild.props.children.length - 1 ) as ReactNode[] + let slug = slugger.slug(triggerChildren.toString()) + if (slug[0] === '️') { + slug = slug.slice(1) + } + const id = slug.startsWith('-') ? slug.slice(1) : slug + + useEffect(() => { + if (location.hash === `#${id}`) { + setIsOpen(true) + } + + return () => { + setIsOpen(false) + } + }, [location.hash]) + /* Collapsible's trigger type wants ReactElement, so we force a TS cast from ReactNode here. */ return ( - - {filteredChildren.slice(1)} - +
+ + + + + {filteredChildren.slice(1)} + +
) } @@ -195,23 +231,27 @@ const Toggle: React.FC<{ ) } +const Tab: React.FC = ({ children }) => { + return {children} +} // Rehype's typedefs don't allow for custom components, even though they work // eslint-disable-next-line @typescript-eslint/no-explicit-any -const renderAst = new (rehypeReact as any)({ - createElement: React.createElement, - Fragment: React.Fragment, - components: { - a: Link, - abbr: Abbr, - card: Card, - cards: Cards, - details: Details, - toggle: Toggle, - tab: React.Fragment - } -}).Compiler - +const renderAst = (slugger: GithubSlugger) => { + return new (rehypeReact as any)({ + createElement: React.createElement, + Fragment: React.Fragment, + components: { + a: Link, + abbr: Abbr, + card: Card, + cards: Cards, + details: (props: any) =>
, + toggle: Toggle, + tab: Tab + } + }).Compiler +} interface IMarkdownProps { htmlAst: Node githubLink: string @@ -227,9 +267,10 @@ const Markdown: React.FC = ({ tutorials, githubLink }) => { + const slugger = new GithubSlugger() return (
- {renderAst(htmlAst)} + {renderAst(slugger)(htmlAst)}
) } diff --git a/yarn.lock b/yarn.lock index be144e217c..faffebf0cf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3386,6 +3386,11 @@ resolved "https://registry.npmjs.org/@types/get-port/-/get-port-3.2.0.tgz" integrity sha512-TiNg8R1kjDde5Pub9F9vCwZA/BNW9HeXP5b9j7Qucqncy/McfPZ6xze/EyBdXS5FhMIGN6Fx3vg75l5KHy3V1Q== +"@types/github-slugger@^1.3.0": + version "1.3.0" + resolved "https://registry.yarnpkg.com/@types/github-slugger/-/github-slugger-1.3.0.tgz#16ab393b30d8ae2a111ac748a015ac05a1fc5524" + integrity sha512-J/rMZa7RqiH/rT29TEVZO4nBoDP9XJOjnbbIofg7GQKs4JIduEO3WLpte+6WeUz/TcrXKlY+bM7FYrp8yFB+3g== + "@types/glob@*", "@types/glob@^7.1.1": version "7.1.4" resolved "https://registry.npmjs.org/@types/glob/-/glob-7.1.4.tgz"