{"id":18471660,"url":"https://github.com/portabletext/dotnet-portable-text","last_synced_at":"2025-04-08T11:32:16.894Z","repository":{"id":43656093,"uuid":"270282821","full_name":"portabletext/dotnet-portable-text","owner":"portabletext","description":"Library for working with Portable Text in C#.","archived":false,"fork":false,"pushed_at":"2025-02-11T10:05:55.000Z","size":1060,"stargazers_count":5,"open_issues_count":4,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-23T12:04:21.134Z","etag":null,"topics":["portable-text"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/portabletext.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-06-07T11:20:27.000Z","updated_at":"2023-09-28T16:09:30.000Z","dependencies_parsed_at":"2023-10-15T07:03:33.813Z","dependency_job_id":"85659402-b9e6-43fc-8ca4-4f13ecc55cf3","html_url":"https://github.com/portabletext/dotnet-portable-text","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/portabletext%2Fdotnet-portable-text","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/portabletext%2Fdotnet-portable-text/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/portabletext%2Fdotnet-portable-text/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/portabletext%2Fdotnet-portable-text/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/portabletext","download_url":"https://codeload.github.com/portabletext/dotnet-portable-text/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247834034,"owners_count":21003903,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["portable-text"],"created_at":"2024-11-06T10:17:39.229Z","updated_at":"2025-04-08T11:32:15.220Z","avatar_url":"https://github.com/portabletext.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![test](https://github.com/saasen/dotnet-portable-text/actions/workflows/test.yml/badge.svg)](https://github.com/saasen/dotnet-portable-text/actions/workflows/test.yml)\n![Nuget](https://img.shields.io/nuget/v/PortableText?color=blue\u0026logo=nuget)\n\n# .NET Portable Text\n\nThis repo contains tools for working with [Portable Text](https://portabletext.org) in .NET.\n\n## Installation\n\n```\ndotnet add package PortableText\n```\n\n## Basic Usage\n\n### Rendering\n\n```cs\nusing PortableText;\n\nvar result = PortableTextToHtml.Render(\n    json,       // A string value representing your Portable Text\n    serializers // Optional. Specifies how to render a certain type, mark, list etc.\n);\n```\n\n## Customizing rendering\n\nYou can pass custom serializers in the `serializers` parameter.\n\n### Types\n\nA type serializer takes in a JSON de-serialized type representing your data.\n\nThis example shows how to render a custom type:\n\n```cs\nvar serializers = new PortableTextSerializers\n{\n    TypeSerializers = new Dictionary\u003cstring, TypeSerializer\u003e\n    {\n        {\n            \"youtubeEmbed\", new TypeSerializer\n            {\n                Type = typeof(YoutubeEmbed),\n                Serialize = (value, rawValue, serializers, isInline) =\u003e\n                {\n                    // We are specifying which type we want the JSON de-serialized to, so this is safe.\n                    var typedBlock = value as YoutubeEmbed;\n                    return $@\"\u003ciframe title=\"\"{typedBlock.Title}\"\" href=\"\"{typedBlock.Url}\"\"\u003e\u003c/iframe\u003e\";\n                }\n            }\n        }\n    }\n};\n\nvar result = PortableTextToHtml.Render(\n    json,\n    serializers\n);\n```\n\n### Block styles\n\nBlock styles typically describes a visual property for the whole block.\n\nYou can customize rendering of block styles like this:\n\n```cs\nvar serializers = new PortableTextSerializers\n{\n    BlockStyleSerializers = new Dictionary\u003cstring, Func\u003cIEnumerable\u003cstring\u003e, string\u003e\u003e\n    {\n        { \"h1\", blocks =\u003e @$\"\u003ch1 className=\"\"text-2xl\"\"\u003e{string.Join(string.Empty, blocks)}\u003c/h1\u003e\" }\n    }\n};\n\nvar result = PortableTextToHtml.Render(\n    json,\n    serializers\n);\n```\n\n### Marks\n\nThis library separates annotation marks and decorator marks.\n\n#### Decorator marks\n\nDecorator marks are marks that mean something by themselves.\nExamples of decorator marks would be \"em\" or \"strong\" which could mean to emphasize or bold text.\n\nThis example shows how to render a in-line link in your text:\n\n```cs\nvar serializers = new PortableTextSerializers\n{\n    MarkSerializers =\n    {\n        Decorators = new Dictionary\u003cstring, Func\u003c(string, string)\u003e\u003e\n        {\n            {\n                \"highlight\", () =\u003e (\"\u003cem\u003e\", \"\u003c/em\u003e\u003e\")\n            }\n        }\n    }\n};\n\nvar result = PortableTextToHtml.Render(\n    json,\n    serializers\n);\n```\n\n#### Annotation marks\n\nAnnotation marks are marks that needs additional information other than the name of the mark itself for it to be useful.\nExamples of this would be a \"link\". A link needs a URL to be meaningful.\n\n```cs\npublic class LinkPortableTextMarkAnnotation\n{\n    public string Href { get; set; }\n}\n\nvar serializers = new PortableTextSerializers\n{\n    MarkSerializers =\n    {\n        Annotations = new Dictionary\u003cstring, AnnotatedMarkSerializer\u003e\n        {\n            {\n                \"link\",\n                new AnnotatedMarkSerializer\n                {\n                    Type = typeof(LinkPortableTextMarkAnnotation),\n                    Serialize = (value, rawValue) =\u003e\n                    {\n                        var typed = value as LinkPortableTextMarkAnnotation;\n\n                        return ($@\"\u003ca href=\"\"{typed.Href}\"\"\u003e\", \"\u003c/a\u003e\");\n                    }\n                }\n            }\n        }\n    }\n};\n\nvar result = PortableTextToHtml.Render(\n    json,\n    serializers\n);\n```\n\n### Lists\n\nThis example shows how to customize rendering of a list. It uses the thumbs up sign as the list style type:\n\n```cs\nvar serializers = new PortableTextSerializers\n{\n    ListSerializers = new Dictionary\u003cstring, Func\u003cIEnumerable\u003cstring\u003e, string\u003e\u003e()\n    {\n        { \"bullet\", listItems =\u003e @$\"\u003cul style=\"\"list-style-type: \"\"\\1F44D\"\"\"\"\u003e{string.Join(string.Empty, listItems)}\u003c/ul\u003e\" }\n    }\n};\n\nvar result = PortableTextToHtml.Render(\n    json,\n    serializers\n);\n```\n\n### List items\n\nThis example shows how to customize rendering of a list item:\n\n```cs\nvar serializers = new PortableTextSerializers\n{\n    ListItemSerializers = new Dictionary\u003cstring, Func\u003c(string, string)\u003e\u003e\n    {\n         { \"bullet\", () =\u003e (@\"\u003cli style=\"\"list-style-type: circle;\"\"\u003e🎱 \", \"\u003c/li\u003e\") }\n    }\n};\n\nvar result = PortableTextToHtml.Render(\n    json,\n    serializers\n);\n```\n\n## Unknown types\n\nWhen this library encounters unknown types, they are ignored and you get no warnings.\nIf your type isn't outputted, you are probably missing a serializer for it.\n\n## License\n\nMIT © [Anders Stensaas](https://github.com/saasen/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fportabletext%2Fdotnet-portable-text","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fportabletext%2Fdotnet-portable-text","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fportabletext%2Fdotnet-portable-text/lists"}