Skip to content

Commit 6aebaac

Browse files
committed
Code review fixes
- Add a CLI flag (hidden) to use the legacy behavior - Change placeholder string - Detailed comment regarding usage of the placeholder
1 parent 501ffad commit 6aebaac

File tree

7 files changed

+50
-13
lines changed

7 files changed

+50
-13
lines changed

lib/src/dartdoc_options.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,6 +1422,9 @@ class DartdocOptionContext extends DartdocOptionContextBase
14221422
excludePackages.any((pattern) => name == pattern);
14231423

14241424
String get templatesDir => optionSet['templatesDir'].valueAt(context);
1425+
1426+
// TODO(jdkoren): temporary while we confirm href base behavior doesn't break important clients
1427+
bool get useBaseHref => optionSet['useBaseHref'].valueAt(context);
14251428
}
14261429

14271430
/// Instantiate dartdoc's configuration file and options parser with the
@@ -1635,6 +1638,14 @@ Future<List<DartdocOption>> createDartdocOptions() async {
16351638
'property, top_level_constant, top_level_property, typedef. Partial templates are '
16361639
'supported; they must begin with an underscore, and references to them must omit the '
16371640
'leading underscore (e.g. use {{>foo}} to reference the partial template _foo.html).'),
1641+
DartdocOptionArgOnly<bool>('useBaseHref', false,
1642+
help:
1643+
'Use <base href> in generated files (legacy behavior). This option '
1644+
'is temporary and support will be removed in the future. Use only '
1645+
'if the default behavior breaks links between your documentation '
1646+
'pages, and please file an issue on Github.',
1647+
negatable: false,
1648+
hide: true),
16381649
// TODO(jcollins-g): refactor so there is a single static "create" for
16391650
// each DartdocOptionContext that traverses the inheritance tree itself.
16401651
]

lib/src/html/html_generator.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,17 @@ class HtmlGeneratorOptions implements HtmlOptions {
132132
@override
133133
final String toolVersion;
134134

135+
@override
136+
final bool useBaseHref;
137+
135138
HtmlGeneratorOptions(
136139
{this.url,
137140
this.relCanonicalPrefix,
138141
this.faviconPath,
139142
String toolVersion,
140143
this.prettyIndexJson = false,
141-
this.templatesDir})
144+
this.templatesDir,
145+
this.useBaseHref = false})
142146
: this.toolVersion = toolVersion ?? 'unknown';
143147
}
144148

@@ -156,7 +160,8 @@ Future<List<Generator>> initGenerators(GeneratorContext config) async {
156160
toolVersion: dartdocVersion,
157161
faviconPath: config.favicon,
158162
prettyIndexJson: config.prettyIndexJson,
159-
templatesDir: config.templatesDir);
163+
templatesDir: config.templatesDir,
164+
useBaseHref: config.useBaseHref);
160165

161166
return [
162167
await HtmlGenerator.create(

lib/src/html/html_generator_instance.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ class HtmlGeneratorInstance {
7676
});
7777

7878
String json = encoder.convert(indexItems);
79-
json = json.replaceAll(HTMLBASE_PLACEHOLDER, '');
79+
if (!_options.useBaseHref) {
80+
json = json.replaceAll(HTMLBASE_PLACEHOLDER, '');
81+
}
8082
_writer(path.join('categories.json'), '${json}\n');
8183
}
8284

@@ -119,7 +121,9 @@ class HtmlGeneratorInstance {
119121
});
120122

121123
String json = encoder.convert(indexItems);
122-
json = json.replaceAll(HTMLBASE_PLACEHOLDER, '');
124+
if (!_options.useBaseHref) {
125+
json = json.replaceAll(HTMLBASE_PLACEHOLDER, '');
126+
}
123127
_writer(path.join('index.json'), '${json}\n');
124128
}
125129

lib/src/html/template_data.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:dartdoc/src/model/model.dart';
88
abstract class HtmlOptions {
99
String get relCanonicalPrefix;
1010
String get toolVersion;
11+
bool get useBaseHref;
1112
}
1213

