Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
ttizze committed Dec 16, 2024
1 parent f050906 commit 55ed8d7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ describe("processHtmlContent", () => {
<p>Line B</p>
<p>Line C</p>
`;

const user = await prisma.user.upsert({
where: { id: 13 },
create: {
Expand All @@ -260,46 +260,46 @@ describe("processHtmlContent", () => {
},
update: {},
});

// 初回処理
await processHtmlContent(title, htmlInput, pageSlug, user.id, "en", true);

const dbPage1 = await prisma.page.findUnique({
where: { slug: pageSlug },
include: { sourceTexts: true },
});
expect(dbPage1).not.toBeNull();
if (!dbPage1) return;

// 初回処理時のIDを記憶
const originalTextIdMap = new Map<string, number>();
for (const st of dbPage1.sourceTexts) {
originalTextIdMap.set(st.text, st.id);
}
expect(originalTextIdMap.size).toBeGreaterThanOrEqual(3);

// 変更なしで再度同一HTMLを処理
await processHtmlContent(title, htmlInput, pageSlug, user.id, "en", true);

const dbPage2 = await prisma.page.findUnique({
where: { slug: pageSlug },
include: { sourceTexts: true },
});
expect(dbPage2).not.toBeNull();
if (!dbPage2) return;

// 再処理後のIDマッピングを取得
const afterTextIdMap = new Map<string, number>();
for (const st of dbPage2.sourceTexts) {
afterTextIdMap.set(st.text, st.id);
}

// 全てのテキストでIDが変わっていないことを確認
for (const [text, originalId] of originalTextIdMap.entries()) {
console.log(text, originalId);
expect(afterTextIdMap.get(text)).toBe(originalId);
}

// source_textsの数が増減していないこと(無駄な消去がないこと)
expect(dbPage2.sourceTexts.length).toBe(dbPage1.sourceTexts.length);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ function extractTextFromHAST(node: Parent): string {
});
return result;
}
export function rehypeAddDataId(pageId: number, title: string): Plugin<[], Root> {
export function rehypeAddDataId(
pageId: number,
title: string,
): Plugin<[], Root> {
return function attacher() {
return async (tree: Root, file: VFile) => {
const textOccurrenceMap = new Map<string, number>();
Expand Down Expand Up @@ -74,13 +77,12 @@ export function rehypeAddDataId(pageId: number, title: string): Plugin<[], Root>
}
});


const allTextsForDb = blocks.map((block, index) => ({
text: block.text,
textAndOccurrenceHash: block.textAndOccurrenceHash,
number: index + 1,
}))
}));

allTextsForDb.push({
text: title,
textAndOccurrenceHash: generateHashForText(title, 0),
Expand Down

0 comments on commit 55ed8d7

Please sign in to comment.