Skip to content

Show single exports of resolved types in nav bar #280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions addon/components/docs-header/search-results/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ export default Component.extend(EKMixin, {
// Filter out modules that are not in the navigationIndex
.filter(({ document }) => {
if (document.type === 'module') {
let navigableModules = this.get('project.navigationIndex.modules').map(x => x.name);
return navigableModules.includes(document.title);
let navigableModules = this.get('project.navigationIndex').find(section => section.type === 'modules');
let navigableModuleIds = navigableModules ? navigableModules.items.map(item => item.id) : [];

return navigableModuleIds.includes(document.title);
} else {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion addon/components/docs-viewer/x-main/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default Component.extend({
tagName: 'main',
classNames: [
'docs-px-4', 'md:docs-px-8', 'lg:docs-px-20', 'docs-mx-auto', 'md:docs-mx-0', 'docs-mt-6',
'md:docs-mt-12', 'md:docs-min-w-0'
'md:docs-mt-12', 'md:docs-min-w-0', 'md:docs-flex-1'
],

didInsertElement() {
Expand Down
10 changes: 5 additions & 5 deletions addon/components/docs-viewer/x-nav/template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@
{{yield (hash
section=(component 'docs-viewer/x-section')
item=(component 'docs-viewer/x-nav-item')
subnav=(component 'docs-viewer/x-nav-list' class='ml-4')
subnav=(component 'docs-viewer/x-nav-list' class='docs-ml-4')
)}}

{{!-- Autogenerated API docs --}}
{{#if (gt project.modules.length 0)}}
{{docs-viewer/x-section 'API Reference' style='large'}}

{{#each-in project.navigationIndex as |type items|}}
{{docs-viewer/x-section type}}
{{#each project.navigationIndex as |section|}}
{{docs-viewer/x-section section.type}}

{{#each items as |item|}}
{{#each section.items as |item|}}
{{docs-viewer/x-nav-item (break-on item.name '/') (concat root '.api.item') item.path}}
{{/each}}
{{/each-in}}
{{/each}}
{{/if}}
{{/docs-viewer/x-nav-list}}

Expand Down
4 changes: 2 additions & 2 deletions addon/components/docs-viewer/x-section/template.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<li class='docs-mt-8
{{if (eq style 'large') 'docs--mb-4 docs-text-xs'}}
<li class='docs-mt-8
{{if (eq style 'large') 'docs--mb-4 docs-text-xs docs-uppercase' 'docs-capitalize'}}
'>
{{label}}
</li>
78 changes: 2 additions & 76 deletions lib/broccoli/docs-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const CachingWriter = require('broccoli-caching-writer');
const Serializer = require('../serializers/main');

const compileMarkdown = require('../utils/compile-markdown');
const NavigationIndexGenerator = require('./docs-compiler/navigation-index-generator');

function compileDescriptions(objects) {
if (!objects) { return; }
Expand Down Expand Up @@ -72,81 +73,6 @@ function compileDocDescriptions(modules) {
});
}

const RESOLVED_TYPES = [
'components',
'helpers',
'controllers',
'mixins',
'models',
'services'
];

function generateResolvedTypeNavigationItems(modules, type) {
let items = modules.map(m => {
let segments = m.file.split('/');
segments = segments.slice(segments.indexOf(type) + 1);

if (type.match(segments[segments.length - 1])) {
segments.pop();
}

let path = segments.join('/');
let name;

if (['components', 'helpers'].includes(type)) {
name = `{{${path}}}`;
} else {
let fileName = segments.pop();
name = _.upperFirst(_.camelCase(fileName));
}

return {
path: `${type}/${path}`,
name
};
});

return _.sortBy(items, ['path']);
}

function generateNavigationIndex(modules, klasses) {
let navigationIndex = {};

for (let type of RESOLVED_TYPES) {
let resolvedModules = modules.filter(m => m.file.match(`${type}/`) && !m.file.match('utils/'));

navigationIndex[type] = generateResolvedTypeNavigationItems(resolvedModules, type);
}

navigationIndex.modules = modules
.filter(m => {
return (
!m.file.match(new RegExp(`(${RESOLVED_TYPES.join('|')})/`)) ||
m.file.match('utils/')
);
})
.map(m => {
return {
path: `modules/${m.id}`,
name: m.id
};
});


for (let type in navigationIndex) {
let items = navigationIndex[type];

if (
(Array.isArray(items) && items.length === 0)
|| (!Array.isArray(items) && Object.keys(items).length === 0)
) {
delete navigationIndex[type];
}
}

return navigationIndex;
}

module.exports = class DocsCompiler extends CachingWriter {
constructor(inputNodes, options) {
let defaults = {
Expand Down Expand Up @@ -180,7 +106,7 @@ module.exports = class DocsCompiler extends CachingWriter {
id: name,
name: name,
version: projectVersion,
navigationIndex: generateNavigationIndex(modules),
navigationIndex: (new NavigationIndexGenerator).generate(modules),
modules
}

Expand Down
143 changes: 143 additions & 0 deletions lib/broccoli/docs-compiler/navigation-index-generator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
const _ = require('lodash');
const RESOLVED_TYPES = [
'components',
'helpers',
'controllers',
'mixins',
'models',
'services',
'classes'
];

module.exports = class NavigationIndexGenerator {

generate(modules) {
// The index is an array - the order is important for the UI
let navIndex = [...RESOLVED_TYPES, 'modules'].map(type => ({
type,
items: []
}));

// Push each module into the right section
modules.forEach(module => {
let moduleType = this._getModuleType(module);
let navItem = this._getNavItemForModule(module, moduleType);

navIndex.find(({ type }) => type === moduleType)
.items
.push(navItem);
});

// Filter out empty sections, and sort non-empty section items by path
return navIndex
.filter(section => section.items.length > 0)
.map(section => {
section.items = _.sortBy(section.items, ['name']);

return section;
});
}


// Private

_getModuleType(module) {
let resolvedType = this._resolvedTypeForModule(module);

if (!resolvedType && this._hasSingleExportOfResolvedType(module)) {
let singleExport = this._exportsForModuleOfResolvedTypes(module)[0];
resolvedType = singleExport.type;
}

return resolvedType || 'modules';
}

_resolvedTypeForModule(module) {
return RESOLVED_TYPES.find(type => {
return module.file.match(`${type}/`) && !module.file.match('utils/');
});
}

_isResolvedType(module) {
return this._resolvedTypeForModule(module);
}

_exportsForModuleOfResolvedTypes(module) {
return [
...module.classes.map(item => ({ type: 'classes', export: item })),
...module.components.map(item => ({ type: 'components', export: item }))
];
}

_exportsForModuleOfNonResolvedTypes(module) {
return [
...module.functions.map(item => ({ type: 'functions', export: item })),
...module.variables.map(item => ({ type: 'variables', export: item })),
];
}

_hasSingleExportOfResolvedType(module) {
return (
this._exportsForModuleOfResolvedTypes(module).length === 1 &&
this._exportsForModuleOfNonResolvedTypes(module).length === 0
);
}

_getNavItemForModule(module, type) {
let navItem;

if (this._isResolvedType(module)) {
navItem = this._getNavItemForModuleOfResolvedType(module, type);
} else if (this._hasSingleExportOfResolvedType(module)) {
navItem = this._getNavItemForModuleWithSingleExport(module, type);
} else {
navItem = this._getNavItemForGenericModule(module, type)
}

return navItem;
}

_getNavItemForModuleOfResolvedType(module, type) {
let segments = module.file.split('/');
segments = segments.slice(segments.indexOf(type) + 1);

if (type.match(segments[segments.length - 1])) {
segments.pop();
}

let path = segments.join('/');
let name;

if (['components', 'helpers'].includes(type)) {
name = `{{${path}}}`;
} else {
let fileName = segments.pop();
name = _.upperFirst(_.camelCase(fileName));
}

return {
id: module.id,
path: `${type}/${path}`,
name
};
}

_getNavItemForModuleWithSingleExport(module, type) {
let singleExport = module[type][0];

return {
id: singleExport.id,
path: `modules/${singleExport.id}`,
name: singleExport.name
};
}

_getNavItemForGenericModule(module, type) {
return {
id: module.id,
path: `modules/${module.id}`,
name: module.id
};
}

}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
"broccoli-stew": "^2.0.0",
"ember-cli-autoprefixer": "^0.8.1",
"ember-cli-babel": "^6.16.0",
"ember-cli-clipboard": "^0.9.0",
"ember-cli-htmlbars": "^3.0.0",
"ember-cli-htmlbars-inline-precompile": "^1.0.3",
"ember-cli-clipboard": "^0.10.0",
"ember-cli-sass": "7.1.7",
"ember-cli-string-helpers": "^1.9.0",
"ember-cli-tailwind": "^0.6.2",
Expand Down
Loading