Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Item]This template is to collect annotations and notes grouped only by tags #191

Closed
brunollave opened this issue Oct 22, 2022 · 3 comments
Labels
help wanted Extra attention is needed

Comments

@brunollave
Copy link

Thank you very much, in advance.

Template Type

Item

Desired Template Performance

This template is to collect annotations and notes grouped only by tags. It would be ideal ig we can make it works without an input at the begining, instead of that the script should take all the tags of the selected files.

Current Template

// @beforeloop-begin
${(()=>{
sharedObj.tagRaw = prompt("Please input tags. Split with '',:", "");
return "";
})()}
// @beforeloop-end
// @default-begin
${await new Promise(async (r) => {
  async function getAnnotation(item) {
    try {
      if (!item || !item.isAnnotation()) {
        return null;
      }
      let json = await Zotero.Annotations.toJSON(item);
      json.id = item.key;
      delete json.key;
      for (let key in json) {
        json[key] = json[key] || "";
      }
      json.tags = json.tags || [];
      return json;
    } catch (e) {
      Zotero.logError(e);
      return null;
    }
  }

  async function getAnnotationsByTag(_item, tag) {
    let annots = _item.getAnnotations();
    annots = tag.length? 
      annots.filter((_annot) => _annot.getTags().map(_t=>_t.tag).includes(tag)) :
      annots;
    let annotations = [];
    for (let annot of annots) {
      const annotJson = await getAnnotation(annot);
      annotJson.attachmentItemID = _item.id;
      annotations.push(annotJson);
    }
    const editor =
      await Zotero.Knowledge4Zotero.knowledge.getWorkspaceEditorInstance();
      await editor.importImages(annotations);
      return Zotero.EditorInstanceUtilities.serializeAnnotations(annotations);
    }
  const attachments = Zotero.Items.get(topItem.getAttachments()).filter((i) =>
    i.isPDFAttachment()
  );
  let res = "";
  if(!sharedObj.tagRaw){
    return;
  }
 for (let attachment of attachments) {
    for(tag of sharedObj.tagRaw.split(",").filter(t=>t.length)){
      res += `<h1>Tag: ${tag}</h1>`;
      const tags = (await getAnnotationsByTag(attachment, tag)).html
      res += tags ? tags : "<p>No result</p>";
  }
  }
  r(res);
})}
// @default-end
@brunollave brunollave added the help wanted Extra attention is needed label Oct 22, 2022
@windingwind
Copy link
Owner

Should the collected annotations and notes with the same tag be grouped by their parent items? Maybe an example of what the innput and result should be would be helpful.

@brunollave
Copy link
Author

Hello @windingwind thanks for your message.

Yes, the idea is to group annotations and notes just by tags. Identify all the annotations and notes of the selected files, then group the data by tags and then use each tag as header line and the annotations and notes as part of the paragraph in order to obtain a similar Structure like this:

TAG1:
Content1A (Author 1b...)
Content1A (Author 1c...)
Content1A (Author 1d...)

TAG2:
Content1A (Author 2b...)
Content1A (Author 2c...)
Content1A (Author 2d...)

Thanks for the support.

@windingwind
Copy link
Owner

@brunollave

#85 (comment)

See the last template: [Item] all annotations and notes by tag

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants