PortableText 1.0.0

dotnet add package PortableText --version 1.0.0
                    
NuGet\Install-Package PortableText -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PortableText" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PortableText" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="PortableText" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add PortableText --version 1.0.0
                    
#r "nuget: PortableText, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package PortableText@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=PortableText&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=PortableText&version=1.0.0
                    
Install as a Cake Tool

test Nuget

.NET Portable Text

This repo contains tools for working with Portable Text in .NET.

Installation

dotnet add package PortableText

Basic Usage

Rendering

using PortableText;

var result = PortableTextToHtml.Render(
    json,       // A string value representing your Portable Text
    serializers // Optional. Specifies how to render a certain type, mark, list etc.
);

Customizing rendering

You can pass custom serializers in the serializers parameter.

Types

A type serializer takes in a JSON de-serialized type representing your data.

This example shows how to render a custom type:

var serializers = new PortableTextSerializers
{
    TypeSerializers = new Dictionary<string, TypeSerializer>
    {
        {
            "youtubeEmbed", new TypeSerializer
            {
                Type = typeof(YoutubeEmbed),
                Serialize = (value, rawValue, serializers, isInline) =>
                {
                    // We are specifying which type we want the JSON de-serialized to, so this is safe.
                    var typedBlock = value as YoutubeEmbed;
                    return $@"<iframe title=""{typedBlock.Title}"" href=""{typedBlock.Url}""></iframe>";
                }
            }
        }
    }
};

var result = PortableTextToHtml.Render(
    json,
    serializers
);

Block styles

Block styles typically describes a visual property for the whole block.

You can customize rendering of block styles like this:

var serializers = new PortableTextSerializers
{
    BlockStyleSerializers = new Dictionary<string, Func<IEnumerable<string>, string>>
    {
        { "h1", blocks => @$"<h1 className=""text-2xl"">{string.Join(string.Empty, blocks)}</h1>" }
    }
};

var result = PortableTextToHtml.Render(
    json,
    serializers
);

Marks

This library separates annotation marks and decorator marks.

Decorator marks

Decorator marks are marks that mean something by themselves. Examples of decorator marks would be "em" or "strong" which could mean to emphasize or bold text.

This example shows how to render a in-line link in your text:

var serializers = new PortableTextSerializers
{
    MarkSerializers =
    {
        Decorators = new Dictionary<string, Func<(string, string)>>
        {
            {
                "highlight", () => ("<em>", "</em>>")
            }
        }
    }
};

var result = PortableTextToHtml.Render(
    json,
    serializers
);
Annotation marks

Annotation marks are marks that needs additional information other than the name of the mark itself for it to be useful. Examples of this would be a "link". A link needs a URL to be meaningful.

public class LinkPortableTextMarkAnnotation
{
    public string Href { get; set; }
}

var serializers = new PortableTextSerializers
{
    MarkSerializers =
    {
        Annotations = new Dictionary<string, AnnotatedMarkSerializer>
        {
            {
                "link",
                new AnnotatedMarkSerializer
                {
                    Type = typeof(LinkPortableTextMarkAnnotation),
                    Serialize = (value, rawValue) =>
                    {
                        var typed = value as LinkPortableTextMarkAnnotation;

                        return ($@"<a href=""{typed.Href}"">", "</a>");
                    }
                }
            }
        }
    }
};

var result = PortableTextToHtml.Render(
    json,
    serializers
);

Lists

This example shows how to customize rendering of a list. It uses the thumbs up sign as the list style type:

var serializers = new PortableTextSerializers
{
    ListSerializers = new Dictionary<string, Func<IEnumerable<string>, string>>()
    {
        { "bullet", listItems => @$"<ul style=""list-style-type: ""\1F44D"""">{string.Join(string.Empty, listItems)}</ul>" }
    }
};

var result = PortableTextToHtml.Render(
    json,
    serializers
);

List items

This example shows how to customize rendering of a list item:

var serializers = new PortableTextSerializers
{
    ListItemSerializers = new Dictionary<string, Func<(string, string)>>
    {
         { "bullet", () => (@"<li style=""list-style-type: circle;"">🎱 ", "</li>") }
    }
};

var result = PortableTextToHtml.Render(
    json,
    serializers
);

Unknown types

When this library encounters unknown types, they are ignored and you get no warnings. If your type isn't outputted, you are probably missing a serializer for it.

License

MIT © Anders Stensaas

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 1,986 2/25/2022
1.0.0-beta003 185 2/22/2022
1.0.0-beta002 173 2/14/2022