JSON Render
The comark/plugins/json-render plugin parses JSON Render specs in json-render and yaml-render code blocks and converts them into Comark AST nodes that render through your components map. It requires no extra dependencies.
Usage
<script setup lang="ts">
import { Markdown } from '@comark/vue'
import jsonRender from '@comark/vue/plugins/json-render'
</script>
<template>
<Suspense>
<Markdown :plugins="[jsonRender()]">{{ content }}</Markdown>
</Suspense>
</template>import { Markdown } from '@comark/react'
import jsonRender from '@comark/react/plugins/json-render'
<Markdown plugins={[jsonRender()]}>{content}</Markdown><script lang="ts">
import { Markdown } from '@comark/svelte'
import jsonRender from '@comark/svelte/plugins/json-render'
</script>
<Markdown {content} plugins={[jsonRender()]} />Features
Full spec
A full spec defines a tree of named elements with a root entry point. Both JSON and YAML are supported and produce identical output:
```json-render
{
"root": "card-1",
"elements": {
"card-1": {
"type": "Card",
"props": { "title": "Welcome" },
"children": ["text-1"]
},
"text-1": {
"type": "Text",
"props": { "content": "Hello from JSON Render" }
}
}
}
``````yaml-render
root: card-1
elements:
card-1:
type: Card
props:
title: Welcome
children:
- text-1
text-1:
type: Text
props:
content: Hello from JSON Render
```root: Key of the root element in theelementsmapelements: Map of element definitions, each withtype,props, and optionalchildren
Single element
When only one element is needed, omit root and elements:
```json-render
{
"type": "Text",
"props": { "content": "Hello World" }
}
``````yaml-render
type: Text
props:
content: Hello World
```The plugin wraps this shorthand in a full spec with a template root.
API
jsonRender()
Returns a ComarkPlugin that replaces json-render and yaml-render code blocks with Markdown AST nodes. Takes no options.
Returns: ComarkPlugin
In the post hook, the plugin finds pre nodes with language: "json-render" or language: "yaml-render", parses the spec, and replaces each block with the resulting AST nodes. Text elements become plain text nodes, and all other element types become element nodes with their props as attributes.