Code Monkey home page Code Monkey logo

md2rt's Introduction

Markdown to Rich Text

A C# library that can be used to transform Mardown to Tiptap compatible rich-text JSON. Built upon ProseMirror.Model

Have a look at the unit tests to see what Markdown features are supported.

Usage

Simple case where you can convert a Markdown JSON string into rich-text JSON:

const string markdownString = "\"**Bold**\"";
var richText = MD2RT.MD2RT.ToRichText(markdownString); // Or: .ToRichText("**bold**", isJsonString: false);
var json = JsonConvert.DeserializeObject<string>(richText)!;
var obj = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
Console.WriteLine($"markdown:\n{markdownString}\n\nrich text:\n{JsonConvert.SerializeObject(obj, Formatting.Indented)}");

Which will print:

markdown:
"**Bold**"

rich text:
{
  "type": "doc",
  "content": [
    {
      "type": "paragraph",
      "content": [
        {
          "text": "Bold",
          "type": "text",
          "marks": [
            {
              "type": "bold"
            }
          ]
        }
      ]
    }
  ]
}

Or replace all properties (recursively!) that have an attribute [UIHint("markdown")] above it, and rewrite the rich-text value to another property. For example, running the code:

var page = new Page();

Console.WriteLine("Before processing");
Console.WriteLine($"  TextRt       : {page.TextRt}");
Console.WriteLine($"  NestedTextRt : {page.Components.First().NestedTextRt}\n");

MD2RT.MD2RT.Process(page, "markdown", "rich-text", "Rt");

Console.WriteLine("\nAfter processing");
Console.WriteLine($"  TextRt       : {page.TextRt}");
Console.WriteLine($"  NestedTextRt : {page.Components.First().NestedTextRt}");

public class Page
{
  [UIHint("markdown")]
  public string Text { get; set; } = "\"**Bold**\"";

  [UIHint("rich-text")]
  public string TextRt { get; set; } = "";

  public List<Component> Components { get; set; } = new() { new Component() };

  public List<Component>? NullComponents { get; set; }

  public string StringProp1 { get; set; } = "to be ignored 1";

  public String StringProp2 { get; set; } = "to be ignored 2";
}

public class Component
{
  [UIHint("markdown")]
  public string NestedText { get; set; } = "\"*Italic*\"";

  [UIHint("rich-text")]
  public string NestedTextRt { get; set; } = "";
}

will print:

Before processing
  TextRt       : 
  NestedTextRt : 

Written rich-text to 'TextRt' from markdown 'Text'
Written rich-text to 'NestedTextRt' from markdown 'NestedText'

After processing
  TextRt       : "{\"type\":\"doc\",\"content\":[{\"type\":\"paragraph\",\"content\":[{\"text\":\"Bold\",\"type\":\"text\",\"marks\":[{\"type\":\"bold\"}]}]}]}"
  NestedTextRt : "{\"type\":\"doc\",\"content\":[{\"type\":\"paragraph\",\"content\":[{\"text\":\"Italic\",\"type\":\"text\",\"marks\":[{\"type\":\"italic\"}]}]}]}"

md2rt's People

Contributors

bkiers avatar

Watchers

 avatar  avatar

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.