Skip to content

Commit

Permalink
Merge pull request #67 from oxidecomputer/rough-callout-fix
Browse files Browse the repository at this point in the history
Fixes callouts not rendering properly
  • Loading branch information
benjaminleonard authored Sep 21, 2024
2 parents b35e57c + 23436e5 commit a675e7d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
34 changes: 31 additions & 3 deletions app/components/AsciidocBlocks/Listing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,31 @@ const Listing = ({ node }: { node: AdocTypes.Block }) => {
const content = getContent(node)
const decodedContent = decode(content) || content // unescape the html entities

// Replace callouts with placeholders
const replaceCallouts = (content: string) => {
const calloutRegex = /<i class="conum" data-value="\d+"><\/i>/g
const callouts: string[] = []
let placeholderContent = content.replace(calloutRegex, (match) => {
callouts.push(match)
return `__CALLOUT_PLACEHOLDER_${callouts.length - 1}__`
})
return { placeholderContent, callouts }
}

// Restore callouts from placeholders
const restoreCallouts = (highlightedContent: string, callouts: string[]) => {
return highlightedContent.replace(
/__CALLOUT_PLACEHOLDER_(\d+)__/g,
(_, index) => callouts[parseInt(index)],
)
}

const { placeholderContent, callouts } = replaceCallouts(decodedContent)

// Listing blocks of style `source` are source code, should have their syntax
// highlighted (where we have language support) and be inside both a `pre` and `code` tag
if (node.getStyle() === 'source') {
const lang = attrs.language

return (
<div className="listingblock" {...getLineNumber(node)}>
<CaptionedTitle node={node} />
Expand All @@ -98,7 +118,10 @@ const Listing = ({ node }: { node: AdocTypes.Block }) => {
dangerouslySetInnerHTML={{
__html:
(hljs.getLanguage(lang) &&
hljs.highlight(decodedContent, { language: lang }).value) ||
restoreCallouts(
hljs.highlight(placeholderContent, { language: lang }).value,
callouts,
)) ||
decodedContent,
}}
/>
Expand All @@ -113,7 +136,12 @@ const Listing = ({ node }: { node: AdocTypes.Block }) => {
<div className="listingblock" {...getLineNumber(node)}>
<CaptionedTitle node={node} />
<div className="content">
<pre className={nowrap ? ' nowrap' : ''}>{node.getSource()}</pre>
<pre
className={cn('highlight !block', nowrap ? 'nowrap' : '')}
dangerouslySetInnerHTML={{
__html: restoreCallouts(placeholderContent, callouts),
}}
/>
</div>
</div>
)
Expand Down
8 changes: 7 additions & 1 deletion app/components/AsciidocBlocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,10 @@ const convertInlineQuoted = (node: AdocTypes.Inline) => {
}
}

export { ui, convertInlineQuoted }
function convertInlineCallout(node: AdocTypes.Inline): string {
let text = getText(node)

return `<i class="conum" data-value="${text}"></i>`
}

export { ui, convertInlineQuoted, convertInlineCallout }
9 changes: 8 additions & 1 deletion app/routes/rfd.$slug.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ import { Fragment, Suspense, useMemo } from 'react'
import { renderToString } from 'react-dom/server'
import { ClientOnly } from 'remix-utils'

import { convertInlineQuoted, opts, ui } from '~/components/AsciidocBlocks'
import {
convertInlineCallout,
convertInlineQuoted,
opts,
ui,
} from '~/components/AsciidocBlocks'
import Image from '~/components/AsciidocBlocks/Image'
import Container from '~/components/Container'
import Header from '~/components/Header'
Expand Down Expand Up @@ -55,6 +60,8 @@ class InlineConverter {
return renderToString(<Image node={node} hasLightbox={false} />)
case 'inline_quoted':
return convertInlineQuoted(node as unknown as AdocTypes.Inline) // We know this is always inline
case 'inline_callout':
return convertInlineCallout(node as unknown as AdocTypes.Inline) // We know this is always inline
default:
break
}
Expand Down
4 changes: 4 additions & 0 deletions app/styles/lib/asciidoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,8 @@
.asciidoc-body .admonition-content > div:first-of-type {
@apply text-sans-semi-md;
}

.asciidoc-body pre .conum[data-value] {
@apply text-accent bg-accent-secondary-hover;
}
}

0 comments on commit a675e7d

Please # to comment.