Skip to content

Commit 57c7032

Browse files
authored
Fix rel canonical link (#2126)
* Fix rel canonical link * Verify files at different levels in test
1 parent 3169171 commit 57c7032

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

lib/src/html/template_data.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:dartdoc/src/render/template_renderer.dart';
65
import 'package:dartdoc/src/model/model.dart';
76

87
abstract class HtmlOptions {
@@ -40,6 +39,13 @@ abstract class TemplateData<T extends Documentable> {
4039
String get relCanonicalPrefix => htmlOptions.relCanonicalPrefix;
4140
bool get useBaseHref => htmlOptions.useBaseHref;
4241

42+
String get bareHref {
43+
if (self is Indexable) {
44+
return (self as Indexable).href.replaceAll(HTMLBASE_PLACEHOLDER, '');
45+
}
46+
return '';
47+
}
48+
4349
String _layoutTitle(String name, String kind, bool isDeprecated) =>
4450
packageGraph.rendererFactory.templateRenderer
4551
.composeLayoutTitle(name, kind, isDeprecated);

lib/templates/_head.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<meta name="description" content="{{ metaDescription }}">
1111
<title>{{ title }}</title>
1212
{{ #relCanonicalPrefix }}
13-
<link rel="canonical" href="{{{relCanonicalPrefix}}}/{{{self.href}}}">
13+
<link rel="canonical" href="{{{relCanonicalPrefix}}}/{{{bareHref}}}">
1414
{{ /relCanonicalPrefix}}
1515

1616
{{#useBaseHref}}{{! TODO(jdkoren): remove when the useBaseHref option is removed.}}

test/dartdoc_test.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,5 +406,24 @@ void main() {
406406
expect(e is DartdocFailure, isTrue);
407407
}
408408
});
409+
410+
test('rel canonical prefix does not include base href', () async {
411+
final String prefix = 'foo.bar/baz';
412+
Dartdoc dartdoc = await buildDartdoc(
413+
['--rel-canonical-prefix', prefix], testPackageDir, tempDir);
414+
await dartdoc.generateDocsBase();
415+
416+
// Verify files at different levels have correct <link> content.
417+
File level1 = File(path.join(tempDir.path, 'ex', 'Apple-class.html'));
418+
expect(level1.existsSync(), isTrue);
419+
expect(
420+
level1.readAsStringSync(),
421+
contains(
422+
'<link rel="canonical" href="$prefix/ex/Apple-class.html">'));
423+
File level2 = File(path.join(tempDir.path, 'ex', 'Apple', 'm.html'));
424+
expect(level2.existsSync(), isTrue);
425+
expect(level2.readAsStringSync(),
426+
contains('<link rel="canonical" href="$prefix/ex/Apple/m.html">'));
427+
});
409428
}, timeout: Timeout.factor(8));
410429
}

0 commit comments

Comments
 (0)