Skip to content

Commit

Permalink
Reusable AsciiDoc utils (#100)
Browse files Browse the repository at this point in the history
* Reusable Asciidoc utils

* Delete index.ts

* Handle empty extensions

* More shared exports

* Switch parse source

* Move `attrs`

* Export `attrs`

* Shared inline converter and ToC

* Fix icon

* Add mermaid support to listing

* License

* Export should be listing not mermaid

* Undo mermaid listing

* Test `renderWithBreaks`

* Animated accordion styles

* Hide desktop ToC

* Take `className` on ToC instead

* Controlled ToC / autoclose

* Remove unused title

* ToC padding tweak

* Fix accordion trigger padding

* `useDelegatedReactRouterLinks`

* Switch let for const

* Export and add license

* Tweak license

* Add copyright ignore `use-delegated-links` license
  • Loading branch information
benjaminleonard authored Dec 17, 2024
1 parent 6ee86ab commit 37b3cc2
Show file tree
Hide file tree
Showing 13 changed files with 10,583 additions and 5,113 deletions.
1 change: 1 addition & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ header:
- '**/dist/*'
- '**/*.md'
- 'LICENSE'
- 'components/src/asciidoc/use-delegated-links.ts'

comment: on-failure
55 changes: 55 additions & 0 deletions components/src/asciidoc/Section.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*
* Copyright Oxide Computer Company
*/
import { Link16Icon } from '@/icons/react'
import { Content, type SectionBlock, parse } from '@oxide/react-asciidoc'
import cn from 'classnames'
import { createElement } from 'react'

// We need to remove anchors from the section title (and table of contents) because having
// an anchor within an anchor causes a client/server render mismatch
export const stripAnchors = (str: string) => str.replace(/<a[^>]*>(.*?)<\/a>/gi, '$1')

const Section = ({ node }: { node: SectionBlock }) => {
const level = node.level
let title: JSX.Element | string = ''

let sectNum = node.num
sectNum = sectNum === '.' ? '' : sectNum

title = (
<>
<span className="anchor" id={node.id || ''} aria-hidden="true" />
<a className="link group" href={`#${node.id}`}>
{parse(stripAnchors(node.title))}
<Link16Icon className="ml-2 hidden text-accent-secondary group-hover:inline-block" />
</a>
</>
)

if (level === 0) {
return (
<>
<h1 className={cn('sect0', node.role)} data-sectnum={sectNum}>
{title}
</h1>
<Content blocks={node.blocks} />
</>
)
} else {
return (
<div className={cn(`sect${level}`, node.role)}>
{createElement(`h${level + 1}`, { 'data-sectnum': sectNum }, title)}
<div className="sectionbody">
<Content blocks={node.blocks} />
</div>
</div>
)
}
}

export default Section
Loading

0 comments on commit 37b3cc2

Please sign in to comment.