unxok / dataedit

Dataview tables but editable!
MIT License
74 stars 2 forks source link

It Seems Like It Choked: Uncaught (in promise) Syntax Errors #4

Open LynetteCullens opened 6 months ago

LynetteCullens commented 6 months ago

From Dataview Type Query

TABLE mathLink-blocks.AltNotation, mathLink-blocks.Dimensions, mathLink-blocks.LinkedDimensions, mathLink-blocks.StaticDimensions, mathLink-blocks.LinkedStaticDimensions, mathLink-blocks.MKS, mathLink-blocks.CGS,mathLink-blocks.FPS, mathLink-blocks.LinkedFormula
FROM "Glossary"
LIMIT 50

obsidian.md-1714877932099.log

From Dataviewjs Type Query

const {fieldModifier: f} = this.app.plugins.plugins["metadata-menu"].api; 
dv.table (["File", "mathLink", "AltNotation", "Dimensions", "LinkedDimensions", "StaticDimensions", "LinkedStaticDimensions", "MKS", "CGS", "FPS", "LinkedFormula"], 
    await Promise.all(dv.pages('"Glossary"').map(async p => [ 
        p.file.link,
        await f(dv, p, "mathLink"),
        await f(dv, p, "mathLink-blocks.AltNotation"), 
        await f(dv, p, "mathLink-blocks.Dimensions"),
        await f(dv, p, "mathLink-blocks.LinkedDimensions"), 
        await f(dv, p, "mathLink-blocks.StaticDimensions"),
        await f(dv, p, "mathLink-blocks.LinkedStaticDimensions"),
        await f(dv, p, "mathLink-blocks.MKS"),
        await f(dv, p, "mathLink-blocks.CGS"),
        await f(dv, p, "mathLink-blocks.FPS"),
        await f(dv, p, "mathLink-blocks.LinkedFormula"), 
        ]) 
)) 

obsidian.md-1714878245785.log

LynetteCullens commented 6 months ago
const pageNum = dv.current().page;
const start = 10 * (pageNum - 1);
const end = 10 * pageNum;
const results = dv.pages('"Glossary"');
const regex = />\s\[\!theorem\]\s(.+?)((\n>\s.*?)*)\n/;

const rows = []
for (const page of results) {
    const file = app.vault.getAbstractFileByPath(page.file.path)
    // Read the file contents
    const contents = app.vault.read(file)
    // Extract the summary via regex
    for (const callout of contents.match(new RegExp(regex, 'sg')) || []) {
        const match = callout.match(new RegExp(regex, 's')) 
        rows.push([match[1], match[2], page.file.link])
    }
};
const rows2 = rows.slice(start, end)
const totalPages = Math.ceil(rows.length / 10);
const options = rows.slice(0, totalPages).map((_, i) => `option(${i + 1})`).join(',');
const selector = `\`INPUT[inlineSelect(${options}):page]\``;
dv.span(`Page ${selector} out of *${totalPages}*`);
const cols = ['Title', 'Content', 'Page Link'];
dv.table(cols, rows2)

obsidian.md-1715178564263.log

// You can update this to filter as you like - filtering for just your daily notes would be good
const pages = dv.pages('"Glossary"')

// This regex will find the contents of a specifically formatted callout
const regex = />\s\[\!theorem\]\s(.+?)((\n>\s.*?)*)\n/

const rows = []
for (const page of pages) {
    const file = app.vault.getAbstractFileByPath(page.file.path)
    // Read the file contents
    const contents = app.vault.read(file)
    // Extract the summary via regex
    for (const callout of contents.match(new RegExp(regex, 'sg')) || []) {
        const match = callout.match(new RegExp(regex, 's')) 
        rows.push([match[1], match[2], page.file.link])
    }
}

dv.table(['Title', 'Content', 'Page Link'], rows)

obsidian.md-1715178722797.log

unxok commented 6 months ago

So neither the dataview query nor dataviewjs worked right? (the dataview query looks like it should work)

For the dataviewjs, you don't do dv.table(). You have to instead return an object with properties headers and values

const data = dv.pages().map(f => [f.file.link, f.foo]);
return {
  headers: ['file', 'foo'],
  values: data
}
unxok commented 5 months ago

Something interesting-- The same way you make a metabind plugin codeblock, you can create a dataedit codeblock from within a dataview block like this:

dv.span("```dataedit\n" + 
"TABLE test, date, bool\n" +
"```"
)

Which would allow you to render other things (like metabind codeblocks or whatever you want) within the block while being able to utilize Dataedit's functionality.

The only meh thing about this approach is that after making an edit in the Dataedit table (within a dataview codeblock), editable table will still update quickly and without any flicker, but shortly after Dataview will flicker due to metadata changing (which is a known issue with Dataview and one of the reasons for Datacore being written with React).