Skip to content

Commit ded5a31

Browse files
committed
Make redirect pages for categories
1 parent 9ab9ba0 commit ded5a31

File tree

4 files changed

+61
-31
lines changed

4 files changed

+61
-31
lines changed

lib/src/generator/generator_backend.dart

+12-2
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ abstract class GeneratorBackend {
8686
);
8787
}
8888
var e = data.self;
89-
writer.write(filename, content,
90-
element: e is Warnable ? e : null);
89+
writer.write(filename, content, element: e is Warnable ? e : null);
9190
}
9291

9392
/// Emits JSON describing the [categories] defined by the package.
@@ -117,11 +116,22 @@ abstract class GeneratorBackend {
117116
writer.write(_pathContext.join('index.json'), '$json\n');
118117
}
119118

119+
String _redirectContent(Category category) => '''<!DOCTYPE html>
120+
<html>
121+
<head>
122+
<meta http-equiv="refresh" content="0; url=${category.fileName}" />
123+
</head>
124+
</html> ''';
125+
120126
/// Emits documentation content for the [category].
121127
void generateCategory(PackageGraph packageGraph, Category category) {
122128
var data = CategoryTemplateData(options, packageGraph, category);
123129
var content = templates.renderCategory(data);
124130
write(writer, category.filePath, data, content);
131+
if (category.filePath != category.legacyFilePath) {
132+
writer.write(category.legacyFilePath, _redirectContent(category));
133+
}
134+
125135
runtimeStats.incrementAccumulator('writtenCategoryFileCount');
126136
}
127137

lib/src/model/category.dart

+12-8
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Category
2727
@override
2828
final Package package;
2929

30-
final String? _name;
30+
final String? _internalName;
3131

3232
@override
3333
final DartdocOptionContext config;
@@ -62,10 +62,10 @@ class Category
6262

6363
final CategoryDefinition _categoryDefinition;
6464

65-
Category(this._name, this.package, this.config)
65+
Category(this._internalName, this.package, this.config)
6666
: _categoryDefinition =
67-
config.categories.categoryDefinitions[_name.orDefault] ??
68-
CategoryDefinition(_name, null, null);
67+
config.categories.categoryDefinitions[_internalName.orDefault] ??
68+
CategoryDefinition(_internalName, null, null);
6969

7070
Iterable<ExternalItem> get externalItems => _categoryDefinition.externalItems;
7171

@@ -84,7 +84,7 @@ class Category
8484
String get name => _categoryDefinition.displayName;
8585

8686
@override
87-
String get sortKey => _name.orDefault;
87+
String get sortKey => _internalName.orDefault;
8888

8989
@override
9090
List<String> get containerOrder => config.categoryOrder;
@@ -105,11 +105,15 @@ class Category
105105
late final bool isDocumented =
106106
documentedWhere != DocumentLocation.missing && documentationFile != null;
107107

108-
String get filePath {
109-
assert(_name != null);
110-
return 'topics/$_name-topic.html';
108+
String get fileName {
109+
assert(_internalName != null);
110+
return '$_internalName-topic.html';
111111
}
112112

113+
String get filePath => 'topics/$fileName';
114+
115+
String get legacyFilePath => 'topics/$name-topic.html';
116+
113117
@override
114118
String? get href => isCanonical ? '${package.baseHref}$filePath' : null;
115119

test/end2end/model_test.dart

+5
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,11 @@ void main() async {
730730
});
731731
});
732732

