@barnabask/astro-minisearch
Version:
Utilities to add a static full text index to an Astro project
61 lines • 2.07 kB
JavaScript
import { z } from "zod";
import Slugger from "github-slugger";
import { pluginOptionValidator, } from "./types.js";
const pageValidator = z.object({
url: z.string(),
frontmatter: z.record(z.any()),
});
const sluggers = new Map();
function slugifyUrl(url, heading) {
if (!url || !heading || url.includes("#"))
return url;
const slugger = sluggers.get(url) || new Slugger();
if (!sluggers.has(url))
sluggers.set(url, slugger);
return [url, slugger.slug(heading)].join("#");
}
/**
* Convert a single page to an array of SearchDocuments.
* @ignore
*/
export function pageToDocuments(page, contentKey) {
const { url, frontmatter } = page;
const { [contentKey]: textData, title, ...other } = frontmatter;
let sections = [];
if (!textData) {
return [];
}
else if (typeof textData === "string") {
// if plain text was a simple string, treat like an object with null key
sections = [{ heading: "", text: textData }];
}
else if (Array.isArray(textData)) {
sections = textData.map(([heading, text]) => {
return { heading, text };
});
}
return sections.map(({ heading, text }) => {
return { url: slugifyUrl(url, heading), heading, title, text, ...other };
});
}
/**
* Converts glob output of static pages to an array of {@link types!SearchDocument}s.
*
* @param pages result of of [`import.meta.glob`](https://vitejs.dev/guide/features.html#glob-import)
* @param options plugin options (optional)
* @example
* ```ts
* pagesGlobToDocuments(import.meta.glob('./**\/*.md*'));
* ```
*/
export async function pagesGlobToDocuments(pages, options = {}) {
const documents = [];
const opts = pluginOptionValidator.parse(options);
const contentKey = opts.contentKey;
await Promise.all(Object.values(pages).map(async (getInfo) => {
const page = pageValidator.parse(await getInfo());
documents.push(...pageToDocuments(page, contentKey));
}));
return documents;
}
//# sourceMappingURL=pages.js.map