import { html } from 'lit'; import { ifDefined } from 'lit/directives/if-defined.js'; import { document } from '../layout-helper.js'; import { resolve } from '../../resolve.js'; import { webAwesomeComponents } from '@rocket/js/components/web-awesome.js'; import { addBootstrapIconLibrary } from '../layout.js'; import { pageNavigationLinks } from '../../menus/pageNavigation.js'; import { rocketDemoComponents } from '../../components.js'; export const DEFAULT_ATLAS_DOC_NAVIGATION_ICON_SERVER_BUDGET = 35; /** @type {import('@rocket/js/types.js').Components} */ export const atlasDocComponents = { 'rocket-menu': { file: './menus/RocketMenu.js', className: 'RocketMenu', loading: 'server', }, 'rocket-toc': { file: './menus/RocketToc.js', className: 'RocketToc', loading: 'hydrate:onVisible', }, 'rocket-next-page': { file: './menus/RocketNextPage.js', className: 'RocketNextPage', loading: 'server', }, 'rocket-previous-page': { file: './menus/RocketPreviousPage.js', className: 'RocketPreviousPage', loading: 'server', }, 'rocket-social-link': { file: './components/RocketSocialLink.js', className: 'RocketSocialLink', loading: 'server', }, ...rocketDemoComponents, ...webAwesomeComponents, }; /** * Render socials as rocket-social-link elements. * @param {Array<{url: string, name: string, label?: string}>} socials * @param {string} siteName */ export function renderSocials(socials, siteName) { return socials.map( social => html` `, ); } /** * Renders the header logo. * If a single image is provided, it will be used as the logo. * If two images are provided, the first will be used as the logo (make sure to provide it without any surrounding text) and the second as the text. * @param {Array} logo * @param {string} siteName */ export function renderHeaderLogo(logo, siteName) { if (logo.length === 1) { return html`${siteName}`; } else if (logo.length === 2) { return html` ${siteName} `; } else { return html`Header logo should only contain one or two images.`; } } /** * @param {import('@rocket/js/types.js').HomeNavLink} link */ export function renderHeaderNavLink(link) { const isExternal = link.external ?? link.href.startsWith('http'); if (isExternal) { return html` ${link.text} `; } return html`${link.text}`; } /** * @param {import('@rocket/js/types.js').HomeNavLink[]} links */ export function renderHeaderNavLinks(links = []) { return links.map(link => renderHeaderNavLink(link)); } /** * @param {import('@rocket/js/types.js').HomeNavLink[]} links */ export function renderHeaderNav(links = []) { if (!links.length) { return html``; } return html` `; } /** * @param {string[] | undefined} stylesheets */ export function renderStylesheets(stylesheets = []) { return stylesheets.map(href => html``); } /** * @param {import('@rocket/js/types.js').PageMetadataCustomValue | undefined} customValue * @returns {import('@rocket/js/types.js').AtlasDocAsideTip | false | undefined} */ export function resolveAsideTip(customValue) { const atlasDocCustom = readRecord(customValue); return atlasDocCustom ? normalizeAsideTip(atlasDocCustom.asideTip) : undefined; } /** * @param {import('@rocket/js/types.js').AtlasDocAsideTip | false | undefined} asideTip */ export function renderAsideTip(asideTip) { if (!asideTip) { return ''; } return html`

${asideTip.title || 'Tip'}

${asideTip.description}

`; } /** * @param {import('@rocket/js/types.js').PageMetadataCustomValue | undefined} value * @returns {import('@rocket/js/types.js').AtlasDocAsideTip | false | undefined} */ function normalizeAsideTip(value) { if (value === undefined || value === false) { return value; } const tip = readRecord(value); if (!tip || typeof tip.description !== 'string') { return undefined; } return { ...(typeof tip.title === 'string' ? { title: tip.title } : {}), description: tip.description, ...(typeof tip.iconName === 'string' ? { iconName: tip.iconName } : {}), }; } /** * @param {import('@rocket/js/types.js').PageMetadataCustomValue | undefined} value * @returns {Record | undefined} */ function readRecord(value) { if (typeof value !== 'object' || value === null || Array.isArray(value)) { return undefined; } return /** @type {Record} */ ( value ); } /** * @param {import('@rocket/js/types.js').TableOfContents} toc */ function serializeToc(toc) { return JSON.stringify(toc); } /** @type {import('@rocket/js/types.js').Layout} */ export const atlasDocLayout = (pageData, data) => { addBootstrapIconLibrary(pageData); const cssPath = resolve('@rocket/js/docs/assets/prism-one-light.css', import.meta); const siteName = pageData.siteHeadMetadata?.siteName ?? pageData.title; const asideTip = resolveAsideTip(pageData.metadata.custom?.atlasDoc); const pageNavigation = pageNavigationLinks(pageData.pageTree, pageData.url); const serializedToc = serializeToc(pageData.toc); const navigationIconServerBudget = data.navigationIconServerBudget ?? DEFAULT_ATLAS_DOC_NAVIGATION_ICON_SERVER_BUDGET; const headContent = data.headContent?.({ pageData }); return document( pageData, html`
${renderHeaderLogo(data.headerData.logo, siteName)}
On this page
${pageData.content}
${ pageNavigation.previous || pageNavigation.next ? html`` : '' }
`, { menu: false, headContent: html` ${renderStylesheets(data.stylesheets)} ${headContent} `, }, ); };