Retrieve annotations for Hypothes.is via Templater plugin (Hypothes.idian)

Thanks for the help getting my locally annotated files into Obsidian!

I ran against an issue when the Hypothes.is didn’t store the file name. By replacing the apiAnnotationSimplify method with this more error handling variant I was able to also get the highlights of files without titles.

const apiAnnotationSimplify = async (results) => {
  return results.rows.map(e => {
    const r = {
      title: e.document?.title?.[0] || '', // Handle potential undefined title
      uri: e.uri || '', // Handle potential undefined URI
      context: e.links?.incontext || '', // Handle potential undefined incontext
      text: e.text || '', // Handle potential undefined text
      highlight: '',
      tags: e.tags || [], // Handle potential undefined tags
      user: e.user || '', // Handle potential undefined user
      group: e.group || '', // Handle potential undefined group
      created: e.created || '', // Handle potential undefined created
      updated: e.updated || '', // Handle potential undefined updated
    };

    try {
      if (e.target && e.target[0] && e.target[0].selector) {
        const txt = e.target[0].selector.filter(e => e.type === 'TextQuoteSelector');
        if (txt && txt.length > 0) { // Check if txt is not empty
          r.highlight = txt[0].exact || ''; // Handle potential undefined exact
        }
      }
    } catch (error) {
      console.error('Error processing annotation:', error); // Log the error for debugging
    }

    return r;
  });
};