Skip to content

Commit

Permalink
Better structure for the is-library-detection code. (#1423)
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos authored Nov 26, 2024
1 parent 5dfd160 commit 840f2c3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
6 changes: 3 additions & 3 deletions lib/src/dartdoc/dartdoc_index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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');
Expand Down
22 changes: 22 additions & 0 deletions lib/src/dartdoc/dartdoc_internals.dart
Original file line number Diff line number Diff line change
@@ -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('/');
}
6 changes: 3 additions & 3 deletions lib/src/dartdoc/pub_dartdoc_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import 'package:json_annotation/json_annotation.dart';

import 'dartdoc_internals.dart';

part 'pub_dartdoc_data.g.dart';

@JsonSerializable()
Expand Down Expand Up @@ -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 =
Expand Down

0 comments on commit 840f2c3

Please sign in to comment.