Skip to content

IContentResolver

A content resolver turns pasted data into components. When an editor pastes into the Block library dialog, Veva asks every resolver whether it recognises the data, lists the suggestions on the From clipboard tab, and inserts the components of the one the editor picks. Implement IContentResolver to suggest your own component for a kind of text, HTML or URL.

Interface declaration

	namespace Veva.Core.Domain.Interfaces;

	public interface IContentResolver
	{
		string Name { get; }

		int Priority { get; }

		Task<ContentResolverResult> CanResolve(string type, string data);

		Task<List<ContentMeta>> GetContentMeta(string type, string data, Guid websiteId);
	}

Example

This resolver recognises a pasted Vimeo video address and suggests inserting it as an Embedded page component that shows the Vimeo player. It is modelled on Veva's own IframeContentResolver.

	using Veva.Core.Domain.Interfaces;
	using Veva.Core.Domain.Model;
	using Veva.Modules.StandardViewComponents.Components;

	public class VimeoContentResolver : IContentResolver
	{
		public string Name => "Vimeo content resolver";

		// Above the generic URL resolvers, so a mixed paste picks this one for a Vimeo line.
		public int Priority => 1150;

		public Task<ContentResolverResult> CanResolve(string type, string data)
		{
			if (GetVideoId(type, data) is null)
			{
				return Task.FromResult<ContentResolverResult>(null);
			}

			return Task.FromResult(new ContentResolverResult
			{
				Name = "Vimeo video",
				Description = "Inserts the video as an embedded Vimeo player",
				Priority = 10,
			});
		}

		public Task<List<ContentMeta>> GetContentMeta(string type, string data, Guid websiteId)
		{
			string videoId = GetVideoId(type, data);
			if (videoId is null)
			{
				return Task.FromResult(new List<ContentMeta>());
			}

			return Task.FromResult(new List<ContentMeta>
			{
				new ContentMeta
				{
					ComponentId = EmbeddedPage.Id,
					Properties = new Dictionary<string, object>
					{
						["Url"] = "https://player.vimeo.com/video/" + videoId,
						["Title"] = "Vimeo video",
					},
				},
			});
		}

		// https://vimeo.com/123456789 gives 123456789; anything else gives null.
		private static string GetVideoId(string type, string data)
		{
			if (type != "text/plain" || !Uri.TryCreate(data?.Trim(), UriKind.Absolute, out Uri uri))
			{
				return null;
			}

			if (uri.Host != "vimeo.com" && uri.Host != "www.vimeo.com")
			{
				return null;
			}

			string id = uri.AbsolutePath.Trim('/');
			return id.Length > 0 && id.All(char.IsDigit) ? id : null;
		}
	}

Registration

This is how the implementation is registered into the DI container in Veva, this should be done in your module config file (The class which implements IModule).

	using Veva.Core.Domain.Interfaces;

	public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
	{
		// Plain DI, not AddNamed: Veva takes every registered IContentResolver.
		services.AddScoped<IContentResolver, VimeoContentResolver>();
	}

How it runs. An editor pastes into the Block library dialog, in the search field or anywhere in the dialog. The admin sends each clipboard item, as a MIME type and its data, to Veva. Veva calls CanResolve on every registered resolver for every item. A resolver that recognises the data returns a ContentResolverResult with a Name and a Description; one that does not returns null. The results are listed on the From clipboard tab. When the editor clicks one, Veva finds the resolver again by its class name and calls GetContentMeta with the same type and data and the website's id, then adds the components it returns to the page.

What you get. type is the clipboard item's MIME type, usually text/plain or text/html; a pasted image arrives with an image type and a data URL. Check the type before you parse data. Trim the data: pasted text often ends with a line break.

What you return. Each ContentMeta is one component to insert: its ComponentId (the component's Id), its property values in Properties keyed by property name, and optionally the content of its areas in Areas and its modifiers in Hooks. An interface property, such as a link, takes its stored form, as UrlContentResolver does for a Link component's link source. Return an empty list when the data is not yours after all.

Ordering. The list of suggestions is sorted by the Priority of each result, highest first, so set it on the result to move your suggestion up. The resolver's own Priority sets the order Veva asks the resolvers in, and it decides which resolver wins for each line of a mixed paste: Multi-lined content asks the other resolvers line by line and uses the one with the highest Priority. ContentResolverResult.StopResolving exists, but nothing in the current code reads it, so it does not hide other suggestions.

Turning it on. Register the resolver with plain dependency injection, as Veva's own are; there is no setting and no list to add it to. Veva's resolvers cover plain text, paragraphs, headings, links, embedded pages, horizontal rules, block quotes, lists, tables, HTML, SVG and pasted Veva content.

Pitfalls. CanResolve runs for every paste and every resolver, so keep it quick and do not call other systems from it. Do not throw on data you do not expect: return null. Name is a label only; Veva finds the resolver by its class name, so the class name must be unique.

Type to search the docs, components and API.

↑↓ to move ↵ to open Esc to close Search by Veva