Skip to content

Instantly share code, notes, and snippets.

@gbrlb
Last active Mar 7, 2022
Embed
What would you like to do?
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';
let md = "\n"
// links doesn't update inside adnote
let mdv = "\n````ad-summary\ntitle:Worked Today\ncollapse: close\n";

const HeadRow = [ "Time", "Link"];
const TableFormat = "|:---:|:---|\n"

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

// Created Today TABLE
md += "### Created Today \n";
//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)
//create md table
md += create_mdtable(HeadRow, TableFormat, created_dv_rows)

// Modified Today TABLE
md += "### Modified Today \n";
//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 md table
md += create_mdtable(HeadRow, TableFormat, modified_dv_rows)
mdv += md
mdv += "\n````\n"
//show table
//dv.paragraph(md);

// 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;
}

dv.paragraph(copyButtonMaker());
dv.paragraph(md);
//dv.paragraph(mdv);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment