Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single item in YAML array not shown, interpreted as "Link" #283

Closed
Moonbase59 opened this issue Jun 12, 2021 · 5 comments
Closed

Single item in YAML array not shown, interpreted as "Link" #283

Moonbase59 opened this issue Jun 12, 2021 · 5 comments
Labels
Milestone

Comments

@Moonbase59
Copy link

@Moonbase59 Moonbase59 commented Jun 12, 2021

I’m trying to build a recipe list that uses an ingredients list in YAML.

For some odd reason, if I only have one item in there, Dataview interprets it as a "Link" (instead of an Array). I assume Dataview parses items in the YAML frontmatter as Markdown, which it shouldn’t. It would then look like an incomplete Markdown link ["sugar, white", 20 g] without the () part.

This fails:

ingredients:
  - ["sugar, white", 20 g]
  #- [watermelon, 286 g] # 1 wedge

This works:

ingredients:
  - ["sugar, white", 20 g]
  - [watermelon, 286 g] # 1 wedge

Here is the complete Obsidian note plus some screenshots:

---
recipe: Melon Salad

# Category - Use one of:
# appetizers, condiments, confectionary, desserts, dips, pastes,
# spreads, dried, dumplings, fast food, fermented foods,
# noodles, pies, salads, sandwiches, sauces, snacks, soups, stews
category: desserts

servingsize: 100 g # blank-separated, so "unit" can be split

# Adjust portions to automatically modify recipe
portions: 4

# use standard durations here
preparationtime: 15 min
cookingtime: 15 min
totaltime: 30 min

# Ingredients should be named like their note (for making links)
# Quantities are always per PORTION.
# Don’t use [[link]] syntax here, might break the YAML!
# Units: pce, g, kg, ml, l, tsp, tbsp, fl oz, cup, pt, qt, gal
ingredients:
  - ["sugar, white", 20 g]
  - [watermelon, 286 g] # 1 wedge

# Tags – Use "recipe", "food" or "drink", and the category
tags: [recipe, food, dessert]
---

# `=this.recipe`

## Ingredients for `=this.portions` portions (`=this.servingsize` ea.)

```dataviewjs
var i;
var list = [];
console.log(dv.current().ingredients);
for (i=0; i < dv.current().ingredients.length; i++) {
    var name = dv.current().ingredients[i][0];
    var amount = dv.current().ingredients[i][1].split(" ")[0];
    var unit = dv.current().ingredients[i][1].split(" ")[1];
    var link = '[[' + name + ']]'
    list.push(amount * dv.current().portions + " " + unit + " " + link);
}
dv.list(list);
```

## Nutritional values

… some dataviewjs to calculate totals from the ingredients, maybe combined GL, carbohydrates, Insulin Index, or the like …

## Preparation

Preparation: `=this.preparationtime` Cooking/baking: `=this.cookingtime` Total: **`=this.totaltime`**
<!-- entries separated by U+2003 emspace, U+00A0 nbsp between label & value -->

… preparation …

## Notes

… notes …

Knowledgebase - Obsidian v0 12 4_007

Knowledgebase - Obsidian v0 12 4_008

Knowledgebase - Obsidian v0 12 4_009

Knowledgebase - Obsidian v0 12 4_010

@blacksmithgu
Copy link
Owner

@blacksmithgu blacksmithgu commented Jun 12, 2021

Haha, you are stupendously good at breaking all of my heuristics. This is borne from a hack to parse links in frontmatter since they are parsed as double-nested lists in YAML. It was intended for parsing things like [[thing]], but using different list syntax also can create that same structure.

Perhaps I should disable link detection in frontmatter or at the very least change the detection (to use a special string construct or require that they are quoted).

@Moonbase59
Copy link
Author

@Moonbase59 Moonbase59 commented Jun 12, 2021

Haha, you are stupendously good at breaking all of my heuristics.

🤣 So sorry, hee hee. Not done intentionally!

But I can see where it comes from "set of sets", or "array in array". Wikilinks and YAML are a mega-pita. But of course everybody likes the nice suggest box … me too. Sigh. I fear, though, that wikilinks in YAML won’t survive long—just too much idiotic stuff for programmers to get at their content, and users will never understand why they should use parenthese, not use constructs like [[link1]], [[link2]] and so forth.

Actually much like your necessity-born '"folder"' construct, hee hee. Workarounds are never good—they’re hard to use, one might forget them when upstream versions change, they need to be adapted, and break much too often. Sigh.

Plus, it’s not easy making users understand why—in an otherwise elegant and practical app like Obsidian—sometimes things have to be so "odd", "quirky", "complicated" and "programmer-like". I feel with you.

Unfortunately, I also don’t have the great idea. As much as I’d love to use wikilinks in YAML, I understand that YAML is YAML, and simply don’t. YAML syntax can be problematic enough, in some special cases. Still, that’s what we have, and so its syntax must be adhered to, even if users don’t like it.

@blacksmithgu
Copy link
Owner

@blacksmithgu blacksmithgu commented Jun 12, 2021

It appears that it's still possible to use Obsidian's link autocomplete inside of quotes, like so:

thing: "[[Page]]"

So perhaps I will swap to that.

@Moonbase59
Copy link
Author

@Moonbase59 Moonbase59 commented Jun 13, 2021

Or in

thing1: [ [[link1]], [[link2]] ]

thing2:
  - [[link1]]
  - [[link2]]

which seems a little "cleaner" to me, because both just add another array around them, in the same way, so these could be used alternatively. (Personally, I think getting at array elements is a little easier than having to parse a string, programmatically. In dataviewjs, one could easily .map() these, too.)

See this example in the Online YAML Parser.

@M-bot M-bot added the bug label Jul 29, 2021
@blacksmithgu
Copy link
Owner

@blacksmithgu blacksmithgu commented Aug 22, 2021

As of 0.4.5, Dataview now uses the Obsidian suggested way of links in frontmatter, via quoting them:

key: "[[Link]]"

@blacksmithgu blacksmithgu added this to the 0.4.5 milestone Aug 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants