hans / obsidian-citation-plugin

Obsidian plugin which integrates your academic reference manager with the Obsidian editor. Search your references from within Obsidian and automatically create and reference literature notes for papers and books.
MIT License
1.12k stars 83 forks source link

Preserve Zotero link in Note field #125

Open TomBener opened 3 years ago

TomBener commented 3 years ago

Thanks for the great plugin.

PDF annotations extracted by Zotfile has the URL, which links to the specific page:

These work environments are often characterised by significant trade-offs in terms of relationship quality, productivity and well-being ([Morrison 2020:366](zotero://open-pdf/library/items/RTDDFWMC?page=1) )

In the .bib file, which looks similar, but with LaTeX format:

These work environments are often characterised by significant trade-offs in terms of relationship quality, productivity and well-being (\href{zotero://open-pdf/library/items/RTDDFWMC?page=1}{Morrison 2020:366})

However, after importing into Obsidian, the plugin replaces the hypertext as the unified Link:

These work environments are often characterised by significant trade-offs in terms of relationship quality, productivity and well-being ([Link](zotero://open-pdf/library/items/RTDDFWMC?page=1) )

I do hope to use Morrison 2020:366 instead of Link. Please consider this feature. Thank you 🙏️

eatse21 commented 2 years ago

Hi,

I ran into the same issue, and it seems that the problem is related to the bibtex parser library used by the plugin, which only returns the zotero link, and not the associated text.

The issue is here (noted in comments !) :

// just take the URL? Not the label?
case 'href':
  if (arg = this.argument(node, 2)) return this.clean(arg[0] as Node)
  break

A definitive fix would require that this library modify the parser’s logic, which currently doesn’t handle links :

    const markup_defaults: Record<string, { open: string, close: string}> = {
      enquote: { open: '\u201c', close: '\u201d' },
      sub: { open: '<sub>', close: '</sub>' },
      sup: { open: '<sup>', close: '</sup>' },
      bold: { open: '<b>', close: '</b>' },
      italics: { open: '<i>', close: '</i>' },
      smallCaps: { open: '<span style="font-variant:small-caps;">', close: '</span>' },
      caseProtect: { open: '<span class="nocase">', close: '</span>' },
      roman: { open: '', close: '' },
      fixedWidth: { open: '', close: '' },
    }

(no <a href="">…</a> kind of stuff), see also here for the conversion logic.

But you can make a quick workaround (it’s working for me) by modifying this plugin’s code in your vault folder : (.obsidian/plugins/obsidian-citation-plugin/main.js).

  1. First, search in the file case 'href': to find this code:

    // just take the URL? Not the label?
    case 'href':
    if (arg = this.argument(node, 2))
    return this.clean(arg[0]);
    break;

    and replace with

    // just take the URL? Not the label?
    case 'href':
    // Workaround for getting a markdown link
    const block = this.argument(node, 2);
    if (block) {
    // block: [ {value: [{ value: "zotero://…"}]}, {…} ]
    const link = block[0].value[0].value;
    
    // block: [ {…}, { value: [ {value: "note on page…"} ]} ]
    const text = block[1] ? block[1].value[0].value : "Link";
    block[0].value[0].value = `[${text}](${link})`;
    }
    if (arg = this.argument(node, 2))
    return this.clean(arg[0]);
    break;

    Be aware that for some reason, the plugin’s code is duplicate in the js file, so make the modifications twice (for me it was at line 48939 and line 84530)

  2. Search [Link] to find this code:
    get: function () {
    var _a;
    return (_a = this._note) === null || _a === void 0 ? void 0 : _a.map(function (el) { return el.replace(/(zotero:\/\/.+)/g, '[Link]($1)'); }).join('\n\n');
    },

    Replace the part

    _a.map(function (el) { return el.replace(/(zotero:\/\/.+)/g, '[Link]($1)'); }).join('\n\n');

    with

    _a.map(function (el) { return el; }).join('\n\n');

    (this part is also duplicate, make sure to replace this in the two locations).

Note: thoses changes will be erased when updating the plugin, but it’s a simple workaround to do until the problem is fixed. Feel free to open an issue on the bibtex-parser repo also

TomBener commented 2 years ago

@abdullahibnnadjo Great! It works as expected. Huge thanks for your workaround!

retorquere commented 2 years ago

The parser has been updated to 6.0.1 to convert links to <a> markup.

Be aware that for some reason, the plugin’s code is duplicate in the js file, so make the modifications twice (for me it was at line 48939 and line 84530)

In what sense is it duplicate? And there isn't a source file in the parser that has more than 4527 lines of code; you're probably looking at something that bundles the parser into a larger javascript source.

TomBener commented 2 years ago

Hi, @retorquere, the duplicated lines are in main.js of obsidian-citation-plugin. It is available at https://github.com/hans/obsidian-citation-plugin/releases/download/0.4.4/main.js

eatse21 commented 2 years ago

The parser has been updated to 6.0.1 to convert links to <a> markup.

Be aware that for some reason, the plugin’s code is duplicate in the js file, so make the modifications twice (for me it was at line 48939 and line 84530)

In what sense is it duplicate? And there isn't a source file in the parser that has more than 4527 lines of code; you're probably looking at something that bundles the parser into a larger javascript source.

Yes, I was refering to the plugin's bundle to apply the workaround, in which the code is duplicate. Your code is completely fine đź‘Ť

evolve2k commented 2 years ago

@TomBener @abdullahibnnadjo with the release of the update to bibtex parser in v6.0.0 is this now resolved? If not could you maybe add more here: https://github.com/retorquere/bibtex-parser/issues/19

eatse21 commented 2 years ago

@evolve2k yes I made a PR resolving this issue with bibtex parser v6, but no news from @hans since then