@ -17,6 +17,7 @@ namespace Volo.Docs.HtmlConverting
private const string JsonCloser = "````" ;
private const string DocsParam = "//[doc-params]" ;
private const string DocsTemplates = "//[doc-template]" ;
private const string DocsNav = "//[doc-nav]" ;
public ILogger < ScribanDocumentSectionRenderer > Logger { get ; set ; }
@ -41,71 +42,87 @@ namespace Volo.Docs.HtmlConverting
var result = await scribanTemplate . RenderAsync ( parameters ) ;
return RemoveOptionsJson ( result ) ;
return RemoveOptionsJson ( result , DocsParam , DocsNav ) ;
}
public async Task < Dictionary < string , List < string > > > GetAvailableParametersAsync ( string document )
public Task < Dictionary < string , List < string > > > GetAvailableParametersAsync ( string document )
{
return GetSectionAsync < Dictionary < string , List < string > > > ( document , DocsParam ) ;
}
public Task < DocumentNavigationsDto > GetDocumentNavigationsAsync ( string documentContent )
{
return GetSectionAsync < DocumentNavigationsDto > ( documentContent , DocsNav ) ;
}
protected virtual async Task < T > GetSectionAsync < T > ( string document , string sectionName ) where T : new ( )
{
try
{
if ( ! document . Contains ( JsonOpener ) | | ! document . Contains ( DocsParam ) )
if ( ! document . Contains ( JsonOpener ) | | ! document . Contains ( sectionName ) )
{
return new Dictionary < string , List < string > > ( ) ;
return new T ( ) ;
}
var ( jsonBeginningIndex , jsonEndingIndex , insideJsonSection ) = GetJsonBeginEndIndexesAndPureJson ( document ) ;
var ( jsonBeginningIndex , jsonEndingIndex , insideJsonSection ) = GetJsonBeginEndIndexesAndPureJson ( document , sectionName ) ;
if ( jsonBeginningIndex < 0 | | jsonEndingIndex < = 0 | | string . IsNullOrWhiteSpace ( insideJsonSection ) )
{
return new Dictionary < string , List < string > > ( ) ;
return new T ( ) ;
}
var pureJson = insideJsonSection . Replace ( DocsParam , "" ) . Trim ( ) ;
var pureJson = insideJsonSection . Replace ( sectionName , "" ) . Trim ( ) ;
if ( ! DocsJsonSerializerHelper . TryDeserialize < Dictionary < string , List < string > > > ( pureJson , out var availableParameters ) )
if ( ! DocsJsonSerializerHelper . TryDeserialize < T > ( pureJson , out var section ) )
{
throw new UserFriendlyException ( "ERROR-20200327: Cannot validate JSON content for `AvailableParameters `!" ) ;
throw new UserFriendlyException ( $ "ERROR-20200327: Cannot validate JSON content for `{sectionName} `!") ;
}
return await Task . FromResult ( availableParameters ) ;
return await Task . FromResult ( section ) ;
}
catch ( Exception )
{
Logger . LogWarning ( "Unable to parse parameters of document." ) ;
return new Dictionary < string , List < string > > ( ) ;
return new T ( ) ;
}
}
private static string RemoveOptionsJson ( string document )
private static string RemoveOptionsJson ( string document , params string [ ] sectionNames )
{
var orgDocument = document ;
try
foreach ( var sectionName in sectionNames )
{
if ( ! document . Contains ( JsonOpener ) | | ! document . Contains ( DocsParam ) )
var orgDocument = document ;
try
{
return orgDocument ;
}
if ( ! document . Contains ( JsonOpener ) | | ! document . Contains ( sectionName ) )
{
continue ;
}
var ( jsonBeginningIndex , jsonEndingIndex , insideJsonSection ) = GetJsonBeginEndIndexesAndPureJson ( document ) ;
var ( jsonBeginningIndex , jsonEndingIndex , insideJsonSection ) = GetJsonBeginEndIndexesAndPureJson ( document , sectionName ) ;
if ( jsonBeginningIndex < 0 | | jsonEndingIndex < = 0 | | string . IsNullOrWhiteSpace ( insideJsonSection ) )
if ( jsonBeginningIndex < 0 | | jsonEndingIndex < = 0 | | string . IsNullOrWhiteSpace ( insideJsonSection ) )
{
continue ;
}
document = document . Remove (
jsonBeginningIndex - JsonOpener . Length ,
( jsonEndingIndex + JsonCloser . Length ) - ( jsonBeginningIndex - JsonOpener . Length )
) ;
}
catch ( Exception )
{
return orgDocument ;
document = orgDocument ;
}
return document . Remove (
jsonBeginningIndex - JsonOpener . Length ,
( jsonEndingIndex + JsonCloser . Length ) - ( jsonBeginningIndex - JsonOpener . Length )
) ;
}
catch ( Exception )
{
return orgDocument ;
}
return document ;
}
private static ( int , int , string ) GetJsonBeginEndIndexesAndPureJson ( string document )
private static ( int , int , string ) GetJsonBeginEndIndexesAndPureJson ( string document , string sectionName )
{
var searchedIndex = 0 ;
@ -121,7 +138,7 @@ namespace Volo.Docs.HtmlConverting
var jsonEndingIndex = document . Substring ( jsonBeginningIndex ) . IndexOf ( JsonCloser , StringComparison . Ordinal ) + jsonBeginningIndex ;
var insideJsonSection = document [ jsonBeginningIndex . . jsonEndingIndex ] ;
if ( insideJsonSection . IndexOf ( DocsParam , StringComparison . Ordinal ) < 0 )
if ( insideJsonSection . IndexOf ( sectionName , StringComparison . Ordinal ) < 0 )
{
searchedIndex = jsonEndingIndex + JsonCloser . Length ;
continue ;