|
| 1 | +--- |
| 2 | +title: API Reference |
| 3 | +description: Comprehensive list of codehike's API |
| 4 | +--- |
| 5 | + |
| 6 | +import { Usage } from "./api" |
| 7 | + |
| 8 | +## `codehike/mdx` |
| 9 | + |
| 10 | +```tsx |
| 11 | +import { remarkCodeHike, recmaCodeHike } from "codehike/mdx" |
| 12 | +``` |
| 13 | + |
| 14 | +### `remarkCodeHike` |
| 15 | + |
| 16 | +A remark plugin that transform codeblocks and inline code into the specified components. |
| 17 | + |
| 18 | +- from annotation |
| 19 | +- you have to provide the components |
| 20 | + |
| 21 | +```tsx |
| 22 | +import { remarkCodeHike, recmaCodeHike } from "codehike/mdx" |
| 23 | + |
| 24 | +/** @type {import('codehike/mdx').CodeHikeConfig} */ |
| 25 | +const chConfig = { |
| 26 | + components: { code: "MyCode", inlineCode: "MyInlineCode" }, |
| 27 | + ignoreCode: (codeblock) => codeblock.lang === "mermaid", |
| 28 | + syntaxHighlighting: { theme: "github-dark" }, |
| 29 | +} |
| 30 | + |
| 31 | +// what you do with the `mdxOptions` depends on what you use to handle mdx |
| 32 | +const mdxOptions = { |
| 33 | + remarkPlugins: [[remarkCodeHike, chConfig], ...otherRemarkPlugins], |
| 34 | + recmaPlugins: [[recmaCodeHike, chConfig]], |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +#### `CodeHikeConfig.components.code` |
| 39 | + |
| 40 | +If you specify a component for `code`, the `remarkCodeHike` plugin will replace code blocks with that component. The language, meta, and value of the code block will be passed inside a `codeblock` prop. |
| 41 | + |
| 42 | +<Usage> |
| 43 | + |
| 44 | +## !caption |
| 45 | + |
| 46 | +How `remarkCodeHike` compiles codeblocks using the `code` component |
| 47 | + |
| 48 | +## !left |
| 49 | + |
| 50 | +```tsx your-config.js |
| 51 | +const chConfig = { |
| 52 | + // !mark[/MyCode/] 1 |
| 53 | + components: { code: "MyCode" }, |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +{/* prettier-ignore */} |
| 58 | +````mdx content.mdx |
| 59 | +# Hello |
| 60 | + |
| 61 | +{/* !mark[/js|lorem ipsum|console.log\(1\)/gm] 5 */} |
| 62 | +```js lorem ipsum |
| 63 | +console.log(1) |
| 64 | +``` |
| 65 | +```` |
| 66 | + |
| 67 | +## !right |
| 68 | + |
| 69 | +{/* prettier-ignore */} |
| 70 | +```jsx compiled output -w |
| 71 | +export default function Content(props = {}) { |
| 72 | + // !mark[/MyCode/gm] 1 |
| 73 | + // !mark[/js|lorem ipsum|console.log\(1\)/gm] 5 |
| 74 | + const { MyCode } = props.components |
| 75 | + return ( |
| 76 | + <> |
| 77 | + <h1>Hello</h1> |
| 78 | + <MyCode codeblock={{ |
| 79 | + lang: "js", |
| 80 | + meta: "lorem ipsum", |
| 81 | + value: "console.log(1)" |
| 82 | + }} /> |
| 83 | + </> |
| 84 | + ) |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +</Usage> |
| 89 | + |
| 90 | +FAQ |
| 91 | + |
| 92 | +- how to pass the components to the `Content` component? |
| 93 | +- how to syntax highlight the code? |
| 94 | + |
| 95 | +#### `CodeHikeConfig.components.inlineCode` |
| 96 | + |
| 97 | +If you specify a component for `inlineCode`, the `remarkCodeHike` plugin will replace inline code with that component. The value of the inline code will be passed inside a `codeblock` prop. |
| 98 | + |
| 99 | +Code Hike uses a special syntax to define inline code ``_`code`_``. This syntax also allows you to specify the language and meta for inline code ``_py lorem ipsum`print 5`_``, will give you _`{lang: "py", meta: "lorem ipsum", value: "print 5"}`_. |
| 100 | + |
| 101 | +<Usage> |
| 102 | + |
| 103 | +## !caption |
| 104 | + |
| 105 | +How `remarkCodeHike` compiles inline code using the `inlineCode` component |
| 106 | + |
| 107 | +## !left |
| 108 | + |
| 109 | +```tsx your-config.js |
| 110 | +const chConfig = { |
| 111 | + // !mark[/MyInlineCode/] 1 |
| 112 | + components: { inlineCode: "MyInlineCode" }, |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +{/* prettier-ignore */} |
| 117 | +````mdx content.mdx -w |
| 118 | +# Hello |
| 119 | + |
| 120 | +Case 1: `var x = 10` |
| 121 | + |
| 122 | +Case 2: _`var x = 10`_ |
| 123 | + |
| 124 | +Case 3: _css`a { color: #123 }`_ |
| 125 | +```` |
| 126 | + |
| 127 | +## !right |
| 128 | + |
| 129 | +{/* prettier-ignore */} |
| 130 | +```jsx compiled output -w |
| 131 | +export default function Content(props = {}) { |
| 132 | + // !mark[/MyInlineCode/gm] 1 |
| 133 | + const { MyInlineCode } = props.components |
| 134 | + return ( |
| 135 | + <> |
| 136 | + <h1>Hello</h1> |
| 137 | + <p>Case 1: <code>var x = 10</code></p> |
| 138 | + <p>Case 2: |
| 139 | + <MyInlineCode codeblock={{ |
| 140 | + lang: "jsx", |
| 141 | + value: "var x = 10" |
| 142 | + }} /> |
| 143 | + </p> |
| 144 | + <p>Case 3: |
| 145 | + <MyInlineCode codeblock={{ |
| 146 | + lang: "css", |
| 147 | + value: "a { color: #123 }" |
| 148 | + }} /> |
| 149 | + </p> |
| 150 | + </> |
| 151 | + ) |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | +</Usage> |
| 156 | + |
| 157 | +#### `CodeHikeConfig.ignoreCode` |
| 158 | + |
| 159 | +#### `CodeHikeConfig.syntaxHighlighting` |
| 160 | + |
| 161 | +### `recmaCodeHike` |
| 162 | + |
| 163 | +A recma plugin that parses code hike blocks. |
| 164 | + |
| 165 | +## `codehike` |
| 166 | + |
| 167 | +```tsx |
| 168 | +import { parse } from "codehike" |
| 169 | +``` |
| 170 | + |
| 171 | +## `codehike/blocks` |
| 172 | + |
| 173 | +```tsx |
| 174 | +import { |
| 175 | + parseRoot, |
| 176 | + parseProps, |
| 177 | + Block, |
| 178 | + CodeBlock, |
| 179 | + HighlightedCodeBlock, |
| 180 | + ImageBlock, |
| 181 | +} from "codehike/blocks" |
| 182 | +``` |
| 183 | + |
| 184 | +## `codehike/code` |
| 185 | + |
| 186 | +```tsx |
| 187 | +import { |
| 188 | + highlight, |
| 189 | + Pre, |
| 190 | + Inline, |
| 191 | + InnerPre, |
| 192 | + InnerLine, |
| 193 | + InnerToken, |
| 194 | + getPreRef, |
| 195 | +} from "codehike/code" |
| 196 | +``` |
| 197 | + |
| 198 | +## `codehike/utils` |
| 199 | + |
| 200 | +```tsx |
| 201 | +import { |
| 202 | + SelectionProvider, |
| 203 | + Selectable, |
| 204 | + Selection, |
| 205 | + useSelectedIndex, |
| 206 | +} from "codehike/utils/selection" |
| 207 | + |
| 208 | +import { |
| 209 | + TokenTransitionsSnapshot, |
| 210 | + calculateTransitions, |
| 211 | + getStartingSnapshot, |
| 212 | +} from "codehike/utils/token-transitions" |
| 213 | +``` |
0 commit comments