CrestApps.Core AI Markdown
CrestApps.Core.AI.Markdown makes text normalization Markdown-aware. It is opt-in: AddCoreAIServices() registers only the plain DefaultAITextNormalizer, and the core AI package does not depend on Markdig.
Guidelines
- Reference the
CrestApps.Core.AI.Markdownpackage and callAddMarkdown()onCrestAppsAISuiteBuilder, orAddCoreAIMarkdown()onIServiceCollection(namespaceCrestApps.Core.AI.Markdown). - Registration adds
MarkdownAITextNormalizeras the singletonIAITextNormalizer. The base registration usesTryAddSingletonand this one usesAddSingleton, so the Markdown normalizer wins whichever is called first. - The normalizer delegates to the static
RagTextNormalizer: HTML is stripped, the text is parsed throughMicrosoft.Extensions.DataIngestion.Markdig, and plain text is extracted before whitespace is normalized. AddMarkdown()does not register document stores, readers or upload endpoints. Add document processing or ingestion separately (crestapps-core-document-processing).
Register
using CrestApps.Core.AI.Documents;
using CrestApps.Core.AI.Documents.OpenXml;
using CrestApps.Core.AI.Documents.Pdf;
using CrestApps.Core.AI.Markdown;
using CrestApps.Core.Data.EntityCore;
builder.Services.AddCrestAppsCore(crestApps => crestApps
.AddAISuite(ai => ai
.AddMarkdown()
.AddDocumentProcessing(documentProcessing => documentProcessing
.AddEntityCoreStores()
.AddOpenXml()
.AddPdf())));
Direct registration:
builder.Services.AddCoreAIMarkdown();
Behavior
IAITextNormalizer has three members, all implemented by MarkdownAITextNormalizer:
| Member | Behavior |
|---|---|
| NormalizeContentAsync(text) | Strips HTML (line breaks and block closes become newlines, entities are decoded, pilcrows removed), parses Markdown, joins the extracted text, collapses horizontal spaces and repeated blank lines |
| NormalizeAndChunkAsync(text) | Strips HTML, parses Markdown, then chunks with DocumentTokenChunker using TiktokenTokenizer.CreateForModel("gpt-4o"), MaxTokensPerChunk = 500, OverlapTokens = 50 |
| NormalizeTitle(title) | Strips HTML and collapses all whitespace to single spaces |
When Markdig reports an unsupported inline type, the normalizer falls back to plain-text normalization and character-based chunking (about 4,000 characters per chunk with 200 characters of overlap) instead of failing the ingestion.
Where It Is Used
The registered IAITextNormalizer is resolved by data source indexing (DefaultAIDataSourceIndexingService), data source retrieval and reference titles (DataSourceRetrieval), and preemptive retrieval (DataSourcePreemptiveRagHandler). Registering Markdown therefore changes both how content is chunked when indexed and how retrieved text and titles look when they reach the model.
Custom Normalizer
Implement IAITextNormalizer (namespace CrestApps.Core.AI.Services) to change chunking, and reuse RagTextNormalizer for the parts you keep:
using CrestApps.Core.AI.Services;
namespace MyApp.Ingestion;
public sealed class HeadingAwareTextNormalizer : IAITextNormalizer
{
public Task<string> NormalizeContentAsync(string text, CancellationToken cancellationToken = default)
=> RagTextNormalizer.NormalizeContentAsync(text, cancellationToken);
public async Task<List<string>> NormalizeAndChunkAsync(string text, CancellationToken cancellationToken = default)
{
var chunks = await RagTextNormalizer.NormalizeAndChunkAsync(text, cancellationToken);
return chunks
.Where(chunk => chunk.Length >= 40)
.ToList();
}
public string NormalizeTitle(string title)
=> RagTextNormalizer.NormalizeTitle(title);
}
builder.Services.AddSingleton<IAITextNormalizer, HeadingAwareTextNormalizer>();
RagTextNormalizer ships in the Markdown package, so a custom normalizer that reuses it still needs the package reference. Register the custom normalizer after AddMarkdown() so it is the last registration.
Troubleshooting
- Retrieved text still contains
**,#or HTML tags: Markdown normalization is not registered; addAddMarkdown(). - Chunks are larger or smaller than expected: the chunker is token-based (gpt-4o tokenizer), not character-based, except in the plain-text fallback.
- Existing indexes look different after enabling Markdown: content is chunked at index time; re-index data sources and documents to apply the new normalization.