Dataviewjs script for creating markdown table

I’m wondering if it’s possible to create a Dataviewjs script that would be able to output a markdown table, so that the resulting markdown file could be run through Pandoc and styled easily in a word processor without a lot of CSS tweaking.

It looks like @Christian has a good approach in his script which puts a “copy” button at the top of a dataviewjs table which then copies the list of file names to the clipboard. It’s way beyond my scripting capabilities to customize the button part of that script to produce a markdown table (although I was able to script my own query to replace his example), but I wonder if it’s possible–this approach seems like it might be useful to others.

My use case here is creating a syllabus as a teacher. I have grouped my tables by “Weeks”, then sorted by “days” to end up with this:

Pretty good! But now the question is getting this into a form to share with students. I can export to PDF, but it’s not quite right and I’d rather tweak in a word processor than via CSS. I’m pretty familiar with using a docx reference file in Pandoc, so that’s my thinking right now, although I’d certainly be interested in other folks’ ideas along these lines.

4 Likes

My non-scripting workaround for what you’re describing:

  • Install Typora and set it as the “Default App” in Obsidian
  • Open the note in Obsidian and then choose “Open in Default App” from the ‘three dot menu.’
  • Note opens in Typora
  • Export note from Typora in whatever format you need. If you have pandoc installed and on your path, the export options include .doc, .rtf, and several others.
  • Open in your word processor, tweak the doc, and publish.

This is a bit more effort than running a script, but only marginally so. And the results are consistently good because, well, pandoc.

But typora won’t understand the dataview table, right?

Hey @derekvan. I took a look at it, and this is what I came up with.

I had to try to replicate your pages and table as much as possible. Maybe I did an OK job of it, but I am not sure.

I tried to make it at least kind of generic, such that others may benefit from it as well.

3 Likes

This looks really great! I can’t wait to tinker with it tomorrow. Very generous of you.

Ok @Christian , I had a chance to tinker already and it works! Thanks. However, one question. How can I change the query to look in a particular folder instead? I’d like to have something like this, but it’s throwing an error:

const pages = dv.pages('"teaching/65011 - Reading & Interpreting"');
instead of
const pages = dv.pages().where(f => f.theme && f.readings);

Good to hear. Sure thing.

Can you tell me which error is occurring?

Edit: const pages = dv.pages('"teaching/65011 - Teaching and Interpreting"').where(f => f.theme && f.readings); seems to work fine for me. Even without the where.

Thanks so much for your help with this.

This line produces a table:

const pages = dv.pages('"teaching/65011 - Reading & Interpreting"').where(f => f.theme && f.readings)

But this line gives an error:

const pages = dv.pages('"teaching/65011 - Reading & Interpreting"')

Error:

`Evaluation Error:
map@[native code]

map@[native code]

forEach@[native code]
eval code
eval@[native code]

generatorResume@[native code]

Promise@[native code]
__awaiter

generatorResume@[native code]

Promise@[native code]
__awaiter
capacitor://localhost/app.js:1:757698
capacitor://localhost/app.js:1:758120

generatorResume@[native code]
fulfilled
promiseReactionJob@[native code]`

Actually, this is probably my fault. There are files in that folder that don’t have proper YAML fields yet …

In the code, it’s assumed that every page in pages has a day, week, theme, and readings property. So if, like you mentioned, a page is incomplete in its properties, the code fails.

I used const pages = dv.pages('"teaching/65011 - Reading & Interpreting"').where(f => f.theme && f.readings) to make sure that every page found has both the theme and readings properties - and even that may fail in the case that those pages don’t have a day and week property.

This would be ‘more correct’.

const pages = dv.pages('"teaching/65011 - Reading & Interpreting"').where(f => f.theme && f.readings && f.day && f.week)

Hope this helps!

2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.