Skip to content

Commit 5c1f7ea

Browse files
committed
tweak: add title frontmatter for mdx, prefer included examples first
1 parent 3a6d1a5 commit 5c1f7ea

6 files changed

Lines changed: 89 additions & 7 deletions

File tree

src/NativeCodeGen.Cli/NativeCodeGen.Cli.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<!-- Trimming to reduce binary size -->
2929
<PublishTrimmed>true</PublishTrimmed>
3030
<TrimMode>link</TrimMode>
31+
3132
</PropertyGroup>
3233

3334
<!-- Preserve types for reflection-based libraries -->

src/NativeCodeGen.Core/Export/DatabaseConverter.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,34 @@ public static ExportDatabase Convert(NativeDatabase db, ExportOptions options)
4747
}
4848
}
4949

50-
// Convert shared examples
50+
// Convert shared examples - referenced examples first
51+
var referencedExamples = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
52+
foreach (var ns in db.Namespaces)
53+
{
54+
foreach (var native in ns.Natives)
55+
{
56+
foreach (var exampleName in native.RelatedExamples)
57+
{
58+
referencedExamples.Add(exampleName);
59+
}
60+
}
61+
}
62+
63+
// Add referenced examples first, then the rest
64+
foreach (var name in referencedExamples)
65+
{
66+
if (db.SharedExamples.TryGetValue(name, out var example))
67+
{
68+
export.SharedExamples.Add(ConvertSharedExample(example));
69+
}
70+
}
71+
5172
foreach (var (name, example) in db.SharedExamples)
5273
{
53-
export.SharedExamples.Add(ConvertSharedExample(example));
74+
if (!referencedExamples.Contains(name))
75+
{
76+
export.SharedExamples.Add(ConvertSharedExample(example));
77+
}
5478
}
5579

5680
// Add type definitions
@@ -141,6 +165,7 @@ private static ExportSharedExample ConvertSharedExample(Models.SharedExample exa
141165
return new ExportSharedExample
142166
{
143167
Name = example.Name,
168+
Title = example.Title,
144169
Content = example.Content,
145170
Language = example.Language
146171
};

src/NativeCodeGen.Core/Export/ExportModels.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,11 @@ public partial class ExportSharedExample
191191
public string Name { get; set; } = string.Empty;
192192

193193
[ProtoMember(2)]
194-
public string Content { get; set; } = string.Empty;
194+
public string? Title { get; set; }
195195

196196
[ProtoMember(3)]
197+
public string Content { get; set; } = string.Empty;
198+
199+
[ProtoMember(4)]
197200
public string? Language { get; set; }
198201
}

src/NativeCodeGen.Core/Export/natives.proto

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ message NativeReference {
9191

9292
message SharedExample {
9393
string name = 1;
94-
string content = 2;
95-
optional string language = 3;
94+
optional string title = 2;
95+
string content = 3;
96+
optional string language = 4;
9697
}

src/NativeCodeGen.Core/Models/SharedExample.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace NativeCodeGen.Core.Models;
33
public class SharedExample
44
{
55
public string Name { get; set; } = string.Empty;
6+
public string? Title { get; set; }
67
public string Content { get; set; } = string.Empty;
78
public string? Language { get; set; }
89
public string? SourceFile { get; set; }

src/NativeCodeGen.Core/Registry/SharedExampleRegistry.cs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
using NativeCodeGen.Core.Models;
2+
using YamlDotNet.Serialization;
3+
using YamlDotNet.Serialization.NamingConventions;
24

35
namespace NativeCodeGen.Core.Registry;
46

7+
public class SharedExampleFrontmatter
8+
{
9+
public string? Title { get; set; }
10+
}
11+
512
public class SharedExampleRegistry
613
{
714
private readonly Dictionary<string, SharedExample> _examples = new(StringComparer.OrdinalIgnoreCase);
15+
private readonly IDeserializer _deserializer;
816

917
public int Count => _examples.Count;
1018

19+
public SharedExampleRegistry()
20+
{
21+
_deserializer = new DeserializerBuilder()
22+
.WithNamingConvention(CamelCaseNamingConvention.Instance)
23+
.IgnoreUnmatchedProperties()
24+
.Build();
25+
}
26+
1127
public void LoadExamples(string directory)
1228
{
1329
if (!Directory.Exists(directory))
@@ -18,19 +34,54 @@ public void LoadExamples(string directory)
1834
var name = Path.GetFileNameWithoutExtension(file);
1935
var content = File.ReadAllText(file);
2036

21-
// Extract language and code content from markdown code block
22-
var (language, code) = ParseCodeBlock(content);
37+
// Extract frontmatter and code content
38+
var (frontmatter, body) = ParseFrontmatter(content);
39+
var (language, code) = ParseCodeBlock(body);
2340

2441
_examples[name] = new SharedExample
2542
{
2643
Name = name,
44+
Title = frontmatter?.Title,
2745
Content = code,
2846
Language = language,
2947
SourceFile = file
3048
};
3149
}
3250
}
3351

52+
private (SharedExampleFrontmatter? frontmatter, string body) ParseFrontmatter(string content)
53+
{
54+
var lines = content.Split('\n');
55+
if (lines.Length == 0 || lines[0].Trim() != "---")
56+
return (null, content);
57+
58+
int endIndex = -1;
59+
for (int i = 1; i < lines.Length; i++)
60+
{
61+
if (lines[i].Trim() == "---")
62+
{
63+
endIndex = i;
64+
break;
65+
}
66+
}
67+
68+
if (endIndex == -1)
69+
return (null, content);
70+
71+
var yamlContent = string.Join('\n', lines.Skip(1).Take(endIndex - 1));
72+
var body = string.Join("\n", lines.Skip(endIndex + 1));
73+
74+
try
75+
{
76+
var frontmatter = _deserializer.Deserialize<SharedExampleFrontmatter>(yamlContent);
77+
return (frontmatter, body);
78+
}
79+
catch
80+
{
81+
return (null, body);
82+
}
83+
}
84+
3485
private static (string? language, string content) ParseCodeBlock(string content)
3586
{
3687
var lines = content.Split('\n');

0 commit comments

Comments
 (0)