zotero-mdnotes icon indicating copy to clipboard operation
zotero-mdnotes copied to clipboard

Allow extraction of year alone from {{date}} for items that have day and month in addition to year

Open Cateroo opened this issue 3 years ago • 1 comments

This is a feature request; currently, articles, books, cases, etc that have a date+month+year are extracted as is from zotero into the markdown note without additional formatting. I tend to prefer tagging just the year for most of my references as more details clutter up connections in backlinking. This is particularly true for legal cases, where the year of judgement is usually relevant information for drawing up data on patterns of adjudication, but I still need to retain the decision month and date in zotero for citations.

It would be great if extraction of just the year, or something that can format the date into specific day/month/year tags could be built for this.

Cateroo avatar Mar 08 '21 21:03 Cateroo

I can't even figure out where the {{date}} or {{dateCreated}} placeholder preference is located. I can't find it in the Config Editor nor in %appdata%\Zotero\Zotero\Profiles\randomstrings.profilename\pref.js

I doubt it is just from the default zotero field name because {{date}} in the template translates to * Date: [[MMM DD, YYYY]]

Edit: my hackiest hack until someone does a better fix

In Zotero Config Editor

Add a new preference with name: extensions.mdnotes.placeholder.date and value: {"content":"{{field_contents}}", "field_contents": "{{content}}","link_style": "no-links"} This returns only the date, when the {{date}} is added into the template.

In your Mdnotes template

Add a field like Year: ((date}} which ends up with something like Year: Apr 13, 2012

A templater script

This script finds the line that contains the string "Year: " and changes the date after it with just the Year. Year: Apr 13, 2012 => Year: 2012

<%* 

/* declare some variables*/
var stringYear = "Year: ";
let curFile = this.app.workspace.getActiveFile();
const curContent = await this.app.vault.read(curFile); 
var curFileLines = curContent.split('\n');

/* function to find string */
function findYear(value, index, array) {  
 return value.includes(stringYear);  
}



/* find the line and index that has string */
var curFileLine = curFileLines.find(findYear);  
var i = curFileLines.indexOf(curFileLine)

if (curFileLine == undefined) {
	console.log("no year");
	} else if (curFileLine !== undefined) {
	// Replace {{date}} with year
	curFileLine = curFileLine.replace(stringYear,"")
	var date = new Date(curFileLine);
	var year = date.getFullYear();
	curFileLines[i] = stringYear + year
	curFileLines = curFileLines.join("\n");
	await app.vault.modify(curFile, curFileLines);
	}


%>

Conclusions

Is it convoluted? Yes Does it work? Yes Should you switch to the Citations Plugin that has a {{year}} placeholder built in? 🤷

Edit 2: my hackiest hack until someone does a better fix pt. 2

In Zotero Config Editor

See Above

In your Mdnotes template

In the location where you want the year tag, insert the below dynamic Templater code. (Also include the code blocks because the <%+* dynamic templater is currently broken but has this fix.

```
<%+*
var stringYear = "Year: ";
var date = new Date("{{Date}}"); //2013-04-27
var year = date.getFullYear();

let curFile = this.app.workspace.getActiveFile();
const curContent = await this.app.vault.read(curFile); 
var curFileLines = curContent.split('\n');

var i1 = curFileLines.indexOf("```")
var i2 = curFileLines.indexOf("<%+*")
var i3 = curFileLines.indexOf("```",(i1+1))

if (i2-i1 == 1){
curFileLines.splice(i1,i3-i1+1,stringYear+year)
curFileLines = curFileLines.join("\n");
await app.vault.modify(curFile, curFileLines);
}
%>
```

demo

gif

welpdx avatar Jun 19 '21 02:06 welpdx