UNPKG

@mintlify/common

Version:

Commonly shared code within Mintlify

44 lines (43 loc) 1.99 kB
import matter from 'gray-matter'; import { removeLeadingSlash, optionallyAddLeadingSlash } from '../fs/index.js'; import { getOpenApiTitleAndDescription } from '../openapi/getOpenApiTitleAndDescription.js'; import { prepOpenApiFrontmatter } from '../openapi/prepOpenApiFrontmatter.js'; import { pagePathToSlug } from './pagePathToSlug.js'; import { slugToTitle } from './slugToTitle.js'; export const getDecoratedNavPageAndSlug = (pagePath, pageContent, openApiFiles) => { let metadata = {}; try { metadata = matter(pageContent).data; } catch (error) { if (error && typeof error === 'object' && 'mark' in error && error.mark && typeof error.mark === 'object' && 'line' in error.mark && typeof error.mark.line === 'number') { const columnText = 'column' in error.mark && error.mark.column !== 0 ? `, column ${error.mark.column}` : ''; throw new Error(`There is a syntax error in your frontmatter on line ${error.mark.line}${columnText} in ${pagePath}`); } else { throw new Error(`Unable to parse page metadata in ${pagePath}`); } } const slug = pagePathToSlug(pagePath); const defaultTitle = slugToTitle(slug); // Append data from OpenAPI if it exists let openApiTitle; let openApiDescription; if (metadata.openapi) { metadata.openapi = prepOpenApiFrontmatter(pagePath, metadata.openapi); const { title, description } = getOpenApiTitleAndDescription(openApiFiles, metadata.openapi); openApiTitle = title; openApiDescription = description; } const pageMetadata = Object.assign(Object.assign({ title: openApiTitle !== null && openApiTitle !== void 0 ? openApiTitle : defaultTitle, description: openApiDescription }, metadata), { href: optionallyAddLeadingSlash(slug) }); return { pageMetadata, slug: removeLeadingSlash(slug), }; };