1212using Markdig . Syntax . Inlines ;
1313using Microsoft . PowerShell . PlatyPS . MarkdownWriter ;
1414using Microsoft . PowerShell . PlatyPS . Model ;
15- using Markdig . Renderers ;
15+ using System . Collections ;
1616
1717namespace Microsoft . PowerShell . PlatyPS
1818{
@@ -198,7 +198,7 @@ internal static CommandHelp GetCommandHelpFromMarkdown(ParsedMarkdownContent mar
198198
199199 CommandHelp commandHelp ;
200200
201- OrderedDictionary ? metadata = GetMetadata ( markdownContent . Ast ) ;
201+ OrderedDictionary ? metadata = GetMetadata ( markdownContent ) ;
202202 if ( metadata is null )
203203 {
204204 throw new InvalidDataException ( $ "No metadata found in { markdownContent . FilePath } ") ;
@@ -327,8 +327,9 @@ internal static CommandHelp GetCommandHelpFromMarkdown(ParsedMarkdownContent mar
327327
328328 }
329329
330- internal static OrderedDictionary ? GetMetadata ( MarkdownDocument ast )
330+ internal static OrderedDictionary ? GetMetadata ( ParsedMarkdownContent md )
331331 {
332+ var ast = md . Ast ;
332333 // The metadata must be the first block in the markdown
333334 if ( ast . Count < 2 )
334335 {
@@ -353,33 +354,36 @@ internal static CommandHelp GetCommandHelpFromMarkdown(ParsedMarkdownContent mar
353354 }
354355 else if ( ast [ 2 ] is Markdig . Syntax . ListBlock && ast [ 1 ] is ParagraphBlock paragraphWithList )
355356 {
356- List < string > listItems = new ( ) ;
357-
358- if ( ast [ 2 ] is Markdig . Syntax . ListBlock listBlock )
357+ if ( paragraphWithList . Inline ? . FirstChild is LiteralInline metadataAsParagraph )
359358 {
360- foreach ( var listItem in listBlock )
359+ var metadataDictionary = ConvertTextToOrderedDictionary ( metadataAsParagraph . Content . Text ) ;
360+
361+ StringBuilder ? sb = null ;
362+
363+ try
361364 {
362- if ( listItem is ListItemBlock listItemBlock )
365+ sb = Constants . StringBuilderPool . Get ( ) ;
366+ int startIndex = ast [ 2 ] . Line - 1 ; // -1 because we are 0 based
367+ int endIndex = ast [ 3 ] . Line ;
368+ for ( int i = startIndex ; i < endIndex ; i ++ )
369+ {
370+ sb . AppendLine ( md . MarkdownLines [ i ] . TrimEnd ( ) ) ;
371+ }
372+ string blockContent = sb . ToString ( ) . Replace ( "\r " , "" ) . Trim ( ) ;
373+
374+ foreach ( DictionaryEntry kv in ConvertTextToOrderedDictionary ( blockContent ) )
363375 {
364- if ( listItemBlock is ContainerBlock containerBlock )
365- {
366- if ( containerBlock . LastChild is ParagraphBlock paragraphListItem )
367- {
368- if ( paragraphListItem . Inline ? . FirstChild is LiteralInline listItemText )
369- {
370- listItems . Add ( listItemText . Content . ToString ( ) . Trim ( ) ) ;
371- }
372- }
373- }
376+ metadataDictionary [ kv . Key ] = kv . Value ;
377+ }
378+ }
379+ finally
380+ {
381+ if ( sb is not null )
382+ {
383+ Constants . StringBuilderPool . Return ( sb ) ;
374384 }
375385 }
376- }
377386
378- if ( paragraphWithList . Inline ? . FirstChild is LiteralInline metadataAsParagraph )
379- {
380- var metadataDictionary = ConvertTextToOrderedDictionary ( metadataAsParagraph . Content . Text ) ;
381- // update the metadata dictionary with the list items of aliases
382- metadataDictionary [ "aliases" ] = listItems ;
383387 return metadataDictionary ;
384388 }
385389 }
0 commit comments