Converters
Convert Andesine content to and from Markdown, MDX, Markdoc, HTML, and plain text.
The API returns content as a JSON document. @andesine/converters turns that document into Markdown, MDX, Markdoc, HTML, or plain text, and turns Markdown, MDX, Markdoc, and HTML back into documents. Use it to render content on your site, or to import files into Andesine. It is open source under the MIT license.
Formats
Format | Import | To document | From document |
|---|---|---|---|
Markdown |
|
|
|
MDX |
|
|
|
Markdoc |
|
|
|
HTML |
|
|
|
Plain text |
| — |
|
All functions return promises.
Render published content
publishedContentOptions(page) gives the converter the image URLs of the published content.
Title, properties, and fragments
Choose how the title, properties, and fragments are written:
Part | Default | Other options |
|---|---|---|
Title | A first-level heading | Omit it, or write it to the frontmatter |
Properties | Omitted | Frontmatter, or |
Fragments | Unwrapped (content kept) | Omit them, or write them as |
For example, an MDX file with the title and properties in frontmatter:
Markdown, MDX, and Markdoc support frontmatter. HTML and plain text do not. Element mappings need a format with elements: MDX, Markdoc, or HTML.
Elements
In MDX, elements stay components, such as <Callout type="tip">. In Markdoc, they become tags. In HTML, they become div blocks with their attributes as data. To write an element in your own way, for example as your site's component, add a custom handler for it.
A handler gets the element and a context, and returns the output. It can also return an array, or null to omit the element. Handlers for an exact name come before the "*" handler, and your handlers come before the built-in ones. For example, write Callout elements as your site's Notice component in MDX:
To convert Notice back to Callout on import, add a decode handler:
Use encode.nodes and encode.marks for other node and mark types. In a handler, call context.encode or context.decode to convert child content with your handlers.
Heading anchors
getHeadingAnchors(document) returns the anchor ID of each heading. Search results and AI answer sources use the same anchors, so links from search go to the right heading. toHTML adds these IDs to the headings.
Import files
The first-level heading becomes the title. Use targetFragment: "content" to put the imported content in a fragment of a collection with a schema. For images, provide an imageAssetID callback that uploads each image and returns its asset ID.