Skip to content

Commit

Permalink
add: api.convert.item2citation
Browse files Browse the repository at this point in the history
  • Loading branch information
windingwind committed Oct 26, 2023
1 parent ce8a130 commit b35d450
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
} from "./modules/template/data";
import { renderTemplatePreview } from "./modules/template/preview";
import { getWorkspaceEditor } from "./modules/workspace/content";
import { parseCitationHTML } from "./utils/citation";
import {
getEditorInstance,
insert,
Expand Down Expand Up @@ -98,6 +99,7 @@ const convert = {
html2md,
annotations2html,
note2html,
item2citation: parseCitationHTML,
};

const template = {
Expand Down
3 changes: 1 addition & 2 deletions src/modules/convert/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
jointPath,
randomString,
} from "../../utils/str";
import { parseCitationHTML } from "../../utils/citation";
import {
copyEmbeddedImagesInHTML,
importImageToNote,
Expand Down Expand Up @@ -1079,7 +1078,7 @@ async function processM2NRehypeCitationNodes(
const ids = dataCitation.citationItems.map((c: { uris: string[] }) =>
Zotero.URI.getURIItemID(c.uris[0]),
);
const html = await parseCitationHTML(ids);
const html = await addon.api.convert.item2citation(ids);
if (html) {
const newNode = note2rehype(html);
// root -> p -> span(cite, this is what we actually want)
Expand Down
2 changes: 1 addition & 1 deletion src/modules/template/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ async function runTemplate(
} catch (e) {
ztoolkit.log(e);
if (options.dryRun) {
return String(e);
return "Template Preview Error: " + String(e);
}
window.alert(`Template ${key} Error: ${e}`);
return "";
Expand Down
17 changes: 16 additions & 1 deletion src/utils/citation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
export async function parseCitationHTML(citationIds: number[]) {
export async function parseCitationHTML(
citationIds: number[],
args: {
locator?: string;
label?: string;
prefix?: string;
suffix?: string;
}[] = [],
) {
let html = "";
if (citationIds.length === 0 || !citationIds.every((id) => id)) {
return null;
Expand All @@ -14,6 +22,7 @@ export async function parseCitationHTML(citationIds: number[]) {
}
}

let i = 0;
for (const item of items) {
if (item.isRegularItem()) {
// @ts-ignore
Expand All @@ -22,6 +31,12 @@ export async function parseCitationHTML(citationIds: number[]) {
citationItems: [
{
uris: [Zotero.URI.getItemURI(item)],
...(args[i++] || {
locator: "",
label: "",
prefix: "",
suffix: "",
}),
itemData,
},
],
Expand Down

0 comments on commit b35d450

Please sign in to comment.