const fs = require('fs-extra');
const path = require('path');
const marked = require('marked');
let sidebar = require('./src/sidebar');
const redirects = require('./src/redirects');
const menuItems = require('./src/menu.js');
const examples = require('./src/examples');
const { encode, decode } = require('html-entities');
const { JSDOM } = require('jsdom');
const esbuild = require('esbuild');
const MiniSearch = require('minisearch');
const { generatePlayground } = require('./src/playground');
const { generateRecipes, loadRecipes } = require('./src/recipes');
const { parseFrontMatter } = require('./src/frontmatter');
const { generateChangelog, parseChangelog, readChangelog } = require('./src/changelog');
// Index-time tokenizer. Splits on whitespace AND punctuation, and ALSO emits
// contiguous pairwise (2-gram) joins of the parts:
// "Node.js" → ["node", "js", "nodejs"]
// "puter.ai.chat()" → ["puter", "ai", "chat", "puterai", "aichat"]
// This lets queries like "nodejs", "puterai", or "aichat" match directly.
function indexTokenize (text) {
if ( !text ) return [];
const tokens = [];
for ( const chunk of text.split(/[\n\r\p{Z}]+/u) ) {
if ( !chunk ) continue;
const parts = chunk.split(/\p{P}+/u).filter(Boolean);
// Individual parts
for ( const part of parts ) tokens.push(part);
// Pairwise adjacent joins (linear in parts.length)
for ( let i = 0; i + 1 < parts.length; i++ ) {
tokens.push(parts[i] + parts[i + 1]);
}
}
return tokens;
}
const MINISEARCH_INDEX_CONFIG = {
fields: ['title', 'text', 'pageTitle'],
storeFields: ['title', 'pageTitle', 'path', 'anchor', 'text'],
tokenize: indexTokenize,
};
const site = 'https://docs.puter.com';
let usedPlaygroundExamples = new Set();
let anyErrors = false;
// The newest changelog day, for the dot on the sidebar's "What's new" link.
const changelogLatest = parseChangelog(readChangelog()).latest;
// Heading ids already used on the page being parsed. Reset before each parse.
let usedHeadingSlugs = new Set();
marked.use({
hooks: {
preprocess (markdown) {
usedHeadingSlugs = new Set();
return markdown;
},
},
renderer: {
// Add a link to each subheading
heading (text, level) {
// `text` is rendered HTML, so drop tags and entities before slugging.
const baseSlug = decode(removeTags(text))
.toLowerCase()
.replace(/[^\w]+/g, '-')
.replace(/^-+|-+$/g, '') || 'section';
// A repeated heading gets a numbered id, so each anchor is unique.
let slug = baseSlug;
for ( let i = 1; usedHeadingSlugs.has(slug); i++ ) {
slug = `${baseSlug}-${i}`;
}
usedHeadingSlugs.add(slug);
return `
`;
html += escaped ? code : encode(code);
html += 'Redirecting to ${to}...
`; const redirectDir = path.join(distDir, from); createDirectoryRecursively(redirectDir); fs.writeFileSync(path.join(redirectDir, 'index.html'), redirectHTML); }); } function generateSitemap () { const urls = [ `${site}/`, ]; sidebar.forEach((item) => { if ( item.path ) { urls.push(`${site}${item.path}/`); } if ( item.children && Array.isArray(item.children) ) { item.children.forEach((child) => { if ( child.path ) { urls.push(`${site}${child.path}/`); } }); } }); urls.push(`${site}/recipes/`); (loadRecipes() ?? []).forEach((recipe) => { urls.push(`${site}/recipes/${recipe.slug}/`); }); examples.forEach((category) => { if ( category.children && Array.isArray(category.children) ) { category.children.forEach((example) => { if ( example.slug !== undefined ) { if ( example.slug === '' ) { urls.push(`${site}/playground/`); } else { urls.push(`${site}/playground/${example.slug}/`); } } }); } }); let xml = '\n'; xml += '