Keep `file:///` for absolute paths in HTML exports

Closes #1155
pull/1160/head
Tyrrrz 11 months ago
parent 6508455f3a
commit 129df42de4

@ -124,9 +124,9 @@ internal class ExportContext
var filePath = await _assetDownloader.DownloadAsync(url, cancellationToken); var filePath = await _assetDownloader.DownloadAsync(url, cancellationToken);
var relativeFilePath = Path.GetRelativePath(Request.OutputDirPath, filePath); var relativeFilePath = Path.GetRelativePath(Request.OutputDirPath, filePath);
// Prefer relative paths so that the output files can be copied around without breaking references. // Prefer the relative path so that the export package can be copied around without breaking references.
// If the asset directory is outside of the export directory, use an absolute path instead. // However, if the assets directory lies outside of the export directory, use the absolute path instead.
var optimalFilePath = var shouldUseAbsoluteFilePath =
relativeFilePath.StartsWith( relativeFilePath.StartsWith(
".." + Path.DirectorySeparatorChar, ".." + Path.DirectorySeparatorChar,
StringComparison.Ordinal StringComparison.Ordinal
@ -134,15 +134,19 @@ internal class ExportContext
|| relativeFilePath.StartsWith( || relativeFilePath.StartsWith(
".." + Path.AltDirectorySeparatorChar, ".." + Path.AltDirectorySeparatorChar,
StringComparison.Ordinal StringComparison.Ordinal
) );
? filePath
: relativeFilePath; var optimalFilePath = shouldUseAbsoluteFilePath ? filePath : relativeFilePath;
// For HTML, the path needs to be properly formatted // For HTML, the path needs to be properly formatted
if (Request.Format is ExportFormat.HtmlDark or ExportFormat.HtmlLight) if (Request.Format is ExportFormat.HtmlDark or ExportFormat.HtmlLight)
{ {
// Create a 'file:///' URI and then strip the 'file:///' prefix to allow for relative paths // Format the path into a valid file URI
return new Uri(new Uri("file:///"), optimalFilePath).ToString()[8..]; var href = new Uri(new Uri("file:///"), optimalFilePath).ToString();
// File schema does not support relative paths, so strip it if that's the case
// https://github.com/Tyrrrz/DiscordChatExporter/issues/1155
return shouldUseAbsoluteFilePath ? href : href[8..];
} }
return optimalFilePath; return optimalFilePath;

Loading…
Cancel
Save