Skip to content

Commit

Permalink
fix: rehypeDetails rehypeSummary
Browse files Browse the repository at this point in the history
  • Loading branch information
abernier committed Aug 27, 2024
1 parent fc22f64 commit a2ab5cc
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 4 deletions.
4 changes: 0 additions & 4 deletions src/app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ body {
scroll-margin-top: theme('spacing.20'); /* <Search />'s h-16 + more spacing */
}

details {
@apply my-4;
}

/**
* Pmndrs theme for JavaScript, CSS and HTML
* Loosely based on https://marketplace.visualstudio.com/items?itemName=pmndrs.pmndrs
Expand Down
6 changes: 6 additions & 0 deletions src/components/mdx/Details/Details.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import cn from '@/lib/cn'
import { type ComponentProps } from 'react'

export function Details({ className, ...props }: ComponentProps<'details'>) {
return <details className={cn('my-4', className)} {...props} />
}
1 change: 1 addition & 0 deletions src/components/mdx/Details/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Details'
12 changes: 12 additions & 0 deletions src/components/mdx/Details/rehypeDetails.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { Root } from 'hast'
import { visit } from 'unist-util-visit'

// https://unifiedjs.com/learn/guide/create-a-rehype-plugin/
export function rehypeDetails() {
return (tree: Root) => {
visit(tree, null, function (node) {
// console.log(node)
if (node.type === 'mdxJsxFlowElement' && node.name === 'details') node.name = 'Details' // map HTML <details> to <Details> React component
})
}
}
6 changes: 6 additions & 0 deletions src/components/mdx/Summary/Summary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import cn from '@/lib/cn'
import { type ComponentProps } from 'react'

export function Summary({ className, ...props }: ComponentProps<'summary'>) {
return <summary className={cn('mb-2', className)} {...props} />
}
1 change: 1 addition & 0 deletions src/components/mdx/Summary/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Summary'
12 changes: 12 additions & 0 deletions src/components/mdx/Summary/rehypeSummary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { Root } from 'hast'
import { visit } from 'unist-util-visit'

// https://unifiedjs.com/learn/guide/create-a-rehype-plugin/
export function rehypeSummary() {
return (tree: Root) => {
visit(tree, null, function (node) {
// console.log(node)
if (node.type === 'mdxJsxFlowElement' && node.name === 'summary') node.name = 'Summary' // map HTML <summary> to <Summary> React component
})
}
}
2 changes: 2 additions & 0 deletions src/components/mdx/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
export * from './Codesandbox'
export * from './Details'
export * from './Gha'
export * from './Grid'
export * from './Hint'
export * from './Img'
export * from './Summary'
export * from './Toc'

import cn from '@/lib/cn'
Expand Down
4 changes: 4 additions & 0 deletions src/utils/docs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import * as components from '@/components/mdx'
import { Codesandbox } from '@/components/mdx/Codesandbox'
import { fetchCSB } from '@/components/mdx/Codesandbox/fetchCSB'
import { rehypeCodesandbox } from '@/components/mdx/Codesandbox/rehypeCodesandbox'
import { rehypeDetails } from '@/components/mdx/Details/rehypeDetails'
import { rehypeGha } from '@/components/mdx/Gha/rehypeGha'
import { rehypeImg } from '@/components/mdx/Img/rehypeImg'
import { rehypeSummary } from '@/components/mdx/Summary/rehypeSummary'
import { rehypeToc } from '@/components/mdx/Toc/rehypeToc'
import resolveMdxUrl from '@/utils/resolveMdxUrl'
import matter from 'gray-matter'
Expand Down Expand Up @@ -158,6 +160,8 @@ async function _getDocs(
remarkPlugins: [remarkGFM],
rehypePlugins: [
rehypeImg,
rehypeDetails,
rehypeSummary,
rehypeGha,
rehypePrismPlus,
rehypeCodesandbox(boxes), // 1. put all Codesandbox[id] into `doc.boxes`
Expand Down

0 comments on commit a2ab5cc

Please sign in to comment.