-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnownAssetStrategy.cs
More file actions
92 lines (74 loc) · 3.25 KB
/
KnownAssetStrategy.cs
File metadata and controls
92 lines (74 loc) · 3.25 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
using OwlCore.Diagnostics;
using OwlCore.Storage;
namespace WindowsAppCommunity.Blog.Assets;
/// <summary>
/// Determines fallback asset behavior when the asset is not known to the strategy selector.
/// </summary>
public enum AssetFallbackBehavior
{
/// <summary>
/// The asset path is rewritten to support being referenced by the folderized markdown.
/// </summary>
Reference,
/// <summary>
/// The asset path is not rewritten and it is included in the output path.
/// </summary>
Include,
/// <summary>
/// The new asset path is returned as null and the asset is not included in the output.
/// </summary>
Drop,
}
/// <summary>
/// Uses a known list of files to decide between asset inclusion (child path) vs asset reference (parented path).
/// </summary>
public sealed class KnownAssetStrategy : IAssetStrategy
{
/// <summary>
/// A list of known file IDs to rewrite to an included asset.
/// </summary>
public HashSet<string> IncludedAssetFileIds { get; set; } = new();
/// <summary>
/// A list of known file IDs rewrite as a referenced asset.
/// </summary>
public HashSet<string> ReferencedAssetFileIds { get; set; } = new();
/// <summary>
/// The strategy to use when encountering an unknown asset.
/// </summary>
public FaultStrategy UnknownAssetFaultStrategy { get; set; }
/// <summary>
/// Gets or sets the fallback used when the asset is unknown but <see cref="UnknownAssetFaultStrategy"/> does not have <see cref="FaultStrategy.Throw"/>.
/// </summary>
public AssetFallbackBehavior UnknownAssetFallbackStrategy { get; set; }
/// <inheritdoc/>
public async Task<string?> DecideAsync(IFile referencingMarkdown, IFile referencedAsset, string originalPath, CancellationToken ct = default)
{
if (string.IsNullOrWhiteSpace(originalPath))
return originalPath;
var isReferenced = ReferencedAssetFileIds.Contains(referencedAsset.Id);
var isIncluded = IncludedAssetFileIds.Contains(referencedAsset.Id);
if (isReferenced)
return $"../{originalPath}";
if (isIncluded)
return originalPath;
// Handle as unknown
HandleUnknownAsset(referencedAsset);
return UnknownAssetFallbackStrategy switch
{
AssetFallbackBehavior.Reference => $"../{originalPath}",
AssetFallbackBehavior.Include => originalPath,
AssetFallbackBehavior.Drop => null,
_ => throw new ArgumentOutOfRangeException(nameof(UnknownAssetFallbackStrategy)),
};
}
private void HandleUnknownAsset(IFile referencedAsset)
{
var faultMessage = $"Unknown asset encountered: {nameof(referencedAsset.Name)} {referencedAsset.Name}, {nameof(referencedAsset.Id)} {referencedAsset.Id}. Please add this ID to either {nameof(IncludedAssetFileIds)} or {nameof(ReferencedAssetFileIds)}.";
if (UnknownAssetFaultStrategy.HasFlag(FaultStrategy.LogWarn))
Logger.LogWarning(faultMessage);
if (UnknownAssetFaultStrategy.HasFlag(FaultStrategy.LogError))
Logger.LogError(faultMessage);
if (UnknownAssetFaultStrategy.HasFlag(FaultStrategy.Throw))
throw new InvalidOperationException(faultMessage);
}
}