Skip to content

Commit

Permalink
refractor: 将格式化文献的函数重构到公共函数中
Browse files Browse the repository at this point in the history
  • Loading branch information
14790897 committed Feb 15, 2024
1 parent 5e7afea commit 06f57ab
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
4 changes: 2 additions & 2 deletions components/Export.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as quillToWord from "quill-to-word";
import { useAppDispatch, useAppSelector } from "@/app/store";
import ReduxProvider from "@/app/store/ReduxProvider";
import { Reference } from "@/utils/global";
import { formatAllReferencesForCopy } from "@/utils/others/quillutils";
import { getAllFullReferences } from "@/utils/others/quillutils";
type ParaIn = {
editor: any;
};
Expand All @@ -24,7 +24,7 @@ const ExportDocx = ({ editor }: ParaIn) => {
insert: "\n参考文献\n",
},
];
const referencesString = formatAllReferencesForCopy(references);
const referencesString = getAllFullReferences(references);
const quillReferences = [{ insert: referencesString }];
// 合并标题和引用列表
return referencesWithTitle.concat(quillReferences);
Expand Down
25 changes: 2 additions & 23 deletions components/ReferenceList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import React, { useState } from "react";
import { Reference } from "@/utils/global";
import {
copyToClipboard,
formatReferenceForCopy,
formatAllReferencesForCopy,
getFullReference,
getAllFullReferences,
delteIndexUpdateBracketNumbersInDeltaKeepSelection,
} from "@/utils/others/quillutils";
//删除文献按钮
Expand Down Expand Up @@ -95,27 +95,6 @@ function ReferenceList({ editor, lng }: ReferenceListProps) {
}
}, [references]);

function formatReference(reference: Reference) {
if (reference.journal) {
return `[J]. ${reference.journal}. `;
} else if (reference.journalReference) {
return `[J]. ${reference.journalReference}`;
} else {
return `${reference.venue}, ${reference.year}.`;
}
}
function getFullReference(reference: Reference) {
let fullReference = `${reference.author}. ${reference.title}`;
fullReference += formatReference(reference);
return fullReference;
}
function getAllFullReferences(references: Reference[]) {
return references
.map((reference, index) => {
return `[${index + 1}] ${getFullReference(reference)}`;
})
.join("\n");
}
return (
<div className=" mx-auto p-4">
{/* 引用列表显示区域 */}
Expand Down
23 changes: 23 additions & 0 deletions utils/others/quillutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ export function formatTextInEditor(editor: Quill) {
updateBracketNumbersInDeltaKeepSelection(editor);
}

//这个是格式化semantic scholar的
export function formatJournalReference(entry: any) {
if (!entry.journal) {
return ""; // 如果没有期刊信息,直接返回空字符串
Expand All @@ -247,6 +248,28 @@ export function formatJournalReference(entry: any) {
return reference;
}

function formatReference(reference: Reference) {
if (reference.journal) {
return `[J]. ${reference.journal}. `;
} else if (reference.journalReference) {
return `[J]. ${reference.journalReference}`;
} else {
return `${reference.venue}, ${reference.year}.`;
}
}
export function getFullReference(reference: Reference) {
let fullReference = `${reference.author}. ${reference.title}`;
fullReference += formatReference(reference);
return fullReference;
}
export function getAllFullReferences(references: Reference[]) {
return references
.map((reference, index) => {
return `[${index + 1}] ${getFullReference(reference)}`;
})
.join("\n");
}

export {
getTextBeforeCursor,
updateBracketNumbersInDelta,
Expand Down

0 comments on commit 06f57ab

Please sign in to comment.