fix: address code review issues — fulltext_md_path clear, key validation, redo no auto-run, ribbon button

This commit is contained in:
Research Assistant 2026-06-01 11:36:29 +08:00
parent ed95776edd
commit f2bee97a0a
3 changed files with 38 additions and 17 deletions

File diff suppressed because one or more lines are too long

View file

@ -30,6 +30,19 @@ export default class PaperForgePlugin extends Plugin {
try { addIcon(PF_ICON_ID, PF_RIBBON_SVG); } catch (_) {}
this.addRibbonIcon(PF_ICON_ID, "PaperForge Dashboard", () => PaperForgeStatusView.open(this as any));
const redoAction = ACTIONS.find(a => a.id === "paperforge-ocr-redo");
if (redoAction) {
this.addRibbonIcon("reset", "PaperForge: 重做OCR", () => {
const vp = (this.app.vault.adapter as any).basePath as string;
new Notice(`PaperForge: 重做OCR starting...`);
const { path: py, extraArgs: ex } = resolvePythonExecutable(vp, this.settings, undefined, undefined);
execFile(py, [...ex, "-m", "paperforge", "ocr", "redo"], { cwd: vp, timeout: 600000 }, (err, stdout, stderr) => {
if (err) { new Notice(`PaperForge: 重做OCR failed`); return; }
new Notice(`PaperForge: 重做OCR done`);
});
});
}
this.addSettingTab(new PaperForgeSettingTab(this.app, this as any));
this.addCommand({
@ -50,7 +63,8 @@ export default class PaperForgePlugin extends Plugin {
const vp = (this.app.vault.adapter as any).basePath as string;
new Notice(`PaperForge: running ${a.cmd}...`);
const { path: cmdPythonExe, extraArgs: cmdExtra = [] } = resolvePythonExecutable(vp, this.settings, undefined, undefined);
execFile(cmdPythonExe, [...cmdExtra, "-m", "paperforge", a.cmd], { cwd: vp, timeout: 300000 }, (err, stdout, stderr) => {
const cmdArgs = Array.isArray(a.args) ? [...a.args] : [];
execFile(cmdPythonExe, [...cmdExtra, "-m", "paperforge", a.cmd, ...cmdArgs], { cwd: vp, timeout: 300000 }, (err, stdout, stderr) => {
if (err) {
new Notice(`[!!] ${a.cmd} failed: ${(stderr || err.message).slice(0, 120)}`, 8000);
return;

View file

@ -1712,6 +1712,9 @@ def run_ocr(vault: Path, verbose: bool = False, no_progress: bool = False, redo_
ocr_root = paths.get("ocr")
lit_root = paths.get("literature")
for key in redo_keys:
if not key or not re.match(r"^[A-Za-z0-9]{8}$", key):
logger.warning("Skipping invalid zotero_key for redo: %s", key)
continue
ocr_dir = ocr_root / key if ocr_root else None
if ocr_dir and ocr_dir.exists():
shutil.rmtree(ocr_dir)
@ -1729,9 +1732,13 @@ def run_ocr(vault: Path, verbose: bool = False, no_progress: bool = False, redo_
if key in text:
text = re.sub(r"^ocr_status:\s*.+$", "ocr_status: pending", text, flags=re.MULTILINE)
text = re.sub(r"^ocr_redo:\s*.+$", "ocr_redo: false", text, flags=re.MULTILINE)
text = re.sub(r"^fulltext_md_path:\s*.+$", "fulltext_md_path: ''", text, flags=re.MULTILINE)
note_file.write_text(text, encoding="utf-8")
logger.info("Reset frontmatter for %s", key)
break
# Only mark for OCR, do NOT auto-run
logger.info("OCR redo: marked %d paper(s) as pending. Run 'paperforge ocr run' to process.", len(redo_keys))
return 0
target_rows = []
for export_path in sorted(paths["exports"].glob("*.json")):