实现单篇和多篇论文的对比总结
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { markdownToNoteHtml } from "../../utils/markdown";
|
||||
|
||||
export async function createChildNote(options: {
|
||||
parentItem: Zotero.Item;
|
||||
title: string;
|
||||
@@ -7,94 +9,8 @@ export async function createChildNote(options: {
|
||||
note.libraryID = options.parentItem.libraryID;
|
||||
note.parentID = options.parentItem.id;
|
||||
|
||||
const html = markdownToSimpleHtml(options.title, options.bodyMarkdown);
|
||||
const html = markdownToNoteHtml(options.title, options.bodyMarkdown);
|
||||
note.setNote(html);
|
||||
await note.saveTx();
|
||||
return note;
|
||||
}
|
||||
|
||||
function escapeHtml(s: string): string {
|
||||
return s
|
||||
.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/"/g, """);
|
||||
}
|
||||
|
||||
/** Minimal markdown → HTML for Zotero notes */
|
||||
export function markdownToSimpleHtml(title: string, md: string): string {
|
||||
const lines = md.replace(/\r\n/g, "\n").split("\n");
|
||||
const parts: string[] = [
|
||||
`<h1>${escapeHtml(title)}</h1>`,
|
||||
`<p><i>Generated by ChatPapers · ${new Date().toLocaleString()}</i></p>`,
|
||||
];
|
||||
|
||||
let inList = false;
|
||||
let inCode = false;
|
||||
const codeBuf: string[] = [];
|
||||
|
||||
const closeList = () => {
|
||||
if (inList) {
|
||||
parts.push("</ul>");
|
||||
inList = false;
|
||||
}
|
||||
};
|
||||
|
||||
for (const line of lines) {
|
||||
if (line.startsWith("```")) {
|
||||
if (inCode) {
|
||||
parts.push(`<pre>${escapeHtml(codeBuf.join("\n"))}</pre>`);
|
||||
codeBuf.length = 0;
|
||||
inCode = false;
|
||||
} else {
|
||||
closeList();
|
||||
inCode = true;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (inCode) {
|
||||
codeBuf.push(line);
|
||||
continue;
|
||||
}
|
||||
|
||||
const heading = /^(#{1,3})\s+(.*)$/.exec(line);
|
||||
if (heading) {
|
||||
closeList();
|
||||
const level = heading[1].length;
|
||||
parts.push(`<h${level + 1}>${escapeHtml(heading[2])}</h${level + 1}>`);
|
||||
continue;
|
||||
}
|
||||
|
||||
const li = /^[-*]\s+(.*)$/.exec(line);
|
||||
if (li) {
|
||||
if (!inList) {
|
||||
parts.push("<ul>");
|
||||
inList = true;
|
||||
}
|
||||
parts.push(`<li>${inlineFormat(li[1])}</li>`);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!line.trim()) {
|
||||
closeList();
|
||||
continue;
|
||||
}
|
||||
|
||||
closeList();
|
||||
parts.push(`<p>${inlineFormat(line)}</p>`);
|
||||
}
|
||||
|
||||
closeList();
|
||||
if (inCode) {
|
||||
parts.push(`<pre>${escapeHtml(codeBuf.join("\n"))}</pre>`);
|
||||
}
|
||||
|
||||
return parts.join("\n");
|
||||
}
|
||||
|
||||
function inlineFormat(text: string): string {
|
||||
let s = escapeHtml(text);
|
||||
s = s.replace(/\*\*(.+?)\*\*/g, "<b>$1</b>");
|
||||
s = s.replace(/`([^`]+)`/g, "<code>$1</code>");
|
||||
return s;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user