Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions JitHub.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
<Platform Solution="Debug|Any CPU" Project="x64" />
<Platform Solution="Release|Any CPU" Project="x86" />
</Project>
<Project Path="MarkdownRenderer/MarkdownRenderer.SyntaxHighlighting.TextMate/MarkdownRenderer.SyntaxHighlighting.TextMate.csproj">
<Platform Solution="*|ARM" Project="x86" />
<Platform Solution="*|ARM64" Project="ARM64" />
<Platform Solution="*|x64" Project="x64" />
<Platform Solution="*|x86" Project="x86" />
<Platform Solution="Debug|Any CPU" Project="x64" />
<Platform Solution="Release|Any CPU" Project="x86" />
</Project>
<Project Path="MarkdownRenderer/MarkdownRenderer.Tests/MarkdownRenderer.Tests.csproj">
<Platform Solution="*|x64" Project="x64" />
<Platform Solution="Debug|Any CPU" Project="x64" />
Expand Down
16 changes: 13 additions & 3 deletions MarkdownRenderer/MarkdownRenderer.Gfm/GfmChildBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ private static StackBox BuildContainer(ContainerBlock cb, MarkdownLayoutContext
return stack;
}

internal static void AddInlines(InlineContainerBox box, ContainerInline inlines, System.Func<Inline, bool>? skipFirstIf = null)
internal static void AddInlines(
InlineContainerBox box,
ContainerInline inlines,
System.Func<Inline, bool>? skipFirstIf = null,
int inheritedAliasStart = -1)
{
bool skippedFirst = skipFirstIf is null;
foreach (var i in inlines)
Expand All @@ -101,10 +105,17 @@ internal static void AddInlines(InlineContainerBox box, ContainerInline inlines,
var run = BuildInline(i, box.Context);
if (run is not null)
{
run.SetStyleAliases(box.Context.CreateStyleAliasSnapshotFrom(aliasStart));
int effectiveAliasStart = inheritedAliasStart >= 0 ? inheritedAliasStart : aliasStart;
run.SetStyleAliases(box.Context.CreateStyleAliasSnapshotFrom(effectiveAliasStart));
box.Context.RegisterMarkdownAttributes(i, box.BlockIndex);
box.Add(run);
}
else if (i is ContainerInline nested)
{
box.Context.RegisterMarkdownAttributes(i, box.BlockIndex);
int effectiveAliasStart = inheritedAliasStart >= 0 ? inheritedAliasStart : aliasStart;
AddInlines(box, nested, inheritedAliasStart: effectiveAliasStart);
}
}
}

Expand Down Expand Up @@ -135,7 +146,6 @@ internal static void AddInlines(InlineContainerBox box, ContainerInline inlines,
{
SourceSpan = new MarkdownRenderer.SourceSpan(inline.Span.Start, inline.Span.Length)
},
ContainerInline ci2 => FlattenAsTextRun(ci2),
_ => null
};

Expand Down
59 changes: 49 additions & 10 deletions MarkdownRenderer/MarkdownRenderer.Sample/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using MarkdownRenderer.Controls;
using MarkdownRenderer.Gfm;
using MarkdownRenderer.Parsing;
using MarkdownRenderer.SyntaxHighlighting.TextMate;
using MarkdownRenderer.Theming;

