@mintlify/common
Version:
Commonly shared code within Mintlify
49 lines (48 loc) • 2.51 kB
JavaScript
import { getAsyncApiChannelMetadata } from '../asyncapi/getAsyncApiChannelMetadata.js';
import { prepAsyncApiFrontmatter } from '../asyncapi/prepAsyncApiFrontmatter.js';
import { parseFrontmatter } from '../frontmatter/index.js';
import { removeLeadingSlash, optionallyAddLeadingSlash } from '../fs/index.js';
import { getOpenApiTitleAndDescription } from '../openapi/getOpenApiTitleAndDescription.js';
import { pagePathToSlug } from './pagePathToSlug.js';
import { slugToTitle } from './slugToTitle.js';
export const getDecoratedNavPageAndSlug = (pagePath, pageContent, openApiFiles, asyncApiFiles) => {
let metadata = {};
try {
metadata = parseFrontmatter(pageContent).attributes;
}
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);
let schemaTitle;
let schemaDescription;
if (metadata.openapi) {
const { title: openApiSchemaTitle, description: openApiSchemaDescription } = getOpenApiTitleAndDescription(openApiFiles, metadata.openapi);
schemaTitle = openApiSchemaTitle;
schemaDescription = openApiSchemaDescription;
}
if (metadata.asyncapi) {
metadata.asyncapi = prepAsyncApiFrontmatter(pagePath, metadata.asyncapi);
const parsedMetadata = getAsyncApiChannelMetadata(metadata.asyncapi, asyncApiFiles);
schemaTitle = parsedMetadata === null || parsedMetadata === void 0 ? void 0 : parsedMetadata.title;
schemaDescription = parsedMetadata === null || parsedMetadata === void 0 ? void 0 : parsedMetadata.description;
}
const pageMetadata = Object.assign(Object.assign({ title: schemaTitle !== null && schemaTitle !== void 0 ? schemaTitle : defaultTitle, description: schemaDescription }, metadata), { href: optionallyAddLeadingSlash(slug) });
return {
pageMetadata,
slug: removeLeadingSlash(slug),
};
};