diff --git a/modules/hudozka-api/src/controllers/breadcrumb.ts b/modules/hudozka-api/src/controllers/breadcrumb.ts new file mode 100644 index 00000000..cda40fb5 --- /dev/null +++ b/modules/hudozka-api/src/controllers/breadcrumb.ts @@ -0,0 +1,23 @@ +import { getPathWaterfall } from '../lib/url' +import { Breadcrumb } from '../types' +import Page from '../core/Page' + +export async function getItems(path: string): Promise { + const bc = getPathWaterfall(path) + const parts = await Promise.all(bc.map(async href => { + const resource = await Page.findByUrl(href) + + if (!resource) { + return null + } + + const name = resource.title + + return { + name, + href, + } + })) + + return parts.filter(Boolean) +} diff --git a/modules/hudozka-api/src/controllers/page.ts b/modules/hudozka-api/src/controllers/page.ts index 7425caf7..58f99752 100644 --- a/modules/hudozka-api/src/controllers/page.ts +++ b/modules/hudozka-api/src/controllers/page.ts @@ -2,31 +2,11 @@ import { Request, Response } from 'express' import Page from '../core/Page' import { dropTrailingSlash, getPathWaterfall } from '../lib/url' import { encodePage } from '../factory/page' -import { Breadcrumb } from '../types' - -async function getBreadcrumbs(path: string): Promise { - const bc = getPathWaterfall(path) - const parts = await Promise.all(bc.map(async href => { - const resource = await Page.findByUrl(href) - - if (!resource) { - return null - } - - const name = resource.title - - return { - name, - href, - } - })) - - return parts.filter(Boolean) -} +import * as breadcrumb from './breadcrumb' export async function getItem(req: Request, res: Response) { const page = dropTrailingSlash(req.query.page) - const breadcrumbs = await getBreadcrumbs(page) + const breadcrumbs = await breadcrumb.getItems(page) const resource = await Page.findByUrl(page) if (resource) {