|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using OwlCore.Storage; |
| 6 | +using WindowsAppCommunity.Blog.Assets; |
| 7 | +using WindowsAppCommunity.Blog.PostPage; |
| 8 | + |
| 9 | +namespace WindowsAppCommunity.Blog.Page |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Asset-aware virtual HTML file - extends base with link detection, asset resolution, and inclusion decisions. |
| 13 | + /// Sealed - this is the final asset-aware implementation. |
| 14 | + /// Implements link rewriting and asset tracking during post-processing. |
| 15 | + /// </summary> |
| 16 | + public sealed class AssetAwareHtmlTemplatedMarkdownFile : HtmlTemplatedMarkdownFile |
| 17 | + { |
| 18 | + private readonly List<IFile> _includedAssets = new(); |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Creates asset-aware virtual HTML file with lazy markdown→HTML generation and asset management. |
| 22 | + /// </summary> |
| 23 | + /// <param name="id">Unique identifier for this file (parent-derived)</param> |
| 24 | + /// <param name="markdownSource">Source markdown file to transform</param> |
| 25 | + /// <param name="templateSource">Template as IFile or IFolder</param> |
| 26 | + /// <param name="templateFileName">Template file name when source is IFolder (defaults to "template.html")</param> |
| 27 | + /// <param name="parent">Parent folder in virtual hierarchy (optional)</param> |
| 28 | + public AssetAwareHtmlTemplatedMarkdownFile( |
| 29 | + string id, |
| 30 | + IFile markdownSource, |
| 31 | + IStorable templateSource, |
| 32 | + string? templateFileName = null, |
| 33 | + IFolder? parent = null) |
| 34 | + : base(id, markdownSource, templateSource, templateFileName, parent) |
| 35 | + { |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Asset link detector for finding relative links in rendered HTML output. |
| 40 | + /// </summary> |
| 41 | + public required IAssetLinkDetector LinkDetector { get; init; } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Asset resolver for converting paths to IFile instances. |
| 45 | + /// </summary> |
| 46 | + public required IAssetResolver Resolver { get; init; } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Inclusion strategy for deciding include vs reference via path rewriting. |
| 50 | + /// </summary> |
| 51 | + public required IAssetInclusionStrategy InclusionStrategy { get; init; } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Assets that were decided for inclusion (self-contained in page folder). |
| 55 | + /// Exposed to containing folder for yielding in virtual structure. |
| 56 | + /// </summary> |
| 57 | + public IReadOnlyCollection<IFile> IncludedAssets => _includedAssets.AsReadOnly(); |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Post-process HTML with asset management pipeline. |
| 61 | + /// Detects links → Resolves to files → Decides include/reference via path rewriting → Tracks included assets. |
| 62 | + /// </summary> |
| 63 | + /// <param name="html">Rendered HTML from template</param> |
| 64 | + /// <param name="model">Data model used for rendering</param> |
| 65 | + /// <param name="ct">Cancellation token</param> |
| 66 | + /// <returns>Post-processed HTML with rewritten links</returns> |
| 67 | + protected override async Task<string> PostProcessHtmlAsync(string html, PostPageDataModel model, CancellationToken ct) |
| 68 | + { |
| 69 | + // Clear included assets from any previous generation |
| 70 | + _includedAssets.Clear(); |
| 71 | + |
| 72 | + // Detect asset links in rendered HTML output (pass self as IFile) |
| 73 | + await foreach (var originalPath in LinkDetector.DetectAsync(this, ct)) |
| 74 | + { |
| 75 | + // Resolve path to IFile |
| 76 | + var resolvedAsset = await Resolver.ResolveAsync(originalPath, ct); |
| 77 | + |
| 78 | + // Null resolver policy: Skip if not found (preserve broken link) |
| 79 | + if (resolvedAsset == null) |
| 80 | + { |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + // Strategy decides include vs reference by returning rewritten path |
| 85 | + // Path structure determines behavior: |
| 86 | + // - Child path (no ../ prefix): Include |
| 87 | + // - Parent path (../ prefix): Reference |
| 88 | + var rewrittenPath = await InclusionStrategy.DecideAsync( |
| 89 | + MarkdownSource, // Pass original markdown source (not virtual HTML) |
| 90 | + resolvedAsset, |
| 91 | + originalPath, |
| 92 | + ct); |
| 93 | + |
| 94 | + // Implicit decision based on path structure |
| 95 | + if (!rewrittenPath.StartsWith("../")) |
| 96 | + { |
| 97 | + // Include: Add to tracked assets (will be yielded by containing folder) |
| 98 | + _includedAssets.Add(resolvedAsset); |
| 99 | + } |
| 100 | + // Reference: Asset not added to included list (stays external) |
| 101 | + |
| 102 | + // Rewrite link in HTML (applies to both Include and Reference) |
| 103 | + html = html.Replace(originalPath, rewrittenPath); |
| 104 | + } |
| 105 | + |
| 106 | + return html; |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments