feat: extract and index inline file blocks in markdown

This commit is contained in:
2026-07-08 15:46:19 +08:00
parent 9a5ee695c2
commit 2a9eee6410
2 changed files with 101 additions and 5 deletions
+33 -5
View File
@@ -11,6 +11,7 @@ import {
scanCompletions,
type CompletionsPayload,
} from "../completions/index.js";
import { extractInlineBlocks } from "../inline-blocks.js";
interface ContentIndex {
[path: string]: string;
@@ -105,7 +106,16 @@ export function scanDirectory(dir: string): ContentIndex {
) {
try {
const content = readFileSync(fullPath, "utf-8");
index[normalizedRelPath] = content;
if (entry.endsWith(".md")) {
const stripped = extractInlineBlocks(
content,
normalizedRelPath,
index,
);
index[normalizedRelPath] = stripped;
} else {
index[normalizedRelPath] = content;
}
} catch (e) {
console.error(`读取文件失败:${fullPath}`, e);
}
@@ -295,9 +305,18 @@ export function createContentServer(
try {
const content = readFileSync(path, "utf-8");
const relPath = "/" + relative(contentDir, path).split(sep).join("/");
contentIndex[relPath] = content;
if (relPath.endsWith(".md")) {
const stripped = extractInlineBlocks(
content,
relPath,
contentIndex,
);
contentIndex[relPath] = stripped;
recomputeCompletions();
} else {
contentIndex[relPath] = content;
}
console.log(`[新增] ${relPath}`);
if (relPath.endsWith(".md")) recomputeCompletions();
} catch (e) {
console.error(`读取新增文件失败:${path}`, e);
}
@@ -312,9 +331,18 @@ export function createContentServer(
try {
const content = readFileSync(path, "utf-8");
const relPath = "/" + relative(contentDir, path).split(sep).join("/");
contentIndex[relPath] = content;
if (relPath.endsWith(".md")) {
const stripped = extractInlineBlocks(
content,
relPath,
contentIndex,
);
contentIndex[relPath] = stripped;
recomputeCompletions();
} else {
contentIndex[relPath] = content;
}
console.log(`[更新] ${relPath}`);
if (relPath.endsWith(".md")) recomputeCompletions();
} catch (e) {
console.error(`读取更新文件失败:${path}`, e);
}