/DraftJSExporter

Library for converting Draft.js ContentState to HTML on server side

Primary LanguageC#MIT LicenseMIT

DraftJSExporter

This is a tool for converting Draft.js raw ContentState to HTML. It can be usefull for .NET applications when it is needed to store data, generated by Draft.js-based editor and render it to HTML pages on server side.

Usage

var exporter = new Exporter();

var html = exporter.Render(
	@"
{
	""blocks"": [
		{
            ""key"": ""w3rt5"",
            ""text"": ""Title"",
            ""type"": ""header-one"",
            ""depth"": 0,
            ""inlineStyleRanges"": [],
            ""entityRanges"": [],
            ""data"": {}
        }
	]
}
");

//	Returns:
// 	<h1>
//		Title
//	</h1>

Options

You can pass custom ExporterConfig object to change render settings.

EntityDecorators

var exporter = new Exporter(new ExporterConfig
{
    EntityDecorators = new Dictionary<string, Func<IReadOnlyDictionary<string, string>, Element>>
    {
        {"LINK", pairs => new Element("a", pairs, null, true)}
    }
});

StyleMap

var exporter = new Exporter(new ExporterConfig
{
    StyleMap = new StyleMap
    {
        Bold = "b"
    }
});

BlockMap

var exporter = new Exporter(new ExporterConfig
{
    BlockMap = new BlockMap
    {
        Unstyled = "p"
    }
});