1314
abstract class TemplateData<T extends Documentable> {
@@ -38,6 +39,7 @@ abstract class TemplateData<T extends Documentable> {
3839
T get self;
3940
String get version => htmlOptions.toolVersion;
4041
String get relCanonicalPrefix => htmlOptions.relCanonicalPrefix;
42+
bool get useBaseHref => htmlOptions.useBaseHref;
4143

4244
String _layoutTitle(String name, String kind, bool isDeprecated) =>
4345
_renderHelper.composeLayoutTitle(name, kind, isDeprecated);

lib/src/model/package.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@ import 'package:pub_semver/pub_semver.dart';
1414

1515
final RegExp substituteNameVersion = RegExp(r'%([bnv])%');
1616

17+
// All hrefs are emitted as relative paths from the output root. We are unable
18+
// to compute them from the page we are generating, and many properties computed
19+
// using hrefs are memoized anyway. To build complete relative hrefs, we emit
20+
// the href with this placeholder, and then replace it with the current page's
21+
// base href afterwards.
22+
// See https://github.com/dart-lang/dartdoc/issues/2090 for further context.
1723
// Unlikely to be mistaken for an identifier, html tag, or something else that
1824
// might reasonably exist normally.
19-
final String HTMLBASE_PLACEHOLDER = '\%\%HTMLBASE\%\%';
25+
final String HTMLBASE_PLACEHOLDER = '\%\%__HTMLBASE_dartdoc_internal__\%\%';
2026

2127
/// A [LibraryContainer] that contains [Library] objects related to a particular
2228
/// package.
@@ -210,7 +216,7 @@ class Package extends LibraryContainer
210216
});
211217
if (!_baseHref.endsWith('/')) _baseHref = '${_baseHref}/';
212218
} else {
213-
_baseHref = HTMLBASE_PLACEHOLDER;
219+
_baseHref = config.useBaseHref ? '' : HTMLBASE_PLACEHOLDER;
214220
}
215221
}
216222
return _baseHref;

lib/templates/_footer.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
<!-- footer-text placeholder -->
1212
</footer>
1313

14+
{{! TODO(jdkoren): unwrap ^useBaseHref sections when the option is removed.}}
1415
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
15-
<script src="%%HTMLBASE%%static-assets/typeahead.bundle.min.js"></script>
16-
<script src="%%HTMLBASE%%static-assets/highlight.pack.js"></script>
17-
<script src="%%HTMLBASE%%static-assets/URI.js"></script>
18-
<script src="%%HTMLBASE%%static-assets/script.js"></script>
16+
<script src="{{^useBaseHref}}%%HTMLBASE_dartdoc_internal%%{{/useBaseHref}}static-assets/typeahead.bundle.min.js"></script>
17+
<script src="{{^useBaseHref}}%%HTMLBASE_dartdoc_internal%%{{/useBaseHref}}static-assets/highlight.pack.js"></script>
18+
<script src="{{^useBaseHref}}%%HTMLBASE_dartdoc_internal%%{{/useBaseHref}}static-assets/URI.js"></script>
19+
<script src="{{^useBaseHref}}%%HTMLBASE_dartdoc_internal%%{{/useBaseHref}}static-assets/script.js"></script>
1920
<!-- footer placeholder -->
2021

2122
</body>

lib/templates/_head.html

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,19 @@
1313
<link rel="canonical" href="{{{relCanonicalPrefix}}}/{{{self.href}}}">
1414
{{ /relCanonicalPrefix}}
1515

16+
{{#useBaseHref}}{{! TODO(jdkoren): remove when the useBaseHref option is removed.}}
17+
{{#htmlBase}}
18+
<!-- required because all the links are pseudo-absolute -->
19+
<base href="{{{htmlBase}}}">
20+
{{/htmlBase}}
21+
{{/useBaseHref}}
22+
23+
{{! TODO(jdkoren): unwrap ^useBaseHref sections when the option is removed.}}
1624
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500,400i,400,300|Source+Sans+Pro:400,300,700" rel="stylesheet">
1725
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
18-
<link rel="stylesheet" href="%%HTMLBASE%%static-assets/github.css">
19-
<link rel="stylesheet" href="%%HTMLBASE%%static-assets/styles.css">
20-
<link rel="icon" href="%%HTMLBASE%%static-assets/favicon.png">
26+
<link rel="stylesheet" href="{{^useBaseHref}}%%HTMLBASE_dartdoc_internal%%{{/useBaseHref}}static-assets/github.css">
27+
<link rel="stylesheet" href="{{^useBaseHref}}%%HTMLBASE_dartdoc_internal%%{{/useBaseHref}}static-assets/styles.css">
28+
<link rel="icon" href="{{^useBaseHref}}%%HTMLBASE_dartdoc_internal%%{{/useBaseHref}}static-assets/favicon.png">
2129
<!-- header placeholder -->
2230
</head>
2331

0 commit comments

Comments
 (0)