|
| 1 | +--- |
| 2 | +title: Customize Markdoc |
| 3 | +--- |
| 4 | + |
| 5 | +To add additional features to the syntax of your files, customize your Markdoc schema. |
| 6 | +You can add the following extensions: |
| 7 | + |
| 8 | +- [Nodes](#nodes) |
| 9 | +- [Tags](#tags) |
| 10 | +- [Variables](#variables) |
| 11 | +- [Functions](#functions) |
| 12 | +- [Partials](#partials) |
| 13 | + |
| 14 | +You can customize schema in two ways: |
| 15 | + |
| 16 | +- **For a single extension** or simple extensions, pass directly to the preprocessor options. |
| 17 | +- **For multiple extensions at once** or more complex configurations, |
| 18 | + create a configuration folder with schema definitions. |
| 19 | + |
| 20 | +For each extension (such as `nodes`), |
| 21 | +schema definitions passed directly overwrite configuration from a folder. |
| 22 | + |
| 23 | +## Direct definitions |
| 24 | + |
| 25 | +To define any of the extension points directly, |
| 26 | +pass it to the preprocessor options as the name of the extension. |
| 27 | +For example, to define a `$site.name` variable, pass the following: |
| 28 | + |
| 29 | +```javascript |
| 30 | +import { markdocPreprocess } from "markdoc-svelte"; |
| 31 | + |
| 32 | +/** @type {import('@sveltejs/kit').Config} */ |
| 33 | +const config = { |
| 34 | + extensions: [".svelte", ".mdoc"], |
| 35 | + preprocess: [ |
| 36 | + markdocPreprocess({ |
| 37 | + variables: { |
| 38 | + site: { |
| 39 | + name: "Markdoc Svelte", |
| 40 | + }, |
| 41 | + }, |
| 42 | + }), |
| 43 | + ], |
| 44 | +}; |
| 45 | +``` |
| 46 | + |
| 47 | +## Configuration folder |
| 48 | + |
| 49 | +For multiple extensions or more complex configurations, create a folder with your entire Markdoc schema. |
| 50 | + |
| 51 | +By default, the preprocessor looks for a schema in the `./markdoc` and `./src/markdoc` directories. |
| 52 | + |
| 53 | +Define each extension point as a single file |
| 54 | +or as a directory with an `index.ts` or `index.js` file that exports it. |
| 55 | +Partials must be a directory holding Markdoc files. |
| 56 | + |
| 57 | +All extension points are optional. |
| 58 | + |
| 59 | +Example structure: |
| 60 | + |
| 61 | +``` |
| 62 | +markdoc |
| 63 | +├── functions.ts |
| 64 | +├── nodes |
| 65 | +│ ├── heading.ts |
| 66 | +│ ├── index.ts |
| 67 | +│ └── callout.ts |
| 68 | +├── partials |
| 69 | +│ ├── content.mdoc |
| 70 | +│ └── more-content.mdoc |
| 71 | +├── tags.ts |
| 72 | +└── variables.ts |
| 73 | +``` |
| 74 | + |
| 75 | +For example, create custom nodes in `markdoc/nodes.ts`: |
| 76 | + |
| 77 | +```typescript |
| 78 | +import type { Config } from "markdoc-svelte"; |
| 79 | +import { Markdoc } from "markdoc-svelte"; |
| 80 | + |
| 81 | +const nodes: Config["nodes"] = { |
| 82 | + image: { |
| 83 | + render: "EnhancedImage", |
| 84 | + attributes: { |
| 85 | + ...Markdoc.nodes.image.attributes, // Include the default image attributes |
| 86 | + }, |
| 87 | + }, |
| 88 | +}; |
| 89 | + |
| 90 | +export default nodes; |
| 91 | +``` |
| 92 | + |
| 93 | +Or create an index file to export all custom nodes from `markdoc/nodes/index.ts` |
| 94 | +(remember to use [relative imports](#relative-imports)): |
| 95 | + |
| 96 | +```typescript |
| 97 | +import image from "./image.ts"; |
| 98 | +import link from "./link.ts"; |
| 99 | +import paragraph from "./paragraph.ts"; |
| 100 | +import type { Config } from "markdoc-svelte"; |
| 101 | + |
| 102 | +const nodes: Config["nodes"] = { |
| 103 | + image, |
| 104 | + link, |
| 105 | + paragraph, |
| 106 | +}; |
| 107 | + |
| 108 | +export default nodes; |
| 109 | +``` |
| 110 | + |
| 111 | +## Relative imports |
| 112 | + |
| 113 | +You can use relative imports to import definitions from either `.js` or `.ts` files. |
| 114 | +Just remember to include the file extension. |
| 115 | + |
| 116 | +For example, if you define custom functions in `src/lib/functions.js`, |
| 117 | +add them to your schema as follows: |
| 118 | + |
| 119 | +```javascript |
| 120 | +import { markdocPreprocess } from "markdoc-svelte"; |
| 121 | +import functions from "./src/lib/functions.js"; |
| 122 | + |
| 123 | +/** @type {import('@sveltejs/kit').Config} */ |
| 124 | +const config = { |
| 125 | + preprocess: [ |
| 126 | + markdocPreprocess({ |
| 127 | + functions: functions, |
| 128 | + }), |
| 129 | + ], |
| 130 | +}; |
| 131 | +``` |
0 commit comments