namespace MarkdownRenderer.Sample;
Expand Down Expand Up @@ -170,6 +171,7 @@ public MainWindow()
_renderer = new MarkdownRendererControlBuilder()
.UseGitHubFlavoredMarkdown()
.UseMarkdownExtra()
.UseTextMateSyntaxHighlighting()
.WithMarkdown(FullDemoSample)
.WithTheme(new MarkdownTheme())
.WithEmbedFactory(new SampleEmbedFactory())
Expand Down Expand Up @@ -394,9 +396,9 @@ 2. And back to normal.
private const string CodeSample = """
## Fenced Code Blocks

C# example:
C# with a filename, line numbers, and highlighted lines:

```csharp
```csharp filename="MarkdownRendererControl.cs" {3,8-10} startLine=120
public sealed class MarkdownRendererControl : UserControl
{
private volatile LayoutSnapshot? _snapshot;
Expand All @@ -417,9 +419,20 @@ private async Task RebuildAsync(CancellationToken ct)
}
```

Python example:
TypeScript with a title:

```ts title="Async preview model"
type RenderState = "idle" | "loading" | "ready";

export async function renderMarkdown(source: string): Promise<RenderState> {
const response = await fetch("/api/markdown", { method: "POST", body: source });
return response.ok ? "ready" : "idle";
}
```

Python example without line numbers:

```python
```python noLineNumbers
import asyncio

async def render_markdown(text: str) -> LayoutSnapshot:
Expand All @@ -428,18 +441,44 @@ async def render_markdown(text: str) -> LayoutSnapshot:
return await asyncio.to_thread(layout_builder.build, document)
```

PowerShell example:

```powershell filename="build.ps1"
dotnet test .\MarkdownRenderer\MarkdownRenderer.Tests\MarkdownRenderer.Tests.csproj -p:Platform=x64
dotnet build .\MarkdownRenderer\MarkdownRenderer.Sample\MarkdownRenderer.Sample.csproj -p:Platform=x64
```

JSON example:

```json
{
"renderer": "MarkdownRenderer",
"codeBlockVersion": 2,
"syntaxHighlighting": true
}
```

Diff example:

```diff
- plain shaded text box
+ native code surface
+ syntax highlighting
+ copy actions
```

Long line that wraps:

```js filename="long-line.js"
export const message = "This deliberately long line demonstrates that code blocks always wrap instead of requiring horizontal scrolling.";
```

Indented code block (4 spaces):

var x = 42;
Console.WriteLine($"The answer is {x}");

Inline `code` uses a background highlight.

## Language tags

The renderer stores the `FencedCodeBlock.Info` property (e.g. `"csharp"`)
on the source map entry — a future syntax-highlighting pass can use this
to apply per-token colors via `CanvasTextLayout.SetColor`.
""";

private const string AlertsSample = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<ItemGroup>
<ProjectReference Include="..\MarkdownRenderer\MarkdownRenderer.csproj" />
<ProjectReference Include="..\MarkdownRenderer.Gfm\MarkdownRenderer.Gfm.csproj" />
<ProjectReference Include="..\MarkdownRenderer.SyntaxHighlighting.TextMate\MarkdownRenderer.SyntaxHighlighting.TextMate.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0-windows10.0.26100.0</TargetFramework>
<TargetPlatformMinVersion>10.0.19041.0</TargetPlatformMinVersion>
<RootNamespace>MarkdownRenderer.SyntaxHighlighting.TextMate</RootNamespace>
<AssemblyName>MarkdownRenderer.SyntaxHighlighting.TextMate</AssemblyName>
<UseWinUI>true</UseWinUI>
<WinUISDKReferences>false</WinUISDKReferences>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<Platforms>x86;x64;ARM64</Platforms>
<Platform Condition="'$(Platform)' == '' or '$(Platform)' == 'AnyCPU' or '$(Platform)' == 'Any CPU'">x64</Platform>
<PlatformTarget Condition="'$(PlatformTarget)' == ''">$(Platform)</PlatformTarget>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageId>MarkdownRenderer.SyntaxHighlighting.TextMate</PackageId>
<Version>0.1.0</Version>
<Authors>nerocui</Authors>
<Description>TextMate grammar based syntax highlighting for MarkdownRenderer code blocks.</Description>
<PackageTags>markdown;winui;syntax-highlighting;textmate;code</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryUrl>https://github.com/JitHubApp/JitHubV2</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageProjectUrl>https://github.com/JitHubApp/JitHubV2/tree/main/docs/markdown-renderer</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageIcon>icon-192.png</PackageIcon>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\MarkdownRenderer\MarkdownRenderer.csproj" />
<PackageReference Include="TextMateSharp.Grammars" Version="2.0.3" />
<None Include="..\..\LICENSE" Pack="true" PackagePath="" Visible="false" />
<None Include="..\..\docs\markdown-renderer\README.md" Pack="true" PackagePath="README.md" Visible="false" />
<None Include="..\..\JitHub.Web\wwwroot\icon-192.png" Pack="true" PackagePath="icon-192.png" Visible="false" />
</ItemGroup>
</Project>
Loading
Loading