Skip to content

Commit 774022c

Browse files
committed
Fix rel canonical link
1 parent 3169171 commit 774022c

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,5 +406,19 @@ 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], testPackageMinimumDir, tempDir);
414+
await dartdoc.generateDocsBase();
415+
416+
File file = File(path.join(tempDir.path, 'small', 'small-library.html'));
417+
expect(file.existsSync(), isTrue);
418+
expect(
419+
file.readAsStringSync(),
420+
contains(
421+
'<link rel="canonical" href="$prefix/small/small-library.html">'));
422+
});
409423
}, timeout: Timeout.factor(8));
410424
}

0 commit comments

Comments
 (0)