Write the sources of an answer into every exported document

This commit is contained in:
Thorsten Sommer committed 2026-09-13 19:10:45 +02:00
1 parent bb3ec5da92
commit efac4d57f1
8 files changed
+103 -3

No files matched your search

+25
View File
@@ -1,4 +1,5 @@
using Markdig;
using Markdig.Syntax;
using System.Text;
namespace AIStudio.Tools;
@@ -58,6 +59,30 @@ public static class Markdown
return escaped.ToString();
}
/// <summary>Closes a code fence which the text opened but never closed.</summary>
/// <remarks>
/// An unclosed fence runs to the end of the document, so anything appended after it would be
/// read as code instead of as Markdown. The chat never shows this, because it renders the answer
/// and what belongs below it separately. A document is one text, and there an answer which ends
/// in an open fence would swallow whatever follows it.
/// </remarks>
/// <param name="markdownText">The Markdown text to inspect.</param>
/// <returns>The text with its open fence closed, or the text itself when no fence is open.</returns>
public static string CloseOpenCodeFence(string markdownText)
{
if (string.IsNullOrWhiteSpace(markdownText))
return markdownText;
var document = Markdig.Markdown.Parse(markdownText, SAFE_MARKDOWN_PIPELINE);
// Only the last fence of a text can be an open one: an open fence takes everything
// after it with it, so no other block is able to follow it.
if (document.Descendants<FencedCodeBlock>().LastOrDefault() is not { ClosingFencedCharCount: 0 } openFence)
return markdownText;
return $"{markdownText}{Environment.NewLine}{new string(openFence.FencedChar, openFence.OpeningFencedCharCount)}";
}
public static string RemoveSharedIndentation(string value)
{
if (string.IsNullOrWhiteSpace(value))
+1 -1
View File
@@ -118,7 +118,7 @@ public static class PandocExport
// We read the text before we ask for a path: when there is nothing to convert, the user
// should learn that right away instead of picking a file first and getting an error afterwards.
//
if (!markdownContent.TryGetMarkdownText(out var markdownText))
if (!markdownContent.TryGetExportMarkdown(out var markdownText))
{
LOGGER.LogWarning("Cannot export the content as {ExportFormat}, because it carries no text.", format);
await MessageBus.INSTANCE.SendError(new(Icons.Material.Filled.Cancel, TB("Only text messages can be exported.")));
@@ -153,6 +153,26 @@ public static partial class SourceExtensions
return sb.ToString();
}
/// <summary>
/// Converts a list of sources to a markdown-formatted string, headed by a title of its own.
/// </summary>
/// <remarks>
/// The chat shows the sources in a box below the answer, so the reader sees where the one ends
/// and the others begin. An exported document is one text: without a heading of its own, the
/// source list would read like one more section the model wrote. This is why the export asks
/// for this and the chat does not.
/// </remarks>
/// <param name="sources">The list of sources to convert.</param>
/// <returns>A markdown-formatted string representing the sources, or an empty string when there are none.</returns>
public static string ToExportMarkdown(this IList<Source> sources)
{
var sourcesMarkdown = sources.ToMarkdown();
if (string.IsNullOrWhiteSpace(sourcesMarkdown))
return string.Empty;
return $"# {TB("Sources")}{Environment.NewLine}{Environment.NewLine}{sourcesMarkdown}";
}
/// <summary>
/// Merges a list of added sources into an existing list of sources, avoiding duplicates based on normalized URLs.
/// </summary>