Skip to content

Commit

Permalink
Convert relative site URLs in release notes (#7529)
Browse files Browse the repository at this point in the history
  • Loading branch information
parlough committed May 21, 2024
1 parent 1aa6db6 commit a6f9d88
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ bool debugTestReleaseNotes = false;
const String? _debugReleaseNotesUrl = null;

const releaseNotesKey = Key('release_notes');
const _unsupportedPathSyntax = '{{site.url}}';
final _baseUrlRelativeMarkdownLinkPattern = RegExp(
r'(\[.*?]\()(/.*\s*)',
multiLine: true,
);
const _releaseNotesPath = '/f/devtools-releases.json';
final _flutterDocsSite = Uri.https('docs.flutter.dev');

Expand Down Expand Up @@ -97,10 +100,11 @@ class ReleaseNotesController extends SidePanelController {
final debugUri = Uri.parse(debugUrl);
final releaseNotesMarkdown = await http.read(debugUri);

// Update image links to use debug/testing URL.
markdown.value = releaseNotesMarkdown.replaceAll(
_unsupportedPathSyntax,
debugUri.replace(path: '').toString(),
// Update the base-url-relative links in the file to
// absolute links using the debug/testing URL.
markdown.value = _convertBaseUrlRelativeLinks(
releaseNotesMarkdown,
debugUri.replace(path: ''),
);

toggleVisibility(true);
Expand Down Expand Up @@ -145,7 +149,7 @@ class ReleaseNotesController extends SidePanelController {
// release notes (e.g. 2.11.4 -> 2.11.3 -> 2.11.2 -> ...).
while (patchToCheck >= minimumPatch) {
final releaseToCheck = '$majorMinor.$patchToCheck';
if (releases[releaseToCheck] case final String releaseNotePath) {
if (releases[releaseToCheck] case final releaseNotePath?) {
final String releaseNotesMarkdown;
try {
releaseNotesMarkdown = await http.read(
Expand All @@ -161,11 +165,10 @@ class ReleaseNotesController extends SidePanelController {
continue;
}

// Replace the {{site.url}} template syntax that the
// Flutter docs website uses to specify site URLs.
markdown.value = releaseNotesMarkdown.replaceAll(
_unsupportedPathSyntax,
_flutterDocsSite.toString(),
// Update the base-url-relative links in the file to absolute links.
markdown.value = _convertBaseUrlRelativeLinks(
releaseNotesMarkdown,
_flutterDocsSite,
);

toggleVisibility(true);
Expand All @@ -188,13 +191,25 @@ class ReleaseNotesController extends SidePanelController {
return;
}

/// Convert all site-base-url relative links in [markdownContent]
/// to absolute links from the specified [baseUrl].
///
/// For example, if `baseUrl` is `https://docs.flutter.dev`,
/// the path `/tools/devtools` would be converted
/// to `https://docs.flutter.dev/tools/devtools`.
String _convertBaseUrlRelativeLinks(String markdownContent, Uri baseUrl) =>
markdownContent.replaceAllMapped(
_baseUrlRelativeMarkdownLinkPattern,
(m) => '${m[1]}${baseUrl.toString()}${m[2]}',
);

/// Retrieve and parse the release note index from the
/// Flutter website at [_flutterDocsSite]/[_releaseNotesPath].
///
/// Calls [_emptyAndClose] and returns `null` if
/// the retrieval or parsing fails.
@visibleForTesting
Future<Map<String, Object?>?> retrieveReleasesFromIndex() async {
Future<Map<String, String>?> retrieveReleasesFromIndex() async {
final Map<String, Object?> releaseIndex;
try {
final releaseIndexString = await http.read(releaseIndexUrl);
Expand All @@ -212,7 +227,7 @@ class ReleaseNotesController extends SidePanelController {
);
return null;
}
return releaseIndex;
return releases.cast<String, String>();
}

/// Set the release notes viewer as having no contents, hidden,
Expand Down
2 changes: 1 addition & 1 deletion packages/devtools_app/release_notes/NEXT_RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This is draft for future release notes, that are going to land on
The 2.36.0 release of the Dart and Flutter DevTools
includes the following changes among other general improvements.
To learn more about DevTools, check out the
[DevTools overview]({{site.url}}/tools/devtools/overview).
[DevTools overview](/tools/devtools/overview).

## General updates

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This is draft for future release notes, that are going to land on
The <number> release of the Dart and Flutter DevTools
includes the following changes among other general improvements.
To learn more about DevTools, check out the
[DevTools overview]({{site.url}}/tools/devtools/overview).
[DevTools overview](/tools/devtools/overview).

## General updates

Expand Down
6 changes: 2 additions & 4 deletions packages/devtools_app/test/shared/release_notes_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,9 @@ void main() {
test('Parses expected index format correctly', () async {
await http.runWithClient(
() async {
final response = await controller.retrieveReleasesFromIndex();
expect(response!.keys, hasLength(2));
expect(response['latest'], equals('2.32.0'));
final releaseIndex = await controller.retrieveReleasesFromIndex();
expect(
response['releases'],
releaseIndex,
equals({
'2.32.0':
'/tools/devtools/release-notes/release-notes-2.32.0-src.md',
Expand Down

0 comments on commit a6f9d88

Please sign in to comment.