ITextProcessor
A text processor changes the text of components such as Paragraph and Heading before it reaches the page. Veva renders Markdown and data binding this way. Implement ITextProcessor to add your own step: it gets the text, or the previous processor's output, and returns the string to pass on.
Interface declaration
namespace Veva.Core.Domain.TextProcessing; public interface ITextProcessor { string Name { get; } Task<string> Process(TextProcessorContext context, DataBindingContext dataBindingContext, string input); }
Example
This processor shows a key name written in double brackets, such as [[Ctrl]], as a keyboard key with the kbd element. It returns HTML, so it belongs after Standard text processing in the website's list.
using System.ComponentModel; using System.Text.RegularExpressions; using Veva.Core.Domain.DataBinding; using Veva.Core.Domain.TextProcessing; [DisplayName("Keyboard keys")] [Description("Shows a key name in double brackets, such as [[Ctrl]], as a keyboard key")] public partial class KeyboardKeysTextProcessor : ITextProcessor { // The settings and the Skip lists find a processor by this name: keep it the class name. public string Name => nameof(KeyboardKeysTextProcessor); [GeneratedRegex(@"\[\[([^\[\]<>]+?)\]\]")] private static partial Regex KeyPattern(); public Task<string> Process(TextProcessorContext context, DataBindingContext dataBindingContext, string input) { if (string.IsNullOrEmpty(input)) { return Task.FromResult(input); } // Placed after Standard text processing, it gets HTML in which the key name is already encoded. return Task.FromResult(KeyPattern().Replace(input, "<kbd>$1</kbd>")); } }
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.Application.DependencyInjection; using Veva.Core.Domain.TextProcessing; public void ConfigureServices(IServiceCollection services, IConfiguration configuration) { // The key is the class name; the website's settings store it when the processor is added. services.AddNamed<ITextProcessor, KeyboardKeysTextProcessor>(ServiceLifetime.Transient); }
Where text processors run. The Paragraph, Text span, Block quote and Heading components and the data table pass their text through the website's text processors before they render it. The string the last processor returns is written to the page as HTML, unchanged. Heading HTML-encodes its text first and skips the two Markdown steps, so a processor there gets encoded plain text.
Turning one on. Registering a processor only makes it available. A website uses the processors in its Text Processing Settings (in the admin's website settings): add yours to the list, switch it on and save. The list shows the class's [DisplayName] and [Description], and public read/write properties marked [EditorBrowsable] become the processor's settings, as Enable Html and Paragraph Mode are on Veva's own Standard text processing. The settings store the processor under its registration name, which is the class name, so renaming the class drops it from websites that use it.
Order matters. The processors run one after the other, in the order of the list, and each gets the previous one's output. A new website starts with one processor, Standard text processing (StandardTextProcessor), which runs the Markdown pre-processor, data binding and Markdown, in that order. With Enable Html off, Markdown encodes any HTML the editor typed. So put a processor that returns HTML after it: one placed before it has its markup encoded and printed as text. If the list has neither Standard text processing nor the data binding processor, Veva still runs data binding, right after the Markdown pre-processor.
Skipping. A component can ask for some processors to be left out; Heading does this for Markdown. context.Skips(name) tells you whether a name is in that list, which matters only if your processor runs steps of its own. context.PageRequest gives the page, the website and the visitor, and dataBindingContext the data binding values at that place on the page.
Safety and speed. Your output is not encoded again, so encode anything you add that comes from a visitor or from data, and do not undo the encoding Markdown has done. The processors run on every render of every text component on the page: keep Process quick. The processor is created from the service container, so constructor injection works.