diff --git a/lib/src/dartdoc/dartdoc_index.dart b/lib/src/dartdoc/dartdoc_index.dart index 055e2ec23..1053b2650 100644 --- a/lib/src/dartdoc/dartdoc_index.dart +++ b/lib/src/dartdoc/dartdoc_index.dart @@ -6,6 +6,8 @@ import 'dart:convert'; import 'package:json_annotation/json_annotation.dart'; +import 'dartdoc_internals.dart'; + part 'dartdoc_index.g.dart'; /// The parsed content of the `index.json` generated by dartdoc. @@ -71,9 +73,7 @@ class DartdocIndexEntry { /// Weather the entry is a top-level library: /// - pre-8.3.0 the file ended with `-library.html` /// - with 8.3.0 the reference is a top-level directory with no slash in it - late final isLibrary = href != null && - href!.isNotEmpty && - (href!.endsWith('-library.html') || !href!.contains('/')); + late final isLibrary = isHrefALibrary(href); /// Whether the entry is a class declaration. bool get isClass => href != null && href!.endsWith('-class.html'); diff --git a/lib/src/dartdoc/dartdoc_internals.dart b/lib/src/dartdoc/dartdoc_internals.dart new file mode 100644 index 000000000..868d68bf9 --- /dev/null +++ b/lib/src/dartdoc/dartdoc_internals.dart @@ -0,0 +1,22 @@ +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +/// Infers whether a dartdoc href is a link to a library page. +/// - pre-8.3.0 the file ended with `-library.html` +/// - with 8.3.0 the reference is a top-level directory with no slash in it +bool isHrefALibrary(String? href) { + if (href == null || href.isEmpty) { + return false; + } + // libraries before 8.3 + if (href.endsWith('-library.html')) { + return true; + } + // libraries after 8.3 do not have the .html suffix in their href + if (href.endsWith('.html')) { + return false; + } + // libraries have no slash in their hrefs + return !href.contains('/'); +} diff --git a/lib/src/dartdoc/pub_dartdoc_data.dart b/lib/src/dartdoc/pub_dartdoc_data.dart index e94e4dd7d..645a6c4fc 100644 --- a/lib/src/dartdoc/pub_dartdoc_data.dart +++ b/lib/src/dartdoc/pub_dartdoc_data.dart @@ -4,6 +4,8 @@ import 'package:json_annotation/json_annotation.dart'; +import 'dartdoc_internals.dart'; + part 'pub_dartdoc_data.g.dart'; @JsonSerializable() @@ -55,9 +57,7 @@ class ApiElement { /// Weather the entry is a top-level library: /// - pre-8.3.0 the file ended with `-library.html` /// - with 8.3.0 the reference is a top-level directory with no slash in it - late final isLibrary = href != null && - href!.isNotEmpty && - (href!.endsWith('-library.html') || !href!.contains('/')); + late final isLibrary = isHrefALibrary(href); /// Whether the entry is a class declaration. late final isClass =