Skip to content

Instantly share code, notes, and snippets.

@gbrlb
Last active January 24, 2024 14:19
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbrlb/388f49d4dc5679b159b068509037efef to your computer and use it in GitHub Desktop.
Save gbrlb/388f49d4dc5679b159b068509037efef to your computer and use it in GitHub Desktop.
Daily Review - Worked Today : Obsidian dataview to md table + admonition + copy button

{{date}}

  • File name must be in yyyy-MM-dd format, for example 2022-02-23
  • Uncomment last line dv.paragraph(mdv); if you like to see the result inside a collapse ad-note
  • You could copy the table and delete the dataview to maintain the work today table, and the links inside the table will update, but the table will remain for future review.
  • To exclude a folder modifly dailynotes in:
const created_dv_rows = dv.pages('-"dailynotes"')
...
...
const created_dv_rows = dv.pages('-"dailynotes"')

Sources:

Worked Today

const format = dv.current().dateformat || 'yyyy-MM-dd';
const ftime = 'HH:mm:ss';

// define function to create tabe
function create_mdtable(HeadRow_dv, TableFormat, Rows_dv){
	let last_row = ""
	let table = `| ${HeadRow_dv.map(cell => cell + " |").join("")}`;
	table += `\n${TableFormat}`
	for (let Row of Rows_dv) {
		if (Row[0] != last_row) {
		table += `\n| ${Row[0]} | [[${Row[1]}]] |`;
	}
	last_row = Row[0];
	};
	return table
}

// Created TABLE
const HeadRow = [ "Time", "Link"];

// Created Today TABLE
// create dataview rows
const created_dv_rows = dv.pages('-"dailynotes"')
	.where(p => p.file.ctime.toFormat(format) == dv.current().file.name)
	.sort(k => k.file.ctime)
	.map(p => [p.file.ctime.toFormat(ftime), p.file.name]);
// dv.table(HeadRow, created_dv_rows)

// Modified Today TABLE
// create dataview rows
const modified_dv_rows = dv.pages('-"dailynotes"')
	.where(p => p.file.mtime.toFormat(format) == dv.current().file.name)
	.sort(k => k.file.mtime)
	.map(p => [p.file.mtime.toFormat(ftime), p.file.name]);
// dv.table(HeadRow, modified_dv_rows)

// create markdown tables
const TableFormat = "|:---:|:---|"

let body = ``;
let md = ``;
let mdv = ``;

if  (modified_dv_rows.length>0 || modified_dv_rows.length>0) {

	if (created_dv_rows.length>0) {  
	  body += `#### Created Today \n${create_mdtable(HeadRow, TableFormat, created_dv_rows)}\n`;
	}
	if (modified_dv_rows.length>0) {  
		body += `#### Modified Today \n${create_mdtable(HeadRow, TableFormat, modified_dv_rows)}\n`;
  }

	mdv = `\`\`\`\`ad-summary\ntitle: Worked Today\ncollapse: Open\n${body}\n\`\`\`\``;
	
	md = `\n\> [!Summary]+ Worked Today`;
	for (let line of body.split(/\r\n|\r|\n/) ){
		md += `\n\> ${line}`
	}
	md += `\n`
}

// copy button
const copyToClipboard = str => {
	const el = document.createElement('textarea');
	el.value = str;
	document.body.appendChild(el);
	el.select();
	document.execCommand('copy');
	document.body.removeChild(el);
};

const copyButtonMaker = () => {
	const btn = this.container.createEl('button', { "text": "Copy" });
	btn.addEventListener('click', async (evt) => {
		evt.preventDefault();
		copyToClipboard(md);
	});
	return btn;
}

// Display preview
dv.paragraph(copyButtonMaker());
dv.paragraph(mdv);
@cedricbdev
Copy link

Thank you!

I've searched how to format the ctime property for a long time. ctime.toFormat() helped me a lot.

Where / How did you find this? There is nothing in the Dataview plugin about functions accessible to properties...

@gbrlb
Copy link
Author

gbrlb commented Sep 27, 2022

Hi @cedricbdev, glad it helps, data view uses luxon DateTime for dates, in this example I use the luxon toFormat and Dataview dateformat function

@cedricbdev
Copy link

Thank you for the details. I didn't know Luxon.

So I've used toFormat to display how many notes I've created in a weekly note (not that far from the daily note).

`$= dv.pages().where(p => p.file.ctime.toFormat("WW") == dv.current().file.ctime.toFormat("WW")).length`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment