Callout
Add callouts inside your code blocks.
content.md
```jsconst lorem = ipsum(dolor, sit)// !callout[/amet/] This is a calloutconst [amet, consectetur] = [0, 0]lorem.adipiscing((sed, elit) => {if (sed) {amet += elit}})```
const lorem = ipsum(dolor, sit)const [amet, consectetur] = [0, 0]This is a calloutlorem.adipiscing((sed, elit) => {if (sed) {amet += elit}})
Implementation
code.tsx
1import { InlineAnnotation, AnnotationHandler } from "codehike/code"23const callout: AnnotationHandler = {4name: "callout",5transform: (annotation: InlineAnnotation) => {We need to transform the annotations from
InlineAnnotation
toBlockAnnotation
6const { name, query, lineNumber, fromColumn, toColumn, data } = annotation7return {8name,9query,10fromLineNumber: lineNumber,11toLineNumber: lineNumber,12data: { ...data, column: (fromColumn + toColumn) / 2 },This will be the position of the arrow in the callout
13}14},15Block: ({ annotation, children }) => {16const { column } = annotation.data17return (18<>19{children}20<div21style={{ minWidth: `${column + 4}ch` }}22className=""23>24<div25style={{ left: `${column}ch` }}26className=""27/>28{annotation.query}29</div>30</>31)32},33}
Then pass the callout
handler to the Pre
component:
code.tsx
async function Code({ codeblock }: { codeblock: RawCode }) {const highlighted = await highlight(codeblock, "github-dark")return <Pre code={highlighted} handlers={[callout]} />}
Make it better
Some ways to improve the callout annotation:
- add different annotations with different styles (like Warning, Error, Info, etc)
- add an option to show the callout either before or after the line
- put markdown inside the callout (see the tooltip example)