@@ -21,6 +21,12 @@ import Translate from '@docusaurus/Translate';
2121import AutoFixHighIcon from '@mui/icons-material/AutoFixHigh' ;
2222import Admonition from '@theme/Admonition' ;
2323import { getReleaseStatus , getVersion } from '@site/src/utils/SupportedReleases' ;
24+ import {
25+ useActivePlugin ,
26+ useDocVersionSuggestions ,
27+ } from '@docusaurus/plugin-content-docs/client' ;
28+
29+ const getVersionMainDoc = ( version ) => version . docs . find ( ( doc ) => doc . id === version . mainDocId ) ;
2430
2531function UnsupportedTitle ( ) {
2632 return (
@@ -93,6 +99,34 @@ function GeneralSupportExpiredWarning({ versionData }) {
9399 ) ;
94100}
95101
102+ function NewerVersionAvailableWarning ( { versionData } ) {
103+ const { pluginId } = useActivePlugin ( { failfast : true } ) ;
104+ const { latestDocSuggestion, latestVersionSuggestion } = useDocVersionSuggestions ( pluginId ) ;
105+ const latestVersionSuggestedDoc = latestDocSuggestion ?? getVersionMainDoc ( latestVersionSuggestion ) ;
106+
107+ return (
108+ < Admonition
109+ type = "caution"
110+ icon = {
111+ < AutoFixHighIcon fontSize = "inherit" />
112+ }
113+ title = "Newer Version Available"
114+ >
115+ < strong >
116+ This documentation is for Moodle
117+ { ' ' }
118+ { versionData . name }
119+ .
120+ </ strong >
121+ < br />
122+ You may also want to read the documentation for the
123+ { ' ' }
124+ < Link to = { latestVersionSuggestedDoc . path } > upcoming version of Moodle</ Link >
125+ .
126+ </ Admonition >
127+ ) ;
128+ }
129+
96130function FutureReleaseWarning ( { versionData } ) {
97131 return (
98132 < Admonition
@@ -162,23 +196,45 @@ function ExperimentalWarning() {
162196 ) ;
163197}
164198
165- function VersionedSupportWarning ( { versionData, moodleVersion } ) {
199+ function VersionedSupportWarning ( { versionData, moodleVersion, metadata } ) {
166200 const releaseStatus = getReleaseStatus ( versionData , moodleVersion ) ;
201+ const { sidebar } = metadata ;
202+
203+ // Versions fit the following categories:
204+ // - Experimental (show danger warning)
205+ // - Current version (released or `main` branch)
206+ // - main branch (no warning)
207+ // - stable version information (no warning)
208+ // - stable versioned docs (indicate main branch information is available)
209+ // - Security Only (show warning)
210+ // - Future (show warning)
211+ // - Future Stable (show warning)
212+ // - Unsupported (show danger warning)
167213
168214 if ( versionData . isExperimental ) {
169- // Experimental version.
215+ // Experimental versions trump all.
216+ // Always show this notice.
170217 return (
171218 < ExperimentalWarning versionData = { versionData } />
172219 ) ;
173220 }
174221
222+ if ( sidebar === 'docs' && moodleVersion . version !== 'current' && metadata . slug !== '/' ) {
223+ // This is a versioned doc page but it's not the 'current' version (main branch).
224+ // Show a warning banner that that there are docs for the 'main' branch.
225+ return (
226+ < NewerVersionAvailableWarning versionData = { versionData } />
227+ ) ;
228+ }
229+
175230 if ( releaseStatus === 'current' ) {
176- // Still in general support.
231+ // This documentation is for docs still in general support.
177232 return null ;
178233 }
179234
180235 if ( releaseStatus === 'future' || releaseStatus === 'future-stable' ) {
181- // Not yet supported.
236+ // This documentatino relates to a versino of Moodle which has yet to be released.
237+ // This is probably a version about to be released.
182238 // TODO Add a warning banner.
183239 return (
184240 < FutureReleaseWarning versionData = { versionData } />
@@ -197,8 +253,21 @@ function VersionedSupportWarning({ versionData, moodleVersion }) {
197253 ) ;
198254}
199255
200- export default function VersionInfo ( { frontMatter } ) {
201- const { moodleVersion = null } = frontMatter ;
256+ const guessMoodleVersion = ( frontMatter , metadata ) => {
257+ if ( frontMatter . moodleVersion ) {
258+ return frontMatter . moodleVersion ;
259+ }
260+
261+ if ( metadata . version ) {
262+ if ( metadata . version . match ( / ^ (?< seriesMajor > \d + \. \d + ) (?< minor > \. \d + ) ? / ) ) {
263+ return metadata . version ;
264+ }
265+ }
266+ return undefined ;
267+ } ;
268+
269+ export default function VersionInfo ( { frontMatter, metadata = { } } ) {
270+ const moodleVersion = guessMoodleVersion ( frontMatter , metadata ) ;
202271
203272 if ( ! moodleVersion ) {
204273 // No version number found.
@@ -208,13 +277,16 @@ export default function VersionInfo({ frontMatter }) {
208277 const versionData = getVersion ( moodleVersion ) ;
209278 if ( ! versionData ) {
210279 // No valid version data found.
280+ // We don't know how to handle this version.
211281 return null ;
212282 }
213283
214284 return (
215285 < VersionedSupportWarning
216286 versionData = { versionData }
217287 moodleVersion = { moodleVersion }
288+ metadata = { metadata }
289+ sidebar = { metadata ?. sidebar }
218290 />
219291 ) ;
220292}
0 commit comments