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

Citation popup: display locally stored references #4

Merged
merged 12 commits into from
Jun 17, 2021
Merged

Conversation

xUser5000
Copy link
Contributor

What has been done

  • Register addBibTeXReference Command and bind it to a toolbar button.
  • Abstract string constants used in the plugin into a separate file constants.ts.
  • Parse the bibtex file and store results upon executing the command.
  • Enable the citation popup to show the list of references.

Screenshots

Image of the toolbar button
Toolbar button

Image of the citation popup
Citation Popup


function isFileExisting (file: string): boolean {
try {
fs.accessSync(file, fs.constants.F_OK);
Copy link
Member

Choose a reason for hiding this comment

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

You should always use the fs async functions, otherwise you freeze the process while the function runs. It might be fine for a few calls here and there but to be future proof it's always best to use async functions.

With promises, the code isn't even more complex - for example here it would be await fs.access().

Comment on lines 23 to 30
if (!isFileExisting(filePath)) {
await showMessageBox(constants.ERROR_FILE_NOT_FOUND);
return;
}
if (!isFileReadable(filePath)) {
await showMessageBox(constants.ERROR_PERMISSION_DENIED);
return;
}
Copy link
Member

Choose a reason for hiding this comment

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

you don't need any of these checks. Just move the readFile call in the try/catch block (you should do that anyway), and if there's any error you'll catch it there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought this might be useful for showing descriptive error messages for the user. Am I missing something?

Copy link
Member

Choose a reason for hiding this comment

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

You don't need that level of detail for something that's very unlikely to happen. But even then you can always give a descriptive error message in the catch block - Could not open file: ${filePath}: ${error.message}

const ans: string = (
`<ul>` +
refs
.map(ref => `<li>${ref.title}</li>`)
Copy link
Member

Choose a reason for hiding this comment

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

You need to escape the title.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, but I don't fully understand what that means. Do you mean that I need to print another attribute of the reference instead of the title ?

Copy link
Member

Choose a reason for hiding this comment

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

If you add text into HTML, you need to escape it. Otherwise, for example, if the title contains an HTML tag, you'll end up with broken HTML. I usually use this package for this: https://www.npmjs.com/package/html-entities

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I understand now.

label: 'BibTeX File',
}
});
const options = {};
Copy link
Member

Choose a reason for hiding this comment

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

const options:Record<string, SettingItem>

@@ -0,0 +1,16 @@
import joplin from 'api';
import { ToolbarButtonLocation } from 'api/types';
import constants from '../constants';
Copy link
Member

Choose a reason for hiding this comment

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

I don't think it's a good pattern to import all the constants in all the files. If you import only what you need, it will make the code more readable and scalable. import { PLUGIN_TOOLBAR_BUTTON_ID } from '../constants'

@xUser5000
Copy link
Contributor Author

DONE

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.

None yet

2 participants