Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
import { fileDirectory } from './sidebar.mjs';
// The docs index the app's command palette searches (`src/pages/app-search.json.ts`). The app commits a copy
// (`scripts/sync-app-search-index.mjs`) and opens results on the deployed site, so the index holds page ids and heading
// anchors without the deployment base path: every deploy target produces the same file. Plain JavaScript so
// `node --test` can cover it.
/** Deeper headings are too fine-grained to be useful palette results. */
const HEADING_DEPTHS = new Set([2, 3]);
/**
* Developer and contributor docs stay out of the app: palette results are capped, and their many API and checklist
* headings would crowd out the user docs.
*/
const EXCLUDED_TOPIC_GROUPS = new Set(['Build']);
/**
* The app search index. Keys are short because the app bundles the whole index. The app mirrors this shape in
* `invokeai/frontend/webv2/src/workbench/palette/docsSearchProvider.ts`.
* @typedef {object} AppSearchIndex
* @property {AppSearchIndexTopic[]} topics The topics the index covers, in the topic switcher's order.
* @property {AppSearchPage[]} pages
*/
/**
* @typedef {object} AppSearchIndexTopic
* @property {string} l The topic label.
* @property {string} d The topic description.
* @property {string} s The topic's landing page: its URL path without the base path or slashes.
*/
/**
* One docs page in the app search index.
* @typedef {object} AppSearchPage
* @property {string} s The page id: its URL path without the base path or slashes, e.g. `users-guide/models/loras`.
* @property {string} t The page title.
* @property {string} [d] The page description.
* @property {number} [c] The index in `topics` of the topic the page belongs to.
* @property {[anchor: string, text: string][]} [h] The page's h2 and h3 headings, in document order.
*/
/**
* @typedef {{ depth: number, slug: string, text: string }} MarkdownHeading
* @typedef {{ label: string, description: string, group: string, link: string, directory: string }} AppSearchTopic
* @typedef {object} AppSearchDoc
* @property {string} id
* @property {string} [filePath]
* @property {{ title: string, description?: string, draft?: boolean, pagefind?: boolean, template?: string }} data
*/
/**
* Whether a docs page can be in the index at all: published, indexed by the site's own search, and documentation
* rather than a landing page. The splash template is the home page's, whose content is a component, not documentation.
* @param {AppSearchDoc} doc
*/
const isSearchablePage = (doc) =>
doc.id !== '404' && !doc.data.draft && doc.data.pagefind !== false && doc.data.template !== 'splash';
/**
* The topic whose directory holds the page; the most specific one wins, as `users-guide/models` (Models) does over
* `users-guide` (Guide). This mirrors the autogenerated topic sidebars in `src/config/sidebar.ts`.
* @param {AppSearchDoc} doc
* @param {AppSearchTopic[]} topics
*/
const findTopic = (doc, topics) => {
const directory = fileDirectory(doc.filePath);
let match;
for (const topic of topics) {
const inTopic = directory === topic.directory || directory.startsWith(`${topic.directory}/`);
if (inTopic && (!match || topic.directory.length > match.directory.length)) match = topic;
}
return match;
};
/** @param {string} link */
const toPageId = (link) => link.replace(/^\/+|\/+$/g, '');
/**
* Builds the app search index. Pages follow their topics' order, then page id, so the same docs always produce the
* same file and the app breaks ranking ties in favor of the topics readers meet first.
* @template {AppSearchDoc} Doc
* @param {Doc[]} docs
* @param {{ topics: AppSearchTopic[], getHeadings: (doc: Doc) => Promise<MarkdownHeading[]> }} options
* @returns {Promise<AppSearchIndex>}
*/
export const buildAppSearchIndex = async (docs, { topics, getHeadings }) => {
const included = topics.filter((topic) => !EXCLUDED_TOPIC_GROUPS.has(topic.group));
const candidates = docs
.filter(isSearchablePage)
.map((doc) => ({ doc, topic: findTopic(doc, topics) }))
.filter(({ topic }) => !topic || included.includes(topic))
.map(({ doc, topic }) => ({ doc, topicIndex: topic ? included.indexOf(topic) : included.length }))
.sort((a, b) => a.topicIndex - b.topicIndex || (a.doc.id < b.doc.id ? -1 : a.doc.id > b.doc.id ? 1 : 0));
const pages = await Promise.all(
candidates.map(async ({ doc, topicIndex }) => {
const headings = (await getHeadings(doc))
.filter((heading) => HEADING_DEPTHS.has(heading.depth))
.map((heading) => /** @type {[string, string]} */ ([heading.slug, heading.text]));
/** @type {AppSearchPage} */
const page = { s: doc.id, t: doc.data.title };
if (doc.data.description) page.d = doc.data.description;
if (topicIndex < included.length) page.c = topicIndex;
if (headings.length > 0) page.h = headings;
return page;
})
);
return {
topics: included.map((topic) => ({ l: topic.label, d: topic.description, s: toPageId(topic.link) })),
pages,
};
};
/**
* The committed copy's text: one topic or page per line, so a docs change shows up as a readable diff.
* @param {AppSearchIndex} index
*/
export const formatAppSearchIndex = ({ topics, pages }) => {
/** @param {unknown[]} items */
const lines = (items) => (items.length === 0 ? '[]' : `[\n${items.map((item) => JSON.stringify(item)).join(',\n')}\n]`);
return `{"topics":${lines(topics)},\n"pages":${lines(pages)}}\n`;
};