GithubHelp home page GithubHelp logo

solidjs-markdoc's Introduction

solidjs-markdoc

Render Markdoc syntax with SolidJS.

Install

npm install solidjs-markdoc

Usage

Simple Markdown

You can provide simple markdown strings to the renderer to render Markdoc.

import Markdoc from "@markdoc/markdoc";
import render from "solidjs-markdoc";

function App() {
  const md = `
    # Hello World

    We can render markdown.
  `;
  const ast = Markdoc.parse(md);
  const content = Markdoc.transform(ast);

  return render(content);
}

Markdoc Schema and SolidJS Components

Setup a Markdoc schema.

schema/
└── Alert.markdoc.ts
// schema/Alert.markdoc.ts

export default {
  render: "Alert",
  description: "Display the enclosed content in an alert box",
  children: ["paragraph", "tag", "list"],
  attributes: {
    type: {
      type: String,
      default: "default",
      matches: ["default", "info", "warning", "error", "success"],
    },
  },
};

Create your Solid component.

function Alert({ type, children }) {
  return (
    <div class={`alert alert--${type}`}>
      {children}
    </div>
  );
}

Import the renderer and schema into your component.

import Markdoc from "@markdoc/markdoc";
import render from "solidjs-markdoc";
import alert from "./schema/Alert.markdoc";

function Alert({ type, children }) {
  return (
    <div class={`alert alert--${type}`}>
      {children}
    </div>
  );
}

function App() {
  //...
}

Create the config to pass into your Markdoc.transform call.

function App() {
  const md = `
    # Getting started

    You can run SolidJS components in here.

    Check this alert:

    {% alert type="info" %}
    Hey there! Something to look at!
    {% /alert %}
  `;

  const config = {
    tags: {
      alert,
    },
  };

  const ast = Markdoc.parse(md);
  const content = Markdoc.transform(ast, config);

  //...
}

Finally, return the result of the render function making sure to supply your custom component to the render function's components object.

function App() {
  // ...

  return render(content, {
    components: {
      Alert,
    },
  });
}

Full Example

const alert = {
  render: "Alert",
  description: "Display the enclosed content in an alert box",
  children: ["paragraph", "tag", "list"],
  attributes: {
    type: {
      type: String,
      default: "default",
      matches: ["default", "info", "warning", "error", "success"],
    },
  },
};

function Alert({ type, children }) {
  return (
    <div class={`alert alert--${type}`}>
      {children}
    </div>
  );
}

function App() {
  const md = `
    # Getting started

    You can run SolidJS components in here.

    Check this alert:

    {% alert type="info" %}
    Hey there! Something to look at!
    {% /alert %}
  `;

  const config = {
    tags: {
      alert,
    },
  };

  const ast = Markdoc.parse(md);
  const content = Markdoc.transform(ast, config);

  return render(content, {
    components: {
      Alert,
    },
  });
}

solidjs-markdoc's People

Contributors

dillonchanis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

solidjs-markdoc's Issues

SolidStart SSG/SSR

This breaks when using ssr: true, or the solid-start-static adapter. It says "Comp is not defined"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.