Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

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

Closed
Cateroo opened this issue Mar 8, 2021 · 1 comment
Labels
enhancement New feature or request

Comments

@Cateroo
Copy link

Cateroo commented Mar 8, 2021

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 Cateroo added the enhancement New feature or request label Mar 8, 2021
@welpdx
Copy link

welpdx commented Jun 19, 2021

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants