Skip to content

Render a full list of references at the bottom of the note viewer#16

Merged
laurent22 merged 12 commits intomainfrom
content-script
Jul 10, 2021
Merged

Render a full list of references at the bottom of the note viewer#16
laurent22 merged 12 commits intomainfrom
content-script

Conversation

@xUser5000
Copy link
Copy Markdown
Contributor

@xUser5000 xUser5000 commented Jul 9, 2021

What has been done

  • Register a markdown-it content script to render references at the bottom of the note viewer in APA format.
  • Render both inline and block-level references.
  • Parse and store references on startup
  • Prevent rendering duplicate references
  • Use the citekey as the display key of references. (Previously I used a combination of author + year of publication).

Copy link
Copy Markdown
Member

@laurent22 laurent22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks good overall. Your approach to create the reference list token and parse it in particular seems solid. I've just left a few comments.

By the way, could you create a documentation for the format you support? In particular what is this APA thing and what citation formats your plugin supports or creates. For example you have both "Steinbeck2003" and "bu-EtAl:2010:EMNLP". I suppose there's a reason for that, which should be explained in the Readme.

Comment thread src/init.ts Outdated
const refs: Reference[] = parse(fileContent);
DataStore.setReferences(refs);

} catch (e) { }
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I seem to remember you were previously displaying an error with the line number when the file could not be parsed. Was that removed? In general I think the user should be informed if their file can't be loaded. Can you use alert() here for example? Or joplin.views.dialogs.showMessage?

Comment thread src/ui/bibliography-renderer/index.ts Outdated
*/
await joplin.contentScripts.onMessage(REFERENCE_LIST_CONTENT_SCRIPT_ID, (IDs: string[]) => {
let refs: string[] = [];
IDs = [...new Set(IDs)];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're often converting arrays to set and the other way around. Is there any reason for that? Isn't this code pretty much the same as IDS = IDS?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this code pretty much the same as IDS = IDS?

Not exactly. Previously, if there are multiple occurrences of the same reference, it will be rendered twice at the bottom.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I see. It's not very obvious that it's doing this so maybe add a comment?

Comment thread src/ui/bibliography-renderer/render-list-content-script.ts Outdated
return `
<h1 id="references_title" style="display:none">References</h1>
<ul id="references_list"></ul>
<style onload='${script.replace(/\n/g, ' ')}'/>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason you used a style tag for this? Wouldn't it be possible to wrap your script string simply in a <script> tag? I think that would be more proper and perhaps more reliable too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be possible to wrap your script string simply in a <script> tag? I think that would be more proper and perhaps more reliable too.

I tried that approach as it seems to be the most natural but for, some reason, it does not seem to work.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I think this is fragile code that may break at some point or even work in some operating systems but not others. I think it didn't work with the script tag because you need to wait till the "webviewApi" is available. For this you can use the same pattern - just keep checking typeof webviewApi until it becomes available, then run your initialisation code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whatever I put in the script tag does not get executed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if you're looking at all this the wrong way around. The plugin sets the HTML, and has all the references, so instead of adding a script that post messages, can't you directly set the HTML to the correct value from the plugin? Then you won't need a script at all inside the content script.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the documentation, there is no description of such approach (in markdown-it plugin section)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not something about documentation, since it's specific to your plugin. But come to think of it, what I'm suggesting wouldn't work since the reference data is in the plugin process, and the content script in the main process, so there would still be a need for some kind of ipc call. So let's leave it as it is for now.

@xUser5000
Copy link
Copy Markdown
Contributor Author

xUser5000 commented Jul 9, 2021

Renamed _context to context and made it show a message box on startup if there is an error parsing the BibTeX file.

@xUser5000
Copy link
Copy Markdown
Contributor Author

By the way, could you create a documentation for the format you support? In particular what is this APA thing and what citation formats your plugin supports or creates. For example you have both "Steinbeck2003" and "bu-EtAl:2010:EMNLP". I suppose there's a reason for that, which should be explained in the Readme.

Yeah sure, before releasing the new version, I'm gonna do that in addition to updating the main GIF that shows the working mechanism of the plugin.

const referenceListView = document.getElementById("references_list");
const referenceTitleView = document.getElementById("references_title");
if (refs.length > 0) referenceTitleView.style.display = "block";
refs.forEach(ref => referenceListView.innerHTML += ref);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of setting innerHTML multiple times, which is slow, you could build up the HTML string in the loop, then set the inner HTML once at the end.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, I keep doing these silly mistakes 😃

@xUser5000
Copy link
Copy Markdown
Contributor Author

DONE

@laurent22 laurent22 merged commit 5f49ed3 into main Jul 10, 2021
@xUser5000 xUser5000 mentioned this pull request Jul 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants