Can [[wikilink]] in YAML ever be "done right"? NO. But there could be a "route of least pain". #137
Replies: 3 comments 6 replies
-
This is a great writeup. I haven't had much time to sit down and look at this plugin for a few days (got busy :) ), but I appreciate the significant amount of effort that went into this. As far as the YAML parser, I am using Obsidian's exposed For Dataview, just took a look through the code: I have no experience with parsimmon (which is what he is using to parse the Query language), but the wikilinks are pulled out here: And the parsing is defined here: Possibly what I could do here is pull out any parameter in the code block that matches a wikilink RegExp, add it to an internal map and replace the wikilink in the source with the YAML-safe index from the map, parse the YAML, then re-add the wikilink into my parameters object. |
Beta Was this translation helpful? Give feedback.
-
FYI, I just pushed 3.19.0 which implements this YAML pre-parsing. |
Beta Was this translation helpful? Give feedback.
-
I’d say @valentine195’s proposed way of YAML preparsing in 3.19.1+ solves this problem most user-friendly, i.e. Obsidian’s auto-suggest can be used, and most "odd" file names don’t present a problem anymore. I still recommend keeping this somewhere for reference regarding what might be illegal YAML. |
Beta Was this translation helpful? Give feedback.
-
Here are some thoughts I wrote up, detailing the problems of using Obsidian "wikilinks" (
[[link]]
style) in YAML, and a proposed "best route to go". Since Obsidian Leaflet’s code block is also in YAML format (and parsed as such), this is becoming more and more interesting. And error-prone.The original file is from my notes, uses the Dataview and Templater plugins to show some points, and will not work on GitHub, so please just download it and extract into your own Obsidian vault.
Test YAML, with 'single' and "double" quotes.md.zip
I’ll nevertheless copy-paste the text here, so everyone can follow, more or less:
Test YAML, with 'single' and "double" quotes
This is a perfectly legal filename in Obsidian and can be [[Test YAML, with 'single' and "double" quotes#Test YAML with 'single' and double quotes|linked to its own H1]].
How can we safely represent this in YAML?
To be clear, the Obsidian "wikilink" looks like this:
We can see the Obsidian link suggester removes the comma
,
and double quotes"
for its header link, but leaves the single quotes'
in.Using the complete link in YAML
shows us that Obsidian’s syntax highlighter is broken (it shows a YAML comment where there is none), and gives us:
Interestingly, this seems to be "self-quoting" in some way, and it doesn’t break at the hash
#
because there is no space before it. (A YAML comment starts with<space>#
.)The "self-quoting" probably comes from the YAML parser seeing a plain
!!str
scalar within a!!seq
within another!!seq
(double brackets[[
). Plain scalars can use \ backslashes, " quotes ' and $ a % lot /&?+ of other {} [] stuff without the need of quoting.See also [[#Characters that cannot be used at the beginning of a plain scalar]], [[#Character sequences that can't be used inside a scalar]], Plain Scalars (yaml.info).
We can see the YAML parser returns a sequence
!!seq
of two strings!!str
, namelyand
Dataview
Dataview output: ==
=this.file1
==Dataview creates a working link. We don’t know how it parses YAML data, probably using its own parser, or parser exceptions.
Templater
Templater output: ==<%+ tp.frontmatter.file1 %>==
We can see Templater blindly concatenates all elements of the
!!seq
, using a comma,
for joining (watch closely: the blank after "Test," is missing). Assuming is not a good strategy in programming.Obsidian Leaflet
The Obsidian Leaflet plugin uses a YAML parser to parse the code block. (Which?)
Some of the wikilinks work, some don’t, some variables look like wikilinks but aren’t.
markerFile: [[Elvenking’s Halls]]
worksgeojson: [[Columbus, OH]]
doesn’t workgeojson: [["Columbus, OH"]]
works (but breaks auto-suggest)geojson: "[[Columbus, OH]]"
doesn’t workoverlay: [[red, [705.88, 941.175], 100 miles, "Test Overlay"]]
is no wikilink, but looks like one (and works)No good solution seems to be at hand
Still, for Obsidian Leaflet, …
Most compatible solution for wikilinks I can think of
[[link]]
format.|name
part.!!str
scalars inside the brackets, since these allow the most characters without escaping.:
in file names, so we don’t have the colon problem.,<space>
joiner. (Most people will use a blank after a comma.) Tell users in the docs.[[…]]
sequence).[[link]]
),[[link]]
on-
lines, or[ [[link1]], [[link2]] ]
notation),,<space>
joiner,|name
part of an Obsidian link for further use (layer names),Most compatible solution for tags I can think of
In order to not break YAML by accidentally using the hash
#
as a comment introducer, I think using tags in the non-hash notation, like Obsidian’s YAMLtags:
might be best. So we could have:or
The user must still know [[#List of things not allowed for YAML scalars|what’s not allowed]] in YAML scalars.
Caveat: Obsidian’s tag suggester can’t be used. To be precise, it can be used but the hashes
#
have to be removed manually.List of things not allowed for YAML scalars
Other good sources:
Characters that cannot be used at the beginning of a plain scalar
!
Tag like!!null
&
Anchor like&mapping_for_later_use
*
Alias like*mapping_for_later_use
-<space>
Block sequence entry:<space>
Block mapping entry?<space>
Explicit mapping key{
,}
,[
,]
Flow mapping or sequence,
Flow Collection entry separator#
Comment|
,>
Block Scalar@
, ` (backtick) Reserved characters"
,'
Double and single quote<whitespace>
%
DirectiveCharacter sequences that can't be used inside a scalar
As you can see, a comma or a square bracket will end a plain scalar. Therefore, to avoid confusion, the following characters or character sequences are not allowed in plain scalars:
:<space>
Key/value seperator. A colon without following whitespace is allowed.<space>#
This starts a comment.[
,]
{
,}
,
(possible exception: assumed auto-concatenation using,<space>
joiner):[
:]
:{
:}
:,
Special types you shouldn’t use in wikilinks or tags
Another use case for quotes is when you have a string that would be resolved as a special type. This highly depends on the YAML version and on the Schema in use. Here are some examples where you need quotes:
true
,false
Boolean values23
Integer numbers1e3
Numbers with exponent3.14159
Float numbersnull
Beta Was this translation helpful? Give feedback.
All reactions