Skip to content

Commit 69641ad

Browse files
author
Savannah Mastrangelo
committed
Allow child routes under docs pages
This makes the docs-route service more tolerant of pages with their own internal URL structure. For example, we're using this in ember-animated to have a page that includes an outlet in order to demonstrate cross-route animations.
1 parent 7a85f7f commit 69641ad

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

addon/components/docs-viewer/x-main/template.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
{{/if}}
2727
</div>
2828

29-
<div class="w-1/2 text-right">
29+
<div class="w-1/2 text-right" data-test-next-link>
3030
{{#if docsRoutes.next}}
3131
<div class='text-sm text-grey-dark'>
3232
Next

addon/services/docs-routes.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,16 @@ export default Service.extend({
3939
if (this.get('routeUrls.length')) {
4040
let router = this.get('router.router');
4141
let currentURL = router.get('rootURL') + router.get('url');
42-
currentURL = currentURL
43-
.replace('//', '/') // dedup slashes
44-
.replace(/\?.+$/, '') // remove query-params
45-
.replace(/\/$/, '') // remove trailing slash
46-
.replace(/#.+$/, ''); // remove # anchor links
47-
let index = this.get('routeUrls').indexOf(currentURL);
48-
assert(`DocsRoutes wasn't able to correctly detect the current route. The current url is ${currentURL}`, index > -1);
49-
return index;
42+
currentURL = currentURL.replace('//', '/') // dedup slashes
43+
let longestIndex, longestPrefix;
44+
this.get('routeUrls').forEach((url, index) => {
45+
if (currentURL.indexOf(url) === 0 && (!longestPrefix || url.length > longestPrefix.length)) {
46+
longestIndex = index;
47+
longestPrefix = url;
48+
}
49+
});
50+
assert(`DocsRoutes wasn't able to correctly detect the current route. The current url is ${currentURL}`, longestIndex != null);
51+
return longestIndex;
5052
}
5153
}),
5254

tests/acceptance/docs-route-test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@ module('Acceptance | Docs route test', function(hooks) {
1919

2020
assert.dom('h1').hasText('DocsHero');
2121
});
22+
23+
test('I can visit a nested child route within the docs pages and still have the correct links', async function(assert) {
24+
await visit('/docs/deploying/test-nested-route');
25+
assert.dom('[data-test-next-link] > a').hasText('Hero');
26+
});
2227
});

tests/dummy/app/router.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ Router.map(function() {
1212
this.route('usage');
1313
this.route('quickstart');
1414
this.route('patterns');
15-
this.route('deploying');
15+
this.route('deploying', function() {
16+
// This exists so we can acceptance test whether the docs pages tolerate
17+
// embedded child routes. It's not used in the public documentation site.
18+
this.route('test-nested-route');
19+
});
1620

1721
this.route('components', function() {
1822
this.route('docs-hero');

0 commit comments

Comments
 (0)