Skip to content

Commit

Permalink
Support for firstNameInitials in formatting names
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanopagliari committed Jun 19, 2022
1 parent d82c2d7 commit daf36c6
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ All notable changes to this project will be documented in this file.
- Added the possibility of specifying a custom hex colour in the highlight setting and the transformation associated with this colour. Added a "{{CustomHex}}" placeholder in the template gathering all the highlight using that colour
- The backlink now directs to the specific annotation rather than the page in the pdf file (thanks to @CaoKang-David)
- It is possible to name the notes with the author (thanks to @ThomasFKJorna)
- Added {{firstNameInitials}} to the placeholders available to format the name of authors/editors. This is in addition to {{firstName}} and {{lastName}}

### Bugs
- Fixed bugs that prevented notes added in Zotero using markdown from being formatted properly


## [0.9.10]
### Improvement
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "bibnotes",
"name": "BibNotes Formatter",
"version": "0.9.149",
"version": "0.9.151",
"minAppVersion": "0.12.0",
"description": "Plugin to import and format annotations from Zotero.",
"author": "Stefano Pagliari",
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bibnotes",
"version": "0.9.149",
"version": "0.9.151",
"description": "Plugin to export and format annotations from Zotero into Obsidian",
"main": "main.js",
"scripts": {
Expand All @@ -26,6 +26,7 @@
"debugout.js": "^1.0.0",
"loglevel": "^1.8.0",
"node": "^17.2.0",
"tslint": "^6.1.3"
"tslint": "^6.1.3",
"turndown": "^7.1.1"
}
}
25 changes: 20 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import { Plugin, Notice, normalizePath } from "obsidian";

import path from "path";

import 'turndown'



// wasn't actually used
//import { BooleanLiteral, isTokenKind } from "typescript";

Expand Down Expand Up @@ -488,7 +492,14 @@ export default class MyPlugin extends Plugin {

parseAnnotationLinesintoElementsUserNote(note: string) {

note = note


// Replace html formatting with markdown formatting
const turndownService = new TurndownService()
note = turndownService.turndown(note)


note = note
// Replace backticks
.replace(/`/g, "'")
// Correct when zotero exports wrong key (e.g. Author, date, p. p. pagenum)
Expand All @@ -511,8 +522,11 @@ export default class MyPlugin extends Plugin {
// Replace backticks with single quote
selectedLine = replaceTemplate(selectedLine, "`", "'");
//selectedLine = replaceTemplate(selectedLine, "/<i/>", "");
// Correct encoding issues
selectedLine = replaceTemplate(selectedLine, "&amp;", "&");
// Correct encoding issues with special character showing incorrectly
selectedLine = replaceTemplate(selectedLine, "&amp;", "&").replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&amp;/g,'&');


console.log(selectedLine)

const lineElements: AnnotationElements = {
highlightText: "",
Expand Down Expand Up @@ -546,6 +560,7 @@ export default class MyPlugin extends Plugin {
return noteElements;
}
parseAnnotationLinesintoElementsZotero(note: string) {

// clean the entire annotation
note = note
// .replace(
Expand Down Expand Up @@ -579,6 +594,8 @@ export default class MyPlugin extends Plugin {
// // Correct encoding issues
selectedLine = replaceTemplate(selectedLine, "&amp;", "&");

console.log(selectedLine)

const lineElements: AnnotationElements = {
highlightText: "",
highlightColour: "",
Expand Down Expand Up @@ -839,7 +856,6 @@ export default class MyPlugin extends Plugin {
const HexRegex = new RegExp(/#([a-fA-F0-9]{6})/g);

if(HexRegex.test(lineElements.highlightColour)){
console.log(lineElements.highlightColour)
const colorClassifier = new ColorClassifier(Palette.RAINBOW, AlgorithmTypes.HSV);
lineElements.highlightColour = colorClassifier.classify(String(lineElements.highlightColour.match(HexRegex)), "hex");
}
Expand Down Expand Up @@ -868,7 +884,6 @@ export default class MyPlugin extends Plugin {
lineElements.highlightColour = "magenta";
}

console.log(lineElements.highlightColour)

}

Expand Down
2 changes: 1 addition & 1 deletion src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export class SettingTab extends PluginSettingTab {

new Setting(settingsExport)
.setName("Format Names")
.setDesc('Specify how the names of the authors/editors should be exported.')
.setDesc('Specify how the names of the authors/editors should be exported. Accepted values are {{firstName}}, {{lastName}} and {{firstNameInitials}}')
.addTextArea((text) =>
text
.setValue(settings.nameFormat)
Expand Down
11 changes: 11 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ export function formatCreatorsName(creator: Creator, nameCustom: string) {
) {
nameCustom = nameCustom.replace("{{lastName}}", creator.lastName);
nameCustom = nameCustom.replace("{{firstName}}", creator.firstName);
const getInitials = function (string:string) {
let names = string.split(' '),
initials = names[0].substring(0, 1).toUpperCase()+".";

if (names.length > 1) {
initials += names[names.length - 1].substring(0, 1).toUpperCase()+".";
}
return initials;
};

nameCustom = nameCustom.replace("{{firstNameInitials}}", getInitials(creator.firstName));
nameCustom = nameCustom.trim();
return nameCustom;
} else if (
Expand Down

0 comments on commit daf36c6

Please sign in to comment.