using System.Net;
using System.Text.Json;
using XFESystem.Documents;
if (args.Length != 1) throw new ArgumentException("Specify a fresh output directory for host-only browser fixtures.");
var output = Path.GetFullPath(args[0]);
if (Directory.Exists(output)) throw new IOException("The browser fixture directory must be new.");
Directory.CreateDirectory(output);
var builder = new DocumentPreviewBuilder(new());
var markdown = builder.Markdown("""
# XFESystem 文档
| Language | Status |
|---|---|
| C# | Ready |
```js
const greeting = "Hello XFE";
console.log(greeting);
```
Inline math: $a^2+b^2=c^2$.
```mermaid
flowchart LR
A[Markdown] --> B[Preview]
```
<script>globalThis.MarkdownScriptRan=true</script>
<img src="https://outside.invalid/Blocked.png" onerror="globalThis.MarkdownEventRan=true">
""", "XFESystem Markdown");
await File.WriteAllTextAsync(Path.Combine(output, "Markdown.html"), markdown.Html);
var code = builder.Code("public sealed class Sample { public string Name => \"XFE\"; }", "Sample.cs");
await File.WriteAllTextAsync(Path.Combine(output, "Code.html"), code.Html);
var plan = DocumentPreviewBuilder.JavaScript("""
console.log("Result", 6 * 7);
document.body.dataset.Executed = "yes";
fetch("https://outside.invalid/Blocked").catch(() => console.log("Network blocked"));
console.log("Host bridge", typeof window.chrome?.webview);
""");
var jsHost = $"<!doctype html><html><head><meta charset=\"utf-8\"><title>Isolated JS host test</title></head><body><iframe title=\"JavaScript preview\" sandbox=\"{plan.IframeSandbox}\" style=\"width:95vw;height:80vh\" srcdoc=\"{WebUtility.HtmlEncode(plan.Html)}\"></iframe></body></html>";
await File.WriteAllTextAsync(Path.Combine(output, "JavaScript.html"), jsHost);
await File.WriteAllTextAsync(Path.Combine(output, "ExecutionRequirements.json"), JsonSerializer.Serialize(plan, new JsonSerializerOptions { WriteIndented = true }));
using var manifest = JsonDocument.Parse(WebsiteAssets.ReadText("SourceManifest.json"));
Directory.CreateDirectory(Path.Combine(output, "Assets"));
await File.WriteAllTextAsync(Path.Combine(output, "Assets", "SourceManifest.json"), WebsiteAssets.ReadText("SourceManifest.json"));
foreach (var entry in manifest.RootElement.GetProperty("Entries").EnumerateArray())
{
var name = entry.GetProperty("SnapshotPath").GetString()!;
if (name.StartsWith("../", StringComparison.Ordinal)) continue;
var path = Path.Combine(output, "Assets", name.Replace('/', Path.DirectorySeparatorChar));
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
using var resource = WebsiteAssets.Open(name);
await using var file = File.Create(path);
await resource.CopyToAsync(file);
}
Console.WriteLine(output);
using System.Net;
using System.Text.Json;
using XFESystem.Documents;
if (args.Length != 1) throw new ArgumentException("Specify a fresh output directory for host-only browser fixtures.");
var output = Path.GetFullPath(args[0]);
if (Directory.Exists(output)) throw new IOException("The browser fixture directory must be new.");
Directory.CreateDirectory(output);
var builder = new DocumentPreviewBuilder(new());
var markdown = builder.Markdown("""
# XFESystem 文档
| Language | Status |
|---|---|
| C# | Ready |
```js
const greeting = "Hello XFE";
console.log(greeting);
```
Inline math: $a^2+b^2=c^2$.
```mermaid
flowchart LR
A[Markdown] --> B[Preview]
```
<script>globalThis.MarkdownScriptRan=true</script>
<img src="https://outside.invalid/Blocked.png" onerror="globalThis.MarkdownEventRan=true">
""", "XFESystem Markdown");
await File.WriteAllTextAsync(Path.Combine(output, "Markdown.html"), markdown.Html);
var code = builder.Code("public sealed class Sample { public string Name => \"XFE\"; }", "Sample.cs");
await File.WriteAllTextAsync(Path.Combine(output, "Code.html"), code.Html);
var plan = DocumentPreviewBuilder.JavaScript("""
console.log("Result", 6 * 7);
document.body.dataset.Executed = "yes";
fetch("https://outside.invalid/Blocked").catch(() => console.log("Network blocked"));
console.log("Host bridge", typeof window.chrome?.webview);
""");
var jsHost = $"<!doctype html><html><head><meta charset=\"utf-8\"><title>Isolated JS host test</title></head><body><iframe title=\"JavaScript preview\" sandbox=\"{plan.IframeSandbox}\" style=\"width:95vw;height:80vh\" srcdoc=\"{WebUtility.HtmlEncode(plan.Html)}\"></iframe></body></html>";
await File.WriteAllTextAsync(Path.Combine(output, "JavaScript.html"), jsHost);
await File.WriteAllTextAsync(Path.Combine(output, "ExecutionRequirements.json"), JsonSerializer.Serialize(plan, new JsonSerializerOptions { WriteIndented = true }));
using var manifest = JsonDocument.Parse(WebsiteAssets.ReadText("SourceManifest.json"));
Directory.CreateDirectory(Path.Combine(output, "Assets"));
await File.WriteAllTextAsync(Path.Combine(output, "Assets", "SourceManifest.json"), WebsiteAssets.ReadText("SourceManifest.json"));
foreach (var entry in manifest.RootElement.GetProperty("Entries").EnumerateArray())
{
var name = entry.GetProperty("SnapshotPath").GetString()!;
if (name.StartsWith("../", StringComparison.Ordinal)) continue;
var path = Path.Combine(output, "Assets", name.Replace('/', Path.DirectorySeparatorChar));
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
using var resource = WebsiteAssets.Open(name);
await using var file = File.Create(path);
await resource.CopyToAsync(file);
}
Console.WriteLine(output);