Skip to content

Commit bc44f65

Browse files
committed
fix: 优化相似度匹配并兼容旧版大纲数据
- 本地附件匹配改为忽略大小写,提升文件名匹配稳定性 - 将元数据检索相似度阈值改为可配置项,默认值设为 0.6 - 为旧版 outline JSON 增加迁移逻辑,加载时自动回写新 schema
1 parent 720029f commit bc44f65

File tree

5 files changed

+34
-7
lines changed

5 files changed

+34
-7
lines changed

addon/prefs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pref("namePatternCustom", "{%t}");
1212
pref("metadataSource", "CNKI");
1313
pref("isMainlandChina", true);
1414
pref("cnkiAttachmentCookie", "");
15+
pref("similarityThresholdForMetaData", "0.6");
1516
/* match pdf */
1617
pref("pdfMatchFolder", "");
1718
pref("actionAfterAttachmentImport", "backup")

src/modules/attachments/localMatch.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,20 @@ export class LocalAttachmentService implements AttachmentService {
3232
const scoredItems = attachmentFilenames.map((filename) => {
3333
const name = PathUtils.filename(filename);
3434
const name_no_ext = name.replace(/\.(pdf|caj|kdh|nh)$/i, "");
35+
const score = compareTwoStrings(
36+
searchString.toUpperCase(),
37+
name_no_ext.toUpperCase(),
38+
);
3539
ztoolkit.log(
36-
searchString,
40+
searchString.toUpperCase(),
3741
name,
38-
name_no_ext,
39-
compareTwoStrings(searchString, name_no_ext),
42+
name_no_ext.toUpperCase(),
43+
score,
4044
);
4145
return {
4246
title: name,
4347
filename: name,
44-
score: compareTwoStrings(searchString, name_no_ext),
48+
score: score,
4549
url: filename,
4650
source: "local",
4751
};

src/modules/outline/outline.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,23 @@ export async function saveOutlineToJSON(
287287
ztoolkit.log("Save outline to JSON");
288288
}
289289

290+
function migrateOutlineInfo(
291+
raw: any,
292+
fromSchema: number,
293+
): { outline: OutlineNode[]; baseFontSize: number } {
294+
let outline: OutlineNode[] = raw.outline ?? [];
295+
let baseFontSize = DEFAULT_BASE_FONT_SIZE;
296+
297+
// v1 → v2: add baseFontSize
298+
if (fromSchema < 2) {
299+
baseFontSize = raw.info?.baseFontSize ?? DEFAULT_BASE_FONT_SIZE;
300+
}
301+
302+
// Future v2 → v3 migrations go here
303+
304+
return { outline, baseFontSize };
305+
}
306+
290307
// 加载时要考虑JSON文件的版本信息,如果版本低,要重新从原文件加载信息
291308
export async function loadOutlineInfoFromJSON(
292309
item: Zotero.Item,
@@ -304,8 +321,12 @@ export async function loadOutlineInfoFromJSON(
304321
} else {
305322
const content = (await Zotero.File.getContentsAsync(outlinePath)) as string;
306323
const tmp = JSON.parse(content);
307-
if (tmp.info.schema < OUTLINE_SCHEMA) {
308-
return null;
324+
const fileSchema = tmp.info?.schema ?? 1;
325+
if (fileSchema < OUTLINE_SCHEMA) {
326+
// Migrate old outline data instead of discarding
327+
const migrated = migrateOutlineInfo(tmp, fileSchema);
328+
await saveOutlineToJSON(item, migrated.outline, migrated.baseFontSize);
329+
return migrated;
309330
} else {
310331
const outlineInfo = JSON.parse(content) as OutlineInfo;
311332
return {

src/modules/services/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export async function metaSearch(
9292
ztoolkit.log(`Similarity score for "${result.articleTitle}": ${score}`);
9393
return (
9494
!(result.articleTitle as string).includes(searchOption.title) &&
95-
score > 0.85
95+
score > parseFloat(getPref("similarityThresholdForMetaData"))
9696
);
9797
});
9898
scrapeSearchResults = filteredResults1.concat(filteredResults2);

typings/prefs.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ declare namespace _ZoteroTypes {
1818
"metadataSource": string;
1919
"isMainlandChina": boolean;
2020
"cnkiAttachmentCookie": string;
21+
"similarityThresholdForMetaData": string;
2122
"pdfMatchFolder": string;
2223
"actionAfterAttachmentImport": string;
2324
"similarityThreshold": string;

0 commit comments

Comments
 (0)