Skip to content

Commit

Permalink
add Playwright-powered HtmlKernel
Browse files Browse the repository at this point in the history
  • Loading branch information
jonsequitur committed Aug 25, 2022
1 parent eba835d commit bbc97dc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/dotnet-repl/HtmlKernel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Threading.Tasks;
using Microsoft.DotNet.Interactive;
using Microsoft.DotNet.Interactive.Commands;

namespace dotnet_repl;

public class HtmlKernel : Kernel, IKernelCommandHandler<SubmitCode>
{
private readonly Kernel _innerJsKernel;

public HtmlKernel(Kernel innerJsKernel) : base("html", "HTML", "5.0")
{
_innerJsKernel = innerJsKernel;
}

public async Task HandleAsync(SubmitCode command, KernelInvocationContext context)
{
var jsCode = $"await dotnetInteractive.domHtmlFragmentProcessor('{command.Code}');";

await _innerJsKernel.SendAsync(new SubmitCode(jsCode));
}
}
2 changes: 2 additions & 0 deletions src/dotnet-repl/KernelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public static CompositeKernel CreateKernel(StartupOptions? options = null)

var playwrightKernel = Task.Run(() => new PlaywrightKernelConnector().CreateKernelAsync("javascript")).Result;
compositeKernel.Add(playwrightKernel, new[] { "js" });
playwrightKernel.KernelInfo.SupportedKernelCommands.Add(new KernelCommandInfo("SubmitCode"));
compositeKernel.Add(new HtmlKernel(playwrightKernel));
compositeKernel.Add(new MarkdownKernel());
compositeKernel.Add(new SqlDiscoverabilityKernel());
compositeKernel.Add(new KqlDiscoverabilityKernel());
Expand Down
10 changes: 10 additions & 0 deletions src/dotnet-repl/KernelSpecificTheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public abstract class KernelSpecificTheme : Theme
"pwsh" => new PowerShellTheme(),
"javascript" => new JavaScriptTheme(),
"sql" => new SqlTheme(),
"html" => new HtmlTheme(),
_ => null
};
}
Expand Down Expand Up @@ -73,6 +74,15 @@ public class JavaScriptTheme : KernelSpecificTheme
public override string PromptText => "JS";
}

public class HtmlTheme : KernelSpecificTheme
{
public override Style AccentStyle => new(Color.Red3);

public override string KernelDisplayName => "HTML";

public override string PromptText => "HTML";
}

public class SqlTheme : KernelSpecificTheme
{
public override Style AccentStyle => new(Color.Yellow3);
Expand Down

0 comments on commit bbc97dc

Please sign in to comment.