This repository was archived by the owner on Nov 9, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathindexing-utils.mjs
More file actions
293 lines (258 loc) · 8.05 KB
/
Copy pathindexing-utils.mjs
File metadata and controls
293 lines (258 loc) · 8.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import { readFile, readdir } from "fs/promises";
import { execSync } from "child_process";
import matter from "gray-matter";
import { Search } from "@upstash/search";
// Constants
export const MAX_TOTAL = 4096;
export const SAFETY_BUFFER = 8;
export const CONTENT_DIRS = {
BLOG: "./src/content/blog",
PROJECTS: "./src/content/projects",
};
// Document schema:
// id: unique slug or slug#chunk
// content: {
// t: Title (first chunk only)
// d: Description (first chunk only)
// c: Content type ("b" = blog, "p" = project)
// b: Body/searchable text content
// }
/**
* Initialize Upstash Search client and indexes
*/
export function initUpstashClient() {
const client = new Search({
url: process.env.UPSTASH_SEARCH_REST_URL,
token: process.env.UPSTASH_SEARCH_REST_TOKEN,
});
return {
client,
searchIndex: client.index("content"),
metaIndex: client.index("meta"),
};
}
/**
* Get the last git change timestamp for a file
*/
export function getLastGitChange(filePath) {
try {
return Number(execSync(`git log -1 --format="%ct" -- ${filePath}`).toString().trim());
} catch {
return 0;
}
}
/**
* Clean markdown text by removing code blocks, syntax, and normalizing whitespace
*/
export function cleanText(text) {
return text
.replace(/```[\s\S]*?```/g, "")
.replace(/[`*_#>~\-]/g, "")
.replace(/\[(.*?)\]\(.*?\)/g, "$1")
.replace(/\s+/g, " ")
.trim();
}
/**
* Calculate the dynamic body limit for a chunk based on JSON overhead
* @param {string} id - The actual document ID that will be used
* @param {string} t - Title (empty for subsequent chunks)
* @param {string} d - Description (empty for subsequent chunks)
* @param {string} c - Content type ("b" or "p")
*/
export function calcDynamicBodyLimit({ id, t, d, c }) {
const sample = {
id,
content: { t, d, c, b: "" },
};
const jsonOverhead = JSON.stringify(sample).length;
return MAX_TOTAL - jsonOverhead - SAFETY_BUFFER;
}
/**
* Split text into chunks by word boundaries
* Ensures no words are broken across chunks
*/
export function splitIntoDynamicChunksByWords(text, firstChunkLimit, regularLimit) {
const words = text.split(" ");
const chunks = [];
let currentChunk = "";
let currentLimit = firstChunkLimit;
for (const word of words) {
// Check if adding this word would exceed the limit
const wouldExceed = (currentChunk.length + word.length + 1) > currentLimit;
if (wouldExceed && currentChunk.trim().length > 0) {
// Push current chunk (without the word that would overflow)
chunks.push(currentChunk.trim());
// Start new chunk with this word
currentChunk = word + " ";
currentLimit = regularLimit;
} else {
// Add word to current chunk
currentChunk += word + " ";
}
}
// Push the last chunk if it has content
if (currentChunk.trim().length > 0) chunks.push(currentChunk.trim());
return chunks;
}
/**
* Create document objects from chunks for a markdown file
*/
export function createDocumentsFromChunks({ slug, title, description, contentType, chunks }) {
return chunks.map((chunk, i) => {
const body = chunk.trim();
const doc = {
id: i === 0 ? slug : `${slug}#${i + 1}`,
content: {
t: i === 0 ? title || "" : "",
d: i === 0 ? description || "" : "",
c: contentType,
b: body,
},
};
// Enforce byte-safe limit after serialization
// If we exceed, trim by words to avoid breaking words
const jsonLen = JSON.stringify(doc).length;
if (jsonLen > MAX_TOTAL) {
const overBy = jsonLen - MAX_TOTAL;
const targetLength = Math.max(0, doc.content.b.length - overBy - SAFETY_BUFFER);
// Trim by words, not characters, to avoid breaking words
if (targetLength < doc.content.b.length) {
const words = doc.content.b.split(" ");
let trimmedBody = "";
for (const word of words) {
if ((trimmedBody.length + word.length + 1) <= targetLength) {
trimmedBody += (trimmedBody ? " " : "") + word;
} else {
break;
}
}
doc.content.b = trimmedBody.trim();
}
}
return doc;
});
}
/**
* Process a single markdown file and return documents ready for indexing
*/
export async function processMarkdownFile(filePath, type) {
const { data, content } = matter(await readFile(filePath, "utf8"));
if (data.draft) {
return null;
}
const cleaned = cleanText(content);
const contentType = type === "blog" ? "b" : "p";
const slug = data.slug;
// Calculate limit for first chunk using actual slug ID
const firstChunkId = slug;
const firstChunkLimit = calcDynamicBodyLimit({
id: firstChunkId,
t: data.title || "",
d: data.description || "",
c: contentType,
});
// Calculate limit for subsequent chunks using first subsequent chunk ID format (slug#2)
// IDs like slug#2, slug#3, slug#10 are all same or shorter length, so this is safe
const subsequentChunkId = `${slug}#2`;
const regularLimit = calcDynamicBodyLimit({
id: subsequentChunkId,
t: "",
d: "",
c: contentType,
});
const chunks = splitIntoDynamicChunksByWords(cleaned, firstChunkLimit, regularLimit);
const docs = createDocumentsFromChunks({
slug: data.slug,
title: data.title || "",
description: data.description || "",
contentType,
chunks,
});
return {
file: filePath.split("/").pop(),
slug: data.slug,
chunks: chunks.length,
documents: docs,
};
}
/**
* Get meta cache from Upstash
*/
export async function getMetaCache(metaIndex) {
try {
const res = await metaIndex.fetch(["__cache__"]);
return res?.[0]?.metadata?.cache || {};
} catch {
return {};
}
}
/**
* Set meta cache in Upstash
*/
export async function setMetaCache(metaIndex, cache) {
await metaIndex.upsert([
{
id: "__cache__",
content: { updatedAt: Date.now() },
metadata: { cache },
},
]);
}
/**
* Check if a file is a markdown file
* @param {string} file - Filename to check
* @returns {boolean} True if file is markdown
*/
export function isMarkdownFile(file) {
return file.endsWith(".md") || file.endsWith(".mdx");
}
/**
* Extract parent slug from document ID (removes chunk number suffix like #2, #3, etc.)
* @param {string} docId - Document ID (e.g., "slug" or "slug#2")
* @returns {string} Parent slug without chunk number
*/
export function getParentSlug(docId) {
const hashIndex = docId.indexOf("#");
return hashIndex === -1 ? docId : docId.substring(0, hashIndex);
}
/**
* Build a cache object from already processed documents
* Maps document IDs to their git commit timestamps
* @param {Array} processedFiles - Array of { filePath, documents } objects
* @returns {Object} Cache object with parent slug-> git timestamp mapping
*/
export function buildCacheFromProcessedFiles(processedFiles) {
const cache = {};
for (const { filePath, documents } of processedFiles) {
const lastGitChange = getLastGitChange(filePath);
// Only store the parent slug (first document is always the parent)
if (documents.length > 0) {
const parentSlug = getParentSlug(documents[0].id);
cache[parentSlug] = lastGitChange;
}
}
return cache;
}
/**
* Build a cache object mapping parent slugs to their git commit timestamps
* @param {string} dir - Directory path to process
* @param {string} type - Content type ("blog" or "project")
* @returns {Promise<Object>} Cache object with parent slug -> git timestamp mapping
*/
export async function buildCacheFromFiles(dir, type) {
const cache = {};
const files = await readdir(dir);
for (const file of files) {
if (!isMarkdownFile(file)) continue;
const filePath = `${dir}/${file}`;
const lastGitChange = getLastGitChange(filePath);
const result = await processMarkdownFile(filePath, type);
if (!result) continue; // Skip drafts
// Only store the parent slug (first document is always the parent)
if (result.documents.length > 0) {
const parentSlug = getParentSlug(result.documents[0].id);
cache[parentSlug] = lastGitChange;
}
}
return cache;
}