@@ -29,6 +29,12 @@ public partial class ToolkitSampleMetadataGenerator
2929 private const string FrontMatterRegexSubcategoryExpression = @"^subcategory:\s*(?<subcategory>.*)$" ;
3030 private static readonly Regex FrontMatterRegexSubcategory = new Regex ( FrontMatterRegexSubcategoryExpression , RegexOptions . Compiled | RegexOptions . IgnoreCase | RegexOptions . Multiline ) ;
3131
32+ private const string FrontMatterRegexDiscussionIdExpression = @"^discussion-id:\s*(?<discussionid>.*)$" ;
33+ private static readonly Regex FrontMatterRegexDiscussionId = new Regex ( FrontMatterRegexDiscussionIdExpression , RegexOptions . Compiled | RegexOptions . IgnoreCase | RegexOptions . Multiline ) ;
34+
35+ private const string FrontMatterRegexIssueIdExpression = @"^issue-id:\s*(?<issueid>.*)$" ;
36+ private static readonly Regex FrontMatterRegexIssueId = new Regex ( FrontMatterRegexIssueIdExpression , RegexOptions . Compiled | RegexOptions . IgnoreCase | RegexOptions . Multiline ) ;
37+
3238 private const string MarkdownRegexSampleTagExpression = @"^>\s*\[!SAMPLE\s*(?<sampleid>.*)\s*\]\s*$" ;
3339 private static readonly Regex MarkdownRegexSampleTag = new Regex ( MarkdownRegexSampleTagExpression , RegexOptions . Compiled | RegexOptions . IgnoreCase | RegexOptions . Multiline ) ;
3440
@@ -105,9 +111,13 @@ private ImmutableArray<ToolkitFrontMatter> GatherDocumentFrontMatter(SourceProdu
105111 var category = ParseYamlField ( ref ctx , file . Path , ref frontmatter , FrontMatterRegexCategory , "category" ) ;
106112 var subcategory = ParseYamlField ( ref ctx , file . Path , ref frontmatter , FrontMatterRegexSubcategory , "subcategory" ) ;
107113
114+ // TODO: Should these just be optional?
115+ var discussion = ParseYamlField ( ref ctx , file . Path , ref frontmatter , FrontMatterRegexDiscussionId , "discussionid" ) ? . Trim ( ) ;
116+ var issue = ParseYamlField ( ref ctx , file . Path , ref frontmatter , FrontMatterRegexIssueId , "issueid" ) ? . Trim ( ) ;
117+
108118 // Check we have all the fields we expect to continue (errors will have been spit out otherwise already from the ParseYamlField method)
109119 if ( title == null || description == null || keywords == null ||
110- category == null || subcategory == null )
120+ category == null || subcategory == null || discussion == null || issue == null )
111121 {
112122 return null ;
113123 }
@@ -159,6 +169,28 @@ private ImmutableArray<ToolkitFrontMatter> GatherDocumentFrontMatter(SourceProdu
159169 file . Path ) ) ;
160170 }
161171
172+ if ( ! int . TryParse ( discussion , out var discussionId ) || discussionId < 0 )
173+ {
174+ ctx . ReportDiagnostic (
175+ Diagnostic . Create (
176+ DiagnosticDescriptors . MarkdownYAMLFrontMatterException ,
177+ Location . Create ( file . Path , TextSpan . FromBounds ( 0 , 1 ) , new LinePositionSpan ( LinePosition . Zero , LinePosition . Zero ) ) ,
178+ file . Path ,
179+ "Can't parse discussion-id field, must be a positive integer or zero." ) ) ;
180+ return null ;
181+ }
182+
183+ if ( ! int . TryParse ( issue , out var issueId ) || issueId < 0 )
184+ {
185+ ctx . ReportDiagnostic (
186+ Diagnostic . Create (
187+ DiagnosticDescriptors . MarkdownYAMLFrontMatterException ,
188+ Location . Create ( file . Path , TextSpan . FromBounds ( 0 , 1 ) , new LinePositionSpan ( LinePosition . Zero , LinePosition . Zero ) ) ,
189+ file . Path ,
190+ "Can't parse issue-id field, must be a positive integer or zero." ) ) ;
191+ return null ;
192+ }
193+
162194 // Finally, construct the complete object.
163195 return new ToolkitFrontMatter ( )
164196 {
@@ -168,13 +200,15 @@ private ImmutableArray<ToolkitFrontMatter> GatherDocumentFrontMatter(SourceProdu
168200 Category = categoryValue ,
169201 Subcategory = subcategoryValue ,
170202 FilePath = filepath ,
171- SampleIdReferences = sampleids . ToArray ( )
203+ SampleIdReferences = sampleids . ToArray ( ) ,
204+ DiscussionId = discussionId ,
205+ IssueId = issueId ,
172206 } ;
173207 }
174208 } ) . OfType < ToolkitFrontMatter > ( ) . ToImmutableArray ( ) ;
175209 }
176210
177- private string ? ParseYamlField ( ref SourceProductionContext ctx , string filepath , ref string content , Regex pattern , string fieldname )
211+ private string ? ParseYamlField ( ref SourceProductionContext ctx , string filepath , ref string content , Regex pattern , string captureGroupName )
178212 {
179213 var match = pattern . Match ( content ) ;
180214
@@ -185,11 +219,11 @@ private ImmutableArray<ToolkitFrontMatter> GatherDocumentFrontMatter(SourceProdu
185219 DiagnosticDescriptors . MarkdownYAMLFrontMatterMissingField ,
186220 Location . Create ( filepath , TextSpan . FromBounds ( 0 , 1 ) , new LinePositionSpan ( LinePosition . Zero , LinePosition . Zero ) ) ,
187221 filepath ,
188- fieldname ) ) ;
222+ captureGroupName ) ) ;
189223 return null ;
190224 }
191225
192- return match . Groups [ fieldname ] . Value . Trim ( ) ;
226+ return match . Groups [ captureGroupName ] . Value . Trim ( ) ;
193227 }
194228
195229 private void CreateDocumentRegistry ( SourceProductionContext ctx , ImmutableArray < ToolkitFrontMatter > matter )
@@ -223,6 +257,6 @@ private static string FrontMatterToRegistryCall(ToolkitFrontMatter metadata)
223257 var categoryParam = $ "{ nameof ( ToolkitSampleCategory ) } .{ metadata . Category } ";
224258 var subcategoryParam = $ "{ nameof ( ToolkitSampleSubcategory ) } .{ metadata . Subcategory } ";
225259
226- return @$ "yield return new { typeof ( ToolkitFrontMatter ) . FullName } () {{ Title = ""{ metadata . Title } "", Author = ""{ metadata . Author } "", Description = ""{ metadata . Description } "", Keywords = ""{ metadata . Keywords } "", Category = { categoryParam } , Subcategory = { subcategoryParam } , FilePath = @""{ metadata . FilePath } "", SampleIdReferences = new string[] {{ ""{ string . Join ( "\" ,\" " , metadata . SampleIdReferences ) } "" }} }};"; // TODO: Add list of sample ids in document
260+ return @$ "yield return new { typeof ( ToolkitFrontMatter ) . FullName } () {{ Title = ""{ metadata . Title } "", Author = ""{ metadata . Author } "", Description = ""{ metadata . Description } "", Keywords = ""{ metadata . Keywords } "", Category = { categoryParam } , Subcategory = { subcategoryParam } , DiscussionId = { metadata . DiscussionId } , IssueId = { metadata . IssueId } , FilePath = @""{ metadata . FilePath } "", SampleIdReferences = new string[] {{ ""{ string . Join ( "\" ,\" " , metadata . SampleIdReferences ) } "" }} }};"; // TODO: Add list of sample ids in document
227261 }
228262}
0 commit comments