Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/enable math #243

Merged
merged 8 commits into from
May 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 43 additions & 0 deletions MLS.Agent.Tests/Markdown/MarkdownFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,49 @@ static void MyProgram(string[] args)
buttons.Should().BeNull();
}

[Fact]
public async Task Should_emit_math_inline_block_rendering()
{
var html = await RenderHtml(
("Readme.md", @"this is math inline $$\sum ^{n}_{i=0}\left(x_{i}+a_{i}y_{i}\right)$$"));

var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(html);

var math = htmlDocument.DocumentNode
.SelectSingleNode("//span");

math.HasClass("math").Should().BeTrue();
math.InnerText.Should().Match(@"\(\sum ^{n}_{i=0}\left(x_{i}+a_{i}y_{i}\right)\)");
}

[Fact]
public async Task Should_emit_math_block_rendering()
{
var html = await RenderHtml(
("Readme.md", @"$$
\begin{equation}
\int_0^\infty \frac{x^3}{e^x-1}\,dx = \frac{\pi^4}{15}
\label{eq:sample}
\end{equation}
$$"));

var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(html);

var math = htmlDocument.DocumentNode
.SelectSingleNode("//div");

math.HasClass("math").Should().BeTrue();
math.InnerText.EnforceLF().Should().Match(@"
\[
\begin{equation}
\int_0^\infty \frac{x^3}{e^x-1}\,dx = \frac{\pi^4}{15}
\label{eq:sample}
\end{equation}
\]".EnforceLF());
}

[Fact]
public async Task Should_emit_pre_style_for_hidden_blocks()
{
Expand Down
12 changes: 12 additions & 0 deletions MLS.Agent/Controllers/DocumentationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public static async Task<IHtmlContent> SessionControlsHtml(MarkdownFile markdown
<script src=""/api/trydotnet.min.js?v={_cacheBuster}""></script>
<link rel=""stylesheet"" href=""/css/trydotnet.css?v={_cacheBuster}"">
<link rel=""icon"" type=""image/png"" href=""favicon-32x32.png"">
{MathSupport()}
<title>dotnet try - {markdownFile.Path.Value.HtmlEncode()}</title>
</head>

Expand All @@ -138,6 +139,17 @@ public static async Task<IHtmlContent> SessionControlsHtml(MarkdownFile markdown

</html>".ToHtmlContent();

private IHtmlContent MathSupport() =>
@"
<script type=""text/x-mathjax-config"">
MathJax.Hub.Config({
tex2jax: {inlineMath: [[""$"",""$""],[""\\("",""\\)""]]},
showMathMenu: false,
showMathMenuMSIE: false
});
</script>
<script type=""text/javascript"" src=""https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS_SVG""></script>".ToHtmlContent();

private async Task<IHtmlContent> OneColumnLayoutScaffold(string hostUrl, MarkdownFile markdownFile) =>
Layout(hostUrl, markdownFile,
$@"
Expand Down
2 changes: 1 addition & 1 deletion MLS.Agent/MLS.Agent.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Markdig" Version="0.16.0" />
<PackageReference Include="Markdig" Version="0.17.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.3.0" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Blazor.Server" Version="0.7.0" />
Expand Down
12 changes: 7 additions & 5 deletions MLS.Agent/Markdown/MarkdownProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Reactive.Disposables;
using System.Threading.Tasks;
using Markdig;
using Markdig.Extensions.Mathematics;
using Microsoft.DotNet.Try.Markdown;
using Recipes;
using WorkspaceServer;
Expand Down Expand Up @@ -71,7 +72,7 @@ public void WriteAllText(RelativeFilePath path, string text)
{
}
}

internal IDirectoryAccessor DirectoryAccessor { get; }

private readonly PackageRegistry _packageRegistry;
Expand All @@ -85,7 +86,7 @@ internal MarkdownProject(PackageRegistry packageRegistry) : this(new NullDirecto
}

public MarkdownProject(
IDirectoryAccessor directoryAccessor,
IDirectoryAccessor directoryAccessor,
PackageRegistry packageRegistry,
IDefaultCodeBlockAnnotations defaultAnnotations = null)
{
Expand Down Expand Up @@ -118,11 +119,12 @@ internal MarkdownPipeline GetMarkdownPipelineFor(RelativeFilePath filePath)
var relativeAccessor = DirectoryAccessor.GetDirectoryAccessorForRelativePath(filePath.Directory);

return new MarkdownPipelineBuilder()
.UseAdvancedExtensions()
.UseCodeBlockAnnotations(
relativeAccessor,
_packageRegistry,
relativeAccessor,
_packageRegistry,
defaultAnnotations)
.UseMathematics()
.UseAdvancedExtensions()
.Build();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageReference Include="Assent" Version="1.3.1" />
<PackageReference Include="HtmlAgilityPack" Version="1.9.0" />
<PackageReference Include="FluentAssertions" Version="5.6.0" />
<PackageReference Include="Markdig" Version="0.16.0" />
<PackageReference Include="Markdig" Version="0.17.0" />
</ItemGroup>

<ItemGroup>
Expand Down
5 changes: 1 addition & 4 deletions Microsoft.DotNet.Try.Markdown/AnnotatedCodeBlockParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ public AnnotatedCodeBlockParser(CodeFenceAnnotationsParser codeFenceAnnotationsP
protected override AnnotatedCodeBlock CreateFencedBlock(BlockProcessor processor) =>
new AnnotatedCodeBlock(this, _order++);

protected bool ParseCodeOptions(
BlockProcessor state,
ref StringSlice line,
IFencedBlock fenced)
protected bool ParseCodeOptions(BlockProcessor state, ref StringSlice line, IFencedBlock fenced, char openingCharacter)
{
if (!(fenced is AnnotatedCodeBlock codeLinkBlock))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Markdig" Version="0.16.0" />
<PackageReference Include="Markdig" Version="0.17.0" />
<PackageReference Include="System.CommandLine.Experimental" Version="0.2.0-alpha.19261.1" />
<PackageReference Include="Microsoft.AspNetCore.Html.Abstractions" Version="2.1.1" />
</ItemGroup>
Expand Down
15 changes: 15 additions & 0 deletions docs/Learn math.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# dotnet try

Learn math with .NET.

Given the following equation

$$
\sum ^{20}_{i=0}\left(x_{i}+a_{i}y_{i}\right)
$$

create an implementation using real code


```csharp --source-file ./samples/BasicConsole/Program.cs --project ./samples/BasicConsole/BasicConsole.csproj --region wat --session "say meow..."
```