forked from microsoft/markitdown
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMarkItDown.cs
More file actions
406 lines (351 loc) · 15.2 KB
/
MarkItDown.cs
File metadata and controls
406 lines (351 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
using Microsoft.Extensions.Logging;
using System.Text;
using MarkItDown.Converters;
namespace MarkItDown;
/// <summary>
/// Main class for converting various file formats to Markdown.
/// An extremely simple text-based document reader, suitable for LLM use.
/// This reader will convert common file-types or webpages to Markdown.
/// </summary>
public sealed class MarkItDown
{
private readonly List<ConverterRegistration> _converters;
private readonly ILogger? _logger;
private readonly HttpClient? _httpClient;
private readonly MarkItDownOptions _options;
/// <summary>
/// Initialize a new instance of MarkItDown.
/// </summary>
/// <param name="logger">Optional logger for diagnostic information.</param>
/// <param name="httpClient">Optional HTTP client for downloading web content.</param>
public MarkItDown(ILogger? logger = null, HttpClient? httpClient = null)
: this(null, logger, httpClient)
{
}
/// <summary>
/// Initialize a new instance of MarkItDown with advanced configuration options.
/// </summary>
/// <param name="options">Configuration overrides for the converter. When <see langword="null"/> defaults are used.</param>
/// <param name="logger">Optional logger for diagnostic information.</param>
/// <param name="httpClient">Optional HTTP client for downloading web content.</param>
public MarkItDown(MarkItDownOptions? options, ILogger? logger = null, HttpClient? httpClient = null)
{
_options = options ?? new MarkItDownOptions();
_logger = logger;
_httpClient = httpClient;
_converters = [];
if (_options.EnableBuiltins)
{
RegisterBuiltInConverters();
}
if (_options.EnablePlugins)
{
// TODO: parity with Python plugin discovery.
_logger?.LogWarning("Plugin support is not yet available in the .NET port.");
}
}
/// <summary>
/// Register a custom converter.
/// </summary>
/// <param name="converter">The converter to register.</param>
/// <param name="priority">The priority of the converter. Lower values are tried first.</param>
public void RegisterConverter(IDocumentConverter converter, double priority = ConverterPriority.SpecificFileFormat)
{
ArgumentNullException.ThrowIfNull(converter);
_converters.Add(new ConverterRegistration(converter, priority));
// Sort by priority to ensure proper order
_converters.Sort((a, b) => a.Priority.CompareTo(b.Priority));
}
/// <summary>
/// Register a custom converter with the converter's default priority.
/// </summary>
/// <param name="converter">The converter to register.</param>
public void RegisterConverter(IDocumentConverter converter)
{
ArgumentNullException.ThrowIfNull(converter);
RegisterConverter(converter, converter.Priority);
}
/// <summary>
/// Get the list of registered converters.
/// </summary>
/// <returns>A read-only list of registered converters.</returns>
public IReadOnlyList<IDocumentConverter> GetRegisteredConverters()
{
return _converters.Select(r => r.Converter).ToList().AsReadOnly();
}
/// <summary>
/// Convert a file to Markdown.
/// </summary>
/// <param name="filePath">Path to the file to convert.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>The converted Markdown content.</returns>
public Task<DocumentConverterResult> ConvertAsync(string filePath, CancellationToken cancellationToken = default)
=> ConvertFileInternalAsync(filePath, null, cancellationToken);
public Task<DocumentConverterResult> ConvertAsync(string filePath, StreamInfo streamInfoOverride, CancellationToken cancellationToken = default)
=> ConvertFileInternalAsync(filePath, streamInfoOverride, cancellationToken);
/// <summary>
/// Convert a stream to Markdown.
/// </summary>
/// <param name="stream">The stream to convert.</param>
/// <param name="streamInfo">Metadata about the stream.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>The converted Markdown content.</returns>
public async Task<DocumentConverterResult> ConvertAsync(Stream stream, StreamInfo streamInfo, CancellationToken cancellationToken = default)
{
if (!stream.CanSeek)
throw new ArgumentException("Stream must support seeking.", nameof(stream));
var exceptions = new List<Exception>();
var guesses = StreamInfoGuesser.Guess(stream, streamInfo);
foreach (var guess in guesses)
{
foreach (var registration in _converters)
{
cancellationToken.ThrowIfCancellationRequested();
try
{
if (stream.CanSeek)
{
stream.Position = 0;
}
if (!registration.Converter.Accepts(stream, guess, cancellationToken))
{
continue;
}
_logger?.LogDebug("Using converter {ConverterType} for {MimeType} {Extension}",
registration.Converter.GetType().Name,
guess.MimeType,
guess.Extension);
if (stream.CanSeek)
{
stream.Position = 0;
}
return await registration.Converter.ConvertAsync(stream, guess, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger?.LogWarning(ex, "Converter {ConverterType} failed", registration.Converter.GetType().Name);
exceptions.Add(ex);
}
}
}
var message = $"No converter available for file type. MimeType: {streamInfo.MimeType}, Extension: {streamInfo.Extension}";
if (exceptions.Count > 0)
{
throw new UnsupportedFormatException(message, new AggregateException(exceptions));
}
throw new UnsupportedFormatException(message);
}
/// <summary>
/// Convert content from a URL to Markdown.
/// </summary>
/// <param name="url">The URL to download and convert.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>The converted Markdown content.</returns>
public async Task<DocumentConverterResult> ConvertFromUrlAsync(string url, StreamInfo? streamInfoOverride = null, CancellationToken cancellationToken = default)
{
if (_httpClient is null)
throw new InvalidOperationException("HTTP client is required for URL conversion. Provide one in the constructor.");
using var response = await _httpClient.GetAsync(url, cancellationToken);
response.EnsureSuccessStatusCode();
using var contentStream = await response.Content.ReadAsStreamAsync(cancellationToken);
var streamInfo = CreateStreamInfoFromUrl(url, response);
if (streamInfoOverride is not null)
{
streamInfo = streamInfo.CopyWith(streamInfoOverride);
}
// Copy to memory stream to ensure we can seek
using var memoryStream = new MemoryStream();
await contentStream.CopyToAsync(memoryStream, cancellationToken);
memoryStream.Position = 0;
return await ConvertAsync(memoryStream, streamInfo, cancellationToken);
}
/// <summary>
/// Convert a generic URI (file, data, http, https) to Markdown.
/// </summary>
public async Task<DocumentConverterResult> ConvertUriAsync(string uri, StreamInfo? streamInfo = null, CancellationToken cancellationToken = default)
{
ArgumentException.ThrowIfNullOrEmpty(uri);
var trimmed = uri.Trim();
if (trimmed.StartsWith("file:", StringComparison.OrdinalIgnoreCase))
{
var localPath = UriUtilities.ResolveFilePath(trimmed);
return await ConvertFileInternalAsync(localPath, streamInfo, cancellationToken).ConfigureAwait(false);
}
if (trimmed.StartsWith("data:", StringComparison.OrdinalIgnoreCase))
{
var payload = UriUtilities.ParseDataUri(trimmed);
var extension = MimeMapping.GetExtension(payload.MimeType);
var normalizedExtension = string.IsNullOrWhiteSpace(extension) ? null : (extension.StartsWith('.') ? extension : "." + extension);
var baseInfo = new StreamInfo(
mimeType: payload.MimeType,
extension: normalizedExtension,
charset: TryGetEncoding(payload.Charset));
if (streamInfo is not null)
{
baseInfo = baseInfo.CopyWith(streamInfo);
}
using var buffer = new MemoryStream(payload.Data, writable: false);
return await ConvertAsync(buffer, baseInfo, cancellationToken).ConfigureAwait(false);
}
if (trimmed.StartsWith("http://", StringComparison.OrdinalIgnoreCase) || trimmed.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
{
return await ConvertFromUrlAsync(trimmed, streamInfo, cancellationToken).ConfigureAwait(false);
}
throw new ArgumentException($"Unsupported URI scheme for '{uri}'.", nameof(uri));
}
private void RegisterBuiltInConverters()
{
foreach (var converter in CreateBuiltInConverters())
{
RegisterConverter(converter);
}
}
private IEnumerable<IDocumentConverter> CreateBuiltInConverters()
{
IDocumentConverter CreateImageConverter() => new ImageConverter(_options.ExifToolPath, _options.ImageCaptioner);
IDocumentConverter CreateAudioConverter() => new AudioConverter(_options.ExifToolPath, _options.AudioTranscriber);
var converters = new List<IDocumentConverter>
{
new YouTubeUrlConverter(),
new HtmlConverter(),
new WikipediaConverter(),
new BingSerpConverter(),
new RssFeedConverter(),
new JsonConverter(),
new JupyterNotebookConverter(),
new CsvConverter(),
new EpubConverter(),
new EmlConverter(),
new XmlConverter(),
new ZipConverter(CreateZipInnerConverters(CreateImageConverter, CreateAudioConverter)),
new PdfConverter(),
new DocxConverter(),
new XlsxConverter(),
new PptxConverter(),
CreateAudioConverter(),
CreateImageConverter(),
new PlainTextConverter(),
};
return converters;
}
private IEnumerable<IDocumentConverter> CreateZipInnerConverters(Func<IDocumentConverter> imageConverterFactory, Func<IDocumentConverter> audioConverterFactory)
{
return new IDocumentConverter[]
{
new YouTubeUrlConverter(),
new HtmlConverter(),
new WikipediaConverter(),
new BingSerpConverter(),
new RssFeedConverter(),
new JsonConverter(),
new JupyterNotebookConverter(),
new CsvConverter(),
new EmlConverter(),
new XmlConverter(),
new PdfConverter(),
new DocxConverter(),
new XlsxConverter(),
new PptxConverter(),
audioConverterFactory(),
imageConverterFactory(),
new PlainTextConverter(),
};
}
private async Task<DocumentConverterResult> ConvertFileInternalAsync(string filePath, StreamInfo? overrides, CancellationToken cancellationToken)
{
if (!File.Exists(filePath))
{
throw new FileNotFoundException($"File not found: {filePath}");
}
using var fileStream = File.OpenRead(filePath);
var streamInfo = CreateStreamInfoFromFile(filePath);
if (overrides is not null)
{
streamInfo = streamInfo.CopyWith(overrides);
}
return await ConvertAsync(fileStream, streamInfo, cancellationToken).ConfigureAwait(false);
}
private static StreamInfo CreateStreamInfoFromFile(string filePath)
{
var extension = Path.GetExtension(filePath);
var filename = Path.GetFileName(filePath);
// Simple MIME type detection based on extension
var mimeType = GetMimeTypeFromExtension(extension);
return new StreamInfo(mimeType, extension, null, filename);
}
private static StreamInfo CreateStreamInfoFromUrl(string url, HttpResponseMessage response)
{
var uri = new Uri(url);
var extension = Path.GetExtension(uri.LocalPath);
var filename = Path.GetFileName(uri.LocalPath);
// Try to get MIME type from response
var mimeType = response.Content.Headers.ContentType?.MediaType ?? GetMimeTypeFromExtension(extension);
// Try to get charset from response
Encoding? charset = null;
var charsetName = response.Content.Headers.ContentType?.CharSet;
if (!string.IsNullOrEmpty(charsetName))
{
try
{
charset = Encoding.GetEncoding(charsetName);
}
catch
{
// Ignore invalid charset names
}
}
return new StreamInfo(mimeType, extension, charset, filename, url);
}
private static string? GetMimeTypeFromExtension(string? extension)
{
return extension?.ToLowerInvariant() switch
{
".txt" => "text/plain",
".md" => "text/markdown",
".markdown" => "text/markdown",
".html" => "text/html",
".htm" => "text/html",
".json" => "application/json",
".jsonl" => "application/json",
".ndjson" => "application/json",
".ipynb" => "application/x-ipynb+json",
".xml" => "application/xml",
".xsd" => "application/xml",
".xsl" => "application/xml",
".xslt" => "application/xml",
".rss" => "application/rss+xml",
".atom" => "application/atom+xml",
".csv" => "text/csv",
".zip" => "application/zip",
".epub" => "application/epub+zip",
".pdf" => "application/pdf",
".docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
".xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
".pptx" => "application/vnd.openxmlformats-officedocument.presentationml.presentation",
".jpg" => "image/jpeg",
".jpeg" => "image/jpeg",
".png" => "image/png",
".gif" => "image/gif",
".bmp" => "image/bmp",
".tiff" => "image/tiff",
".tif" => "image/tiff",
".webp" => "image/webp",
_ => MimeMapping.GetMimeType(extension)
};
}
private static Encoding? TryGetEncoding(string? charset)
{
if (string.IsNullOrWhiteSpace(charset))
{
return null;
}
try
{
return Encoding.GetEncoding(charset);
}
catch
{
return null;
}
}
}