|
| 1 | +using System.Collections.ObjectModel; |
| 2 | + |
| 3 | +namespace MarkItDown; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Represents the raw artifacts extracted during conversion prior to Markdown composition. |
| 7 | +/// </summary> |
| 8 | +public sealed class ConversionArtifacts |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Initializes a new instance of the <see cref="ConversionArtifacts"/> class. |
| 12 | + /// </summary> |
| 13 | + public ConversionArtifacts() |
| 14 | + { |
| 15 | + TextBlocks = new List<TextArtifact>(); |
| 16 | + Tables = new List<TableArtifact>(); |
| 17 | + Images = new List<ImageArtifact>(); |
| 18 | + Metadata = new Dictionary<string, string>(); |
| 19 | + } |
| 20 | + |
| 21 | + private ConversionArtifacts(bool _) |
| 22 | + { |
| 23 | + TextBlocks = EmptyTextBlocks; |
| 24 | + Tables = EmptyTables; |
| 25 | + Images = EmptyImages; |
| 26 | + Metadata = EmptyMetadata; |
| 27 | + } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Gets a reusable empty instance. |
| 31 | + /// </summary> |
| 32 | + public static ConversionArtifacts Empty { get; } = new(true); |
| 33 | + |
| 34 | + private static readonly IList<TextArtifact> EmptyTextBlocks = new ReadOnlyCollection<TextArtifact>(Array.Empty<TextArtifact>()); |
| 35 | + private static readonly IList<TableArtifact> EmptyTables = new ReadOnlyCollection<TableArtifact>(Array.Empty<TableArtifact>()); |
| 36 | + private static readonly IList<ImageArtifact> EmptyImages = new ReadOnlyCollection<ImageArtifact>(Array.Empty<ImageArtifact>()); |
| 37 | + private static readonly IDictionary<string, string> EmptyMetadata = new ReadOnlyDictionary<string, string>(new Dictionary<string, string>()); |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Gets the raw text artifacts captured from the source. |
| 41 | + /// </summary> |
| 42 | + public IList<TextArtifact> TextBlocks { get; } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Gets the tabular artifacts captured from the source. |
| 46 | + /// </summary> |
| 47 | + public IList<TableArtifact> Tables { get; } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Gets the image artifacts captured from the source. |
| 51 | + /// </summary> |
| 52 | + public IList<ImageArtifact> Images { get; } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Gets conversion-level metadata surfaced by the converter. |
| 56 | + /// </summary> |
| 57 | + public IDictionary<string, string> Metadata { get; } |
| 58 | +} |
| 59 | + |
| 60 | +/// <summary> |
| 61 | +/// Represents a block of text extracted from the source document. |
| 62 | +/// </summary> |
| 63 | +public sealed class TextArtifact |
| 64 | +{ |
| 65 | + public TextArtifact(string text, int? pageNumber = null, string? source = null, string? label = null) |
| 66 | + { |
| 67 | + Text = text ?? string.Empty; |
| 68 | + PageNumber = pageNumber; |
| 69 | + Source = source; |
| 70 | + Label = label; |
| 71 | + } |
| 72 | + |
| 73 | + public string Text { get; set; } |
| 74 | + |
| 75 | + public int? PageNumber { get; set; } |
| 76 | + |
| 77 | + public string? Source { get; set; } |
| 78 | + |
| 79 | + public string? Label { get; set; } |
| 80 | +} |
| 81 | + |
| 82 | +/// <summary> |
| 83 | +/// Represents tabular content extracted from the source document. |
| 84 | +/// </summary> |
| 85 | +public sealed class TableArtifact |
| 86 | +{ |
| 87 | + public TableArtifact(IList<IList<string>> rows, int? pageNumber = null, string? source = null, string? label = null) |
| 88 | + { |
| 89 | + Rows = rows ?? throw new ArgumentNullException(nameof(rows)); |
| 90 | + PageNumber = pageNumber; |
| 91 | + Source = source; |
| 92 | + Label = label; |
| 93 | + } |
| 94 | + |
| 95 | + public IList<IList<string>> Rows { get; } |
| 96 | + |
| 97 | + public int? PageNumber { get; set; } |
| 98 | + |
| 99 | + public string? Source { get; set; } |
| 100 | + |
| 101 | + public string? Label { get; set; } |
| 102 | +} |
| 103 | + |
| 104 | +/// <summary> |
| 105 | +/// Represents an image extracted from the source document. |
| 106 | +/// </summary> |
| 107 | +public sealed class ImageArtifact |
| 108 | +{ |
| 109 | + public ImageArtifact(byte[] data, string? contentType = null, int? pageNumber = null, string? source = null, string? label = null) |
| 110 | + { |
| 111 | + Data = data ?? throw new ArgumentNullException(nameof(data)); |
| 112 | + ContentType = contentType; |
| 113 | + PageNumber = pageNumber; |
| 114 | + Source = source; |
| 115 | + Label = label; |
| 116 | + Metadata = new Dictionary<string, string>(); |
| 117 | + } |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// Gets the raw binary data for the image. |
| 121 | + /// </summary> |
| 122 | + public byte[] Data { get; } |
| 123 | + |
| 124 | + /// <summary> |
| 125 | + /// Gets the content type associated with the image. |
| 126 | + /// </summary> |
| 127 | + public string? ContentType { get; set; } |
| 128 | + |
| 129 | + /// <summary> |
| 130 | + /// Gets or sets the page number that owns the image, when applicable. |
| 131 | + /// </summary> |
| 132 | + public int? PageNumber { get; set; } |
| 133 | + |
| 134 | + /// <summary> |
| 135 | + /// Gets or sets the logical source identifier for the image. |
| 136 | + /// </summary> |
| 137 | + public string? Source { get; set; } |
| 138 | + |
| 139 | + /// <summary> |
| 140 | + /// Gets or sets the friendly label for the image. |
| 141 | + /// </summary> |
| 142 | + public string? Label { get; set; } |
| 143 | + |
| 144 | + /// <summary> |
| 145 | + /// Gets or sets the enriched description generated for the image. |
| 146 | + /// </summary> |
| 147 | + public string? DetailedDescription { get; set; } |
| 148 | + |
| 149 | + /// <summary> |
| 150 | + /// Gets or sets a Mermaid diagram representation when the image depicts structured data. |
| 151 | + /// </summary> |
| 152 | + public string? MermaidDiagram { get; set; } |
| 153 | + |
| 154 | + /// <summary> |
| 155 | + /// Gets or sets additional textual extraction (such as OCR output). |
| 156 | + /// </summary> |
| 157 | + public string? RawText { get; set; } |
| 158 | + |
| 159 | + /// <summary> |
| 160 | + /// Gets metadata describing the image artifact. |
| 161 | + /// </summary> |
| 162 | + public IDictionary<string, string> Metadata { get; } |
| 163 | + |
| 164 | + /// <summary> |
| 165 | + /// Gets or sets the segment index that references this artifact within the composed output. |
| 166 | + /// </summary> |
| 167 | + public int? SegmentIndex { get; set; } |
| 168 | + |
| 169 | + /// <summary> |
| 170 | + /// Gets or sets the Markdown placeholder that was emitted during extraction for this image. |
| 171 | + /// </summary> |
| 172 | + public string? PlaceholderMarkdown { get; set; } |
| 173 | +} |
0 commit comments