Skip to content

About

Solid renderer for Comark.

Topics

Resources

Stars

3 stars

Watchers

0 watching

Forks

Repository files navigation

comark-solid

Solid renderer for Comark.

Warning

Pre-release: Solid 2.0 RC

This package targets the Solid 2.0 RC versions of solid-js and @solidjs/web. Solid 2.0 is not stable yet, so bugs and compatibility issues may occur.

Installation

npm install comark-solid comark

<Markdown>

<Markdown> parses Markdown strings and renders them directly. Parsing is asynchronous, so it suspends to the nearest <Loading> boundary.

import { Markdown } from "comark-solid";
import { Loading } from "solid-js";

const markdown = "# Hello World";

export default function App() {
  return (
    <Loading fallback={<p>Loading Markdown…</p>}>
      <Markdown value={markdown} />
    </Loading>
  );
}

Props

Prop Type Default Description
value string | MarkdownDocument (Required) Markdown string or pre-parsed document.
options ParserOptions undefined Options for parsing Markdown strings.
streaming boolean false Enable incremental parsing and reuse rendered blocks during streaming.
transformNode NodeTransform | readonly NodeTransform[] undefined Transform resolved element tags and props before rendering.
components Record<string, ValidComponent> {} Map of AST node names to Solid components.
data Record<string, unknown> {} Runtime values referenced from markdown via :prop="data.path".
class JSX.ClassValue undefined Additional classes for the wrapper div (comark-content is always included).

Other div props, except children, are passed to the wrapper element.

<MarkdownDocument>

<MarkdownDocument> renders a MarkdownDocument AST produced by parseMarkdown(). It separates rendering from parsing, making it particularly useful for server-side parsing.

import { parseMarkdown } from "comark";
import { MarkdownDocument } from "comark-solid";

const document = await parseMarkdown("# Hello World");

export default function App() {
  return <MarkdownDocument value={document} />;
}

Props

Prop Type Default Description
value MarkdownDocument (Required) Pre-parsed Comark document AST.
streaming boolean false Reuse rendered blocks whose AST node references are retained between updates.
transformNode NodeTransform | readonly NodeTransform[] undefined Transform resolved element tags and props before rendering.
components Record<string, ValidComponent> {} Map of AST node names to Solid components.
data Record<string, unknown> {} Runtime values referenced from markdown via :prop="data.path".
class JSX.ClassValue undefined Additional classes for the wrapper div (comark-content is always included).

Other div props, except children, are passed to the wrapper element.

Custom Components

Map tags to Solid components via the components prop:

import { Markdown } from "comark-solid";
import { Loading } from "solid-js";

import { Card } from "./Card";
import { CustomHeading } from "./CustomHeading";

const markdown = "# Hello World\n\n::card\nCard content\n::";
const components = {
  h1: CustomHeading,
  card: Card,
};

export default function App() {
  return (
    <Loading fallback={<p>Loading Markdown…</p>}>
      <Markdown value={markdown} components={components} />
    </Loading>
  );
}

Components can also be loaded asynchronously using Solid's lazy():

import { lazy } from "solid-js";

const components = {
  chart: lazy(() => import("./Chart")),
};

Component bindings

Props

Attributes in Comark syntax are passed as props to your component. Use the : prefix to pass typed values:

Markdown Prop value
{type="warning"} "warning" (string)
{:count="5"} 5 (number)
{:active=true} true (boolean)
{:config='{"key":"val"}'} { key: "val" } (object)

Slots

The default slot is passed as children. Named slots (e.g. #header) are passed as props prefixed with slot (slotHeader).

::card
#header
Header content

#default
Body content
::
interface CardProps {
  slotHeader?: JSX.Element;
  children?: JSX.Element;
}

export default function Card(props: CardProps) {
  return (
    <div>
      <Show when={props.slotHeader}>{(header) => <div>{header}</div>}</Show>
      <div>{props.children}</div>
    </div>
  );
}

Acknowledgements

This project is heavily based on the official Comark React renderer, adapted for Solid's component model and APIs.

The original implementation is licensed under the MIT License.

License

MIT

About

Solid renderer for Comark.

Topics

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages