|
| 1 | +using Microsoft.AspNetCore.Hosting.Server.Features; |
| 2 | +using Microsoft.AspNetCore.Hosting.Server; |
| 3 | +using Microsoft.Playwright; |
| 4 | +using System.Reflection; |
| 5 | +using Microsoft.AspNetCore.Builder; |
| 6 | +using Microsoft.Extensions.DependencyInjection; |
| 7 | +using Microsoft.AspNetCore.Hosting; |
| 8 | +using SiteGen.Core.Extensions; |
| 9 | + |
| 10 | +namespace SiteGen.Extensions.Markdown.Monaco; |
| 11 | + |
| 12 | +public class MonacoHost : IAsyncDisposable |
| 13 | +{ |
| 14 | + const string url = "http://127.0.0.1:0"; |
| 15 | + readonly DirectoryInfo directory = new DirectoryInfo(Path.Combine(Path.GetDirectoryName(Environment.ProcessPath), ".monaco")); |
| 16 | + IPage page; |
| 17 | + WebApplication app; |
| 18 | + static bool isInitialized; |
| 19 | + |
| 20 | + static readonly SemaphoreSlim semaphore = new SemaphoreSlim(1, 1); |
| 21 | + |
| 22 | + public MonacoHost(IPage page) |
| 23 | + { |
| 24 | + this.page = page; |
| 25 | + |
| 26 | + var options = new WebApplicationOptions |
| 27 | + { |
| 28 | + WebRootPath = directory.FullName, |
| 29 | + ContentRootPath = directory.FullName, |
| 30 | + ApplicationName = "monaco" |
| 31 | + }; |
| 32 | + |
| 33 | + if (!directory.Exists) directory.Create(); |
| 34 | + |
| 35 | + var builder = WebApplication.CreateSlimBuilder(options); |
| 36 | + |
| 37 | + builder.WebHost.UseUrls(url); |
| 38 | + |
| 39 | + app = builder.Build(); |
| 40 | + |
| 41 | + app |
| 42 | + .UseDefaultFiles() |
| 43 | + .UseStaticFiles(new StaticFileOptions { ServeUnknownFileTypes = true }); |
| 44 | + } |
| 45 | + |
| 46 | + public async ValueTask DisposeAsync() |
| 47 | + { |
| 48 | + await page.CloseAsync(); |
| 49 | + await app.StopAsync(); |
| 50 | + await app.DisposeAsync(); |
| 51 | + } |
| 52 | + |
| 53 | + public async Task<string> Highlight(string source, string language) |
| 54 | + { |
| 55 | + if (!isInitialized) |
| 56 | + { |
| 57 | + await semaphore.WaitAsync(); |
| 58 | + |
| 59 | + try |
| 60 | + { |
| 61 | + if (!isInitialized) |
| 62 | + { |
| 63 | + // Purge the directory |
| 64 | + directory.Delete(true); |
| 65 | + directory.Create(); |
| 66 | + |
| 67 | + var type = GetType()!; |
| 68 | + var resourceNameBase = type.Namespace! + ".dist"; |
| 69 | + |
| 70 | + // Write resources to the root directory |
| 71 | + var assembly = Assembly.GetExecutingAssembly(); |
| 72 | + foreach (var name in assembly.GetManifestResourceNames()) |
| 73 | + { |
| 74 | + var ext = Path.GetExtension(name); |
| 75 | + var filename = Path.GetFileNameWithoutExtension(name.AsSpan().StripStart(resourceNameBase.AsSpan()).ToString()).TrimStart('.'); |
| 76 | + |
| 77 | + using var stream = assembly.GetManifestResourceStream(name); |
| 78 | + using var destination = File.Open(Path.Combine(directory.FullName, $"{filename}{ext}"), FileMode.Create, FileAccess.Write); |
| 79 | + stream.CopyTo(destination); |
| 80 | + stream.Flush(); |
| 81 | + } |
| 82 | + |
| 83 | + await Task.Run(() => app.StartAsync()); |
| 84 | + |
| 85 | + // Get the address that was bound to (random port) |
| 86 | + var server = app.Services.GetRequiredService<IServer>(); |
| 87 | + var addressFeature = server.Features.Get<IServerAddressesFeature>()!; |
| 88 | + var address = addressFeature.Addresses.Single(); |
| 89 | + |
| 90 | + await page.GotoAsync(address); |
| 91 | + await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded); |
| 92 | + await page.WaitForLoadStateAsync(LoadState.NetworkIdle); |
| 93 | + await page.WaitForLoadStateAsync(LoadState.Load); |
| 94 | + |
| 95 | + isInitialized = true; |
| 96 | + } |
| 97 | + } |
| 98 | + finally |
| 99 | + { |
| 100 | + semaphore.Release(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // Set the source code |
| 105 | + return await page.EvaluateAsync<string>($@"(source) => {{ |
| 106 | + return monaco.editor.colorize(source, ""{language.ToLowerInvariant()}\\"", {{}}); |
| 107 | +}}", source); |
| 108 | + } |
| 109 | +} |
0 commit comments