77using Meta . Tutorial . Framework . Hub . Pages ;
88using Meta . Tutorial . Framework . Hub . Pages . Markdown ;
99using Meta . Tutorial . Framework . Hub . Parsing ;
10+ using Meta . Tutorial . Framework . Hub . Utilities ;
1011using Meta . Tutorial . Framework . Windows ;
1112using UnityEngine ;
1213
@@ -30,6 +31,7 @@ public class PageConfig
3031 public string SectionTitleAsAnchorId => Regex . Replace ( SectionTitle . Replace ( " " , "-" ) , @"[^a-zA-Z0-9\-]" , string . Empty ) . ToLower ( ) ;
3132 }
3233
34+ [ MDFilePath ]
3335 [ SerializeField ] private string m_markdownPath = "./README.md" ;
3436 [ SerializeField , Tooltip ( "When all content is a level 1 because the header is level 0 we can bring them to level 0" ) ]
3537 private bool m_reduceTitleLevelBy1 = false ;
@@ -84,6 +86,25 @@ private bool AnyAsChildren
8486 }
8587 }
8688
89+ private bool AnyInContext
90+ {
91+ get
92+ {
93+ if ( m_pageConfigs == null || m_pageConfigs . Count == 0 )
94+ {
95+ return false ;
96+ }
97+ foreach ( var pageConfig in m_pageConfigs )
98+ {
99+ if ( pageConfig . ShowSectionInContext )
100+ {
101+ return true ;
102+ }
103+ }
104+ return false ;
105+ }
106+ }
107+
87108 public IReadOnlyList < PageConfig > PageConfigs => m_pageConfigs ;
88109
89110 private string ReadTextFromFile ( string path )
@@ -92,99 +113,123 @@ private string ReadTextFromFile(string path)
92113 public override PageReference [ ] CreatePageReferences ( bool forceCreate = false )
93114 {
94115 var rootPath = System . IO . Path . GetDirectoryName ( m_markdownPath ) + '/' ;
116+ var allPages = new List < PageReference > ( ) ;
95117 var pageAssetPath = GetRootPageAssetPath ( ) ;
96- var isInstance = CreateOrLoadInstance < MetaHubMarkdownPage > ( pageAssetPath , out var markdownPage , forceCreate ) ;
97- if ( isInstance )
98- {
99- markdownPage . OverrideMarkdownText ( ReadTextFromFile ( m_markdownPath ) , rootPath , m_reduceTitleLevelBy1 ) ;
100- markdownPage . OverrideContext ( this ) ;
101- }
102118
103- if ( markdownPage == null )
119+ if ( AnyInContext )
104120 {
105- GenerateParsedMarkdown ( ) ;
106- }
107- else
108- {
109- m_parsedMarkdown = markdownPage . ParsedMarkdown ;
110- }
111-
112- if ( IncludeAllInContext || ( markdownPage && ! isInstance && ! AnyAsChildren ) )
113- {
114- markdownPage = InstanceToAsset ( markdownPage , pageAssetPath ) ;
115- var soPage = new ScriptableObjectPage ( markdownPage , TutorialName , markdownPage . HierarchyName , markdownPage . Priority , ShowBanner ? Banner : null ) ;
116- return new [ ]
121+ var createdInstance = CreateOrLoadInstance < MetaHubMarkdownPage > ( pageAssetPath , out var markdownPage , forceCreate ) ;
122+ // if the instance is created we regenerate the page
123+ if ( createdInstance )
117124 {
118- new PageReference ( )
125+ if ( IncludeAllInContext )
119126 {
120- Page = soPage ,
121- Info = soPage
127+ markdownPage . OverrideMarkdownText ( ReadTextFromFile ( m_markdownPath ) , rootPath , m_reduceTitleLevelBy1 ) ;
122128 }
123- } ;
124- }
125- else
126- {
127- ParsedMD . Section aggregator = null ;
128- var level0Indices = m_parsedMarkdown . Level0SectionIndices ;
129- if ( isInstance )
130- {
131- var processingParsedMarkdown = markdownPage . ParsedMarkdown ;
132- for ( var i = 0 ; i < level0Indices . Length ; i ++ )
129+ else
133130 {
134- if ( m_pageConfigs [ i ] . ShowSectionInContext )
135- {
136- var content = processingParsedMarkdown . GetCollapsedSection0 ( i , m_pageConfigs [ i ] . HideTitle ) ;
137- if ( aggregator == null )
138- {
139- aggregator = content ;
140- }
141- else
142- {
143- aggregator . Append ( content ) ;
144- }
145- }
131+ GenerateMainPageWithSubsections ( ref markdownPage , rootPath , pageAssetPath ) ;
146132 }
147- }
148133
149- var ret = new List < PageReference > ( ) ;
150- if ( aggregator != null )
151- {
152- markdownPage . OverrideMarkdownText ( aggregator . ToString ( ) , rootPath , m_reduceTitleLevelBy1 , false ) ; // the aggregator was already stripped of xml
153- markdownPage = InstanceToAsset ( markdownPage , pageAssetPath ) ;
154-
155- var soPage = new ScriptableObjectPage ( markdownPage , TutorialName , markdownPage . HierarchyName , markdownPage . Priority , ShowBanner ? Banner : null ) ;
156- ret . Add ( new PageReference ( )
134+ if ( AnyAsChildren )
135+ {
136+ markdownPage . OverrideContext ( this , prefixHierarchyName : $ "{ Title } /") ;
137+ }
138+ else
157139 {
158- Page = soPage ,
159- Info = soPage
160- } ) ;
140+ markdownPage . OverrideContext ( this ) ;
141+ }
142+
143+ markdownPage = InstanceToAsset ( markdownPage , pageAssetPath ) ;
144+ }
145+ if ( markdownPage != null )
146+ {
147+ var soPage = new ScriptableObjectPage (
148+ markdownPage , TutorialName , markdownPage . HierarchyName , markdownPage . Priority ,
149+ ShowBanner ? Banner : null ) ;
150+ allPages . Add ( new PageReference ( ) { Page = soPage , Info = soPage } ) ;
161151 }
152+ }
162153
163- // now create pages and references for the children
164- for ( var i = 0 ; i < level0Indices . Length ; i ++ )
154+ if ( AnyAsChildren )
155+ {
156+ for ( var i = 0 ; i < m_pageConfigs . Count ; i ++ )
165157 {
166- if ( m_pageConfigs [ i ] . ShowSectionAsChild )
158+ var pageConfig = m_pageConfigs [ i ] ;
159+ if ( pageConfig . ShowSectionAsChild )
167160 {
168- pageAssetPath = GetChildPageAssetPath ( m_pageConfigs [ i ] . SectionTitle ) ;
169- isInstance = CreateOrLoadInstance < MetaHubMarkdownPage > ( pageAssetPath , out var childPage , forceCreate ) ;
170- if ( isInstance )
161+ pageAssetPath = GetChildPageAssetPath ( pageConfig . SectionTitle ) ;
162+ var createdInstance = CreateOrLoadInstance < MetaHubMarkdownPage > (
163+ pageAssetPath , out var childPage , forceCreate ) ;
164+ if ( createdInstance )
171165 {
172- childPage . OverrideMarkdownText ( m_parsedMarkdown . GetCollapsedSection0 ( i , m_pageConfigs [ i ] . HideTitle ) . ToString ( ) , rootPath , m_reduceTitleLevelBy1 , false ) ; // xml is already stripped
173- childPage . OverrideContext ( this , prefixHierarchyName : $ "{ markdownPage . HierarchyName } /", displayName : m_pageConfigs [ i ] . SectionTitle ) ;
166+ childPage . OverrideMarkdownText (
167+ GetParsedMarkDown ( ) . GetCollapsedSection0 ( i , m_pageConfigs [ i ] . HideTitle ) . ToString ( ) ,
168+ rootPath , m_reduceTitleLevelBy1 , false ) ; // xml is already stripped
169+ childPage . OverrideContext (
170+ this , prefixHierarchyName : $ "{ Title } /",
171+ displayName : m_pageConfigs [ i ] . SectionTitle , overridePriority : Priority + i ) ;
174172 childPage = InstanceToAsset ( childPage , pageAssetPath ) ;
175173 }
176174
177- var childSoPage = new ScriptableObjectPage ( childPage , TutorialName , childPage . HierarchyName , childPage . Priority ) ;
178- ret . Add ( new PageReference ( )
175+ if ( childPage != null )
179176 {
180- Page = childSoPage ,
181- Info = childSoPage
182- } ) ;
177+ var childSoPage = new ScriptableObjectPage (
178+ childPage , TutorialName , childPage . HierarchyName , childPage . Priority ) ;
179+ allPages . Add ( new PageReference ( ) { Page = childSoPage , Info = childSoPage } ) ;
180+ }
183181 }
184182 }
183+ }
185184
186- return ret . ToArray ( ) ;
185+ return allPages . ToArray ( ) ;
186+ }
187+
188+ private ParsedMD GetParsedMarkDown ( )
189+ {
190+ if ( m_parsedMarkdown == null )
191+ {
192+ GenerateParsedMarkdown ( ) ;
187193 }
194+
195+ return m_parsedMarkdown ;
196+ }
197+
198+ private void GenerateMainPageWithSubsections ( ref MetaHubMarkdownPage markdownPage , string rootPath , string pageAssetPath )
199+ {
200+ if ( markdownPage == null )
201+ {
202+ return ;
203+ }
204+ ParsedMD . Section aggregator = null ;
205+ for ( var i = 0 ; i < m_pageConfigs . Count ; i ++ )
206+ {
207+ if ( m_pageConfigs [ i ] . ShowSectionInContext )
208+ {
209+ var content = GetParsedMarkDown ( ) . GetCollapsedSection0 ( i , m_pageConfigs [ i ] . HideTitle ) ;
210+ if ( aggregator == null )
211+ {
212+ aggregator = content ;
213+ }
214+ else
215+ {
216+ aggregator . Append ( content ) ;
217+ }
218+ }
219+ }
220+
221+ if ( aggregator != null )
222+ {
223+ // the aggregator was already stripped of xml
224+ markdownPage . OverrideMarkdownText ( aggregator . ToString ( ) , rootPath , m_reduceTitleLevelBy1 , false ) ;
225+ }
226+ }
227+
228+ public void ReloadPage ( )
229+ {
230+ // clear last loaded path so that it reloads the file
231+ m_lastLoadedPath = null ;
232+ OnValidate ( ) ;
188233 }
189234
190235 private void OnValidate ( )
0 commit comments