733+
test('Verify legacyFilePath set', () {
734+
var category = packageGraph.publicPackages.first.categories.first;
735+
expect(category.legacyFilePath, 'topics/Superb-topic.html');
736+
});
737+
733738
group('LibraryContainer', () {
734739
late final TestLibraryContainer topLevel;
735740
var sortOrderBasic = ['theFirst', 'second', 'fruit'];

test/templates/category_test.dart

+32-21
Original file line numberDiff line numberDiff line change
@@ -74,52 +74,53 @@ analyzer:
7474
dartdocOptions: '''
7575
dartdoc:
7676
categories:
77-
One:
77+
cat1:
7878
markdown: one.md
79+
displayName: One
7980
Documented:
8081
markdown: documented.md
8182
''',
8283
libFiles: [
8384
d.file('lib.dart', '''
8485
/// A class.
85-
/// {@category One}
86+
/// {@category cat1}
8687
class C1 {}
8788
8889
/// A constant.
89-
/// {@category One}
90+
/// {@category cat1}
9091
const c1 = 1;
9192
9293
/// An enum.
93-
/// {@category One}
94+
/// {@category cat1}
9495
enum E1 { one, two }
9596
9697
/// A function.
97-
/// {@category One}
98+
/// {@category cat1}
9899
void F1() {}
99100
100101
/// A mixin.
101-
/// {@category One}
102+
/// {@category cat1}
102103
mixin M1 {}
103104
104105
/// A property.
105-
/// {@category One}
106+
/// {@category cat1}
106107
var p1 = 1;
107108
108109
/// A typedef.
109-
/// {@category One}
110+
/// {@category cat1}
110111
typedef T1 = void Function();
111112
112113
/// A typedef.
113-
/// {@category One}
114+
/// {@category cat1}
114115
// TODO(srawlins): Properly unit-test "typedef pointing to typedef".
115116
typedef T2 = T1;
116117
117118
/// An extension.
118-
/// {@category One}
119+
/// {@category cat1}
119120
extension Ex on int {}
120121
121122
/// An extension type.
122-
/// {@category One}
123+
/// {@category cat1}
123124
extension type ExType(int it) {}
124125
'''),
125126
d.file('other.dart', '''
@@ -136,7 +137,7 @@ library;
136137
await utils.writeDartdocResources(resourceProvider);
137138
await (await buildDartdoc()).generateDocs();
138139
topicOneLines = resourceProvider
139-
.getFile(path.join(packagePath, 'doc', 'topics', 'One-topic.html'))
140+
.getFile(path.join(packagePath, 'doc', 'topics', 'cat1-topic.html'))
140141
.readAsStringSync()
141142
.split('\n');
142143
indexPageLines = resourceProvider
@@ -145,6 +146,14 @@ library;
145146
.split('\n');
146147
});
147148

149+
test('redirect category file created', () async {
150+
final redirectContent = resourceProvider
151+
.getFile(path.join(packagePath, 'doc', 'topics', 'One-topic.html'))
152+
.readAsStringSync();
153+
154+
expect(redirectContent, contains('<meta http-equiv="refresh" content="0;'));
155+
});
156+
148157
test('page links to classes annotated with category', () async {
149158
topicOneLines.expectMainContentContainsAllInOrder([
150159
matches('<h2>Classes</h2>'),
@@ -214,7 +223,7 @@ library;
214223
topicOneLines,
215224
containsAllInOrder([
216225
matches('<div id="dartdoc-sidebar-right" '),
217-
matches('<a href="../topics/One-topic.html#classes">Classes</a>'),
226+
matches('<a href="../topics/cat1-topic.html#classes">Classes</a>'),
218227
matches('<a href="../lib/C1-class.html">C1</a>'),
219228
]),
220229
);
@@ -225,7 +234,7 @@ library;
225234
topicOneLines,
226235
containsAllInOrder([
227236
matches('<div id="dartdoc-sidebar-right" '),
228-
matches('<a href="../topics/One-topic.html#enums">Enums</a>'),
237+
matches('<a href="../topics/cat1-topic.html#enums">Enums</a>'),
229238
matches('<a href="../lib/E1.html">E1</a>'),
230239
]),
231240
);
@@ -236,7 +245,7 @@ library;
236245
topicOneLines,
237246
containsAllInOrder([
238247
matches('<div id="dartdoc-sidebar-right" '),
239-
matches('<a href="../topics/One-topic.html#mixins">Mixins</a>'),
248+
matches('<a href="../topics/cat1-topic.html#mixins">Mixins</a>'),
240249
matches('<a href="../lib/M1-mixin.html">M1</a>'),
241250
]),
242251
);
@@ -247,7 +256,7 @@ library;
247256
topicOneLines,
248257
containsAllInOrder([
249258
matches('<div id="dartdoc-sidebar-right" '),
250-
matches('<a href="../topics/One-topic.html#constants">Constants</a>'),
259+
matches('<a href="../topics/cat1-topic.html#constants">Constants</a>'),
251260
matches('<a href="../lib/c1-constant.html">c1</a>'),
252261
]),
253262
);
@@ -258,7 +267,8 @@ library;
258267
topicOneLines,
259268
containsAllInOrder([
260269
matches('<div id="dartdoc-sidebar-right" '),
261-
matches('<a href="../topics/One-topic.html#properties">Properties</a>'),
270+
matches(
271+
'<a href="../topics/cat1-topic.html#properties">Properties</a>'),
262272
matches('<a href="../lib/p1.html">p1</a>'),
263273
]),
264274
);
@@ -269,7 +279,7 @@ library;
269279
topicOneLines,
270280
containsAllInOrder([
271281
matches('<div id="dartdoc-sidebar-right" '),
272-
matches('<a href="../topics/One-topic.html#functions">Functions</a>'),
282+
matches('<a href="../topics/cat1-topic.html#functions">Functions</a>'),
273283
matches('<a href="../lib/F1.html">F1</a>'),
274284
]),
275285
);
@@ -280,7 +290,7 @@ library;
280290
topicOneLines,
281291
containsAllInOrder([
282292
matches('<div id="dartdoc-sidebar-right" '),
283-
matches('<a href="../topics/One-topic.html#typedefs">Typedefs</a>'),
293+
matches('<a href="../topics/cat1-topic.html#typedefs">Typedefs</a>'),
284294
matches('<a href="../lib/T1.html">T1</a>'),
285295
]),
286296
);
@@ -291,7 +301,8 @@ library;
291301
topicOneLines,
292302
containsAllInOrder([
293303
matches('<div id="dartdoc-sidebar-right" '),
294-
matches('<a href="../topics/One-topic.html#extensions">Extensions</a>'),
304+
matches(
305+
'<a href="../topics/cat1-topic.html#extensions">Extensions</a>'),
295306
matches('<a href="../lib/Ex.html">Ex</a>'),
296307
]),
297308
);
@@ -302,7 +313,7 @@ library;
302313
topicOneLines,
303314
containsAllInOrder([
304315
matches('<div id="dartdoc-sidebar-right" '),
305-
matches('<a href="../topics/One-topic.html#extension-types">'
316+
matches('<a href="../topics/cat1-topic.html#extension-types">'
306317
'Extension Types</a>'),
307318
matches('<a href="../lib/ExType-extension-type.html">ExType</a>'),
308319
]),

0 commit comments

Comments
 (0)