@ztl-uwu/nuxt-content
Version:
Write your content inside your Nuxt app
43 lines (42 loc) • 1.6 kB
JavaScript
import { defineEventHandler } from "h3";
import { isPreview } from "../preview.js";
export default defineEventHandler(async (event) => {
const { getContentQuery } = await import("../../utils/query.js");
const { cacheStorage, serverQueryContent } = await import("../storage.js");
const { createNav } = await import("../navigation.js");
const query = getContentQuery(event);
if (!isPreview(event) && Object.keys(query).length === 0) {
const cache = await cacheStorage().getItem("content-navigation.json");
if (cache) {
return cache;
}
}
const contents = await serverQueryContent(event, query).where({
/**
* Partial contents are not included in the navigation
* A partial content is a content that has `_` prefix in its path
*/
_partial: false,
/**
* Exclude any pages which have opted out of navigation via frontmatter.
*/
navigation: {
$ne: false
}
}).find();
const _locale = (query?.where || []).find((w) => w._locale)?._locale;
const dirConfigs = await serverQueryContent(event, _locale ? { where: [{ _locale }] } : void 0).where({ _path: /\/_dir$/i, _partial: true }).find();
const configs = (dirConfigs?.result || dirConfigs).reduce((configs2, conf) => {
if (conf.title?.toLowerCase() === "dir") {
conf.title = void 0;
}
const key = conf._path.split("/").slice(0, -1).join("/") || "/";
configs2[key] = {
...conf,
// Extract meta from body. (non MD files)
...conf.body
};
return configs2;
}, {});
return createNav(contents?.result || contents, configs);
});