Help with eachLink

Using eachLink() is there a way to get the name of linked notes? I can get the path with this, but not the just the name.

eachLink(aLink){
  vTermLinked+=aLink["source"]+"\n";
};

I get this:

/Resources Folder/Terms Folder/Trm- Test
/Resources Folder/Terms Folder/Trm- Test1
/Resources Folder/Terms Folder/Trm- Test2

What I want is this:

Trm- Test
Trm- Test1
Trm- Test2

There will hundreds of links, so performance is a consideration.

eachLink gives you the path, and you can always get the name through $Name(thePath).

This gives you the desired result

var:string vTermLinked;
eachLink(aLink){
  vTermLinked+=$Name(aLink["source"])+"\n";
};
$Text = vTermLinked;

If you wanted to use $IDString to do the $Name offset reference, you could do this:

var:string vTermLinked;
var:string vPathID;
eachLink(aLink){
  vPathID = $IDString(aLink["source"]);
  vTermLinked+=$Name(vPathID)+"\n";
};
$Text = vTermLinked;

On more example. On offset reference like $Name(nextSibling(aLink["source"])) will not work as the inner reference to the eachLink() loop variable is not resolved. But, if you needed to collect the next sibling of the link source:

var:string vTermLinked;
eachLink(aLink){
  var:string vPath = aLink["source"];
  vTermLinked+=$Name(nextSibling(vPath))+"\n";
};
$Text = vTermLinked;

So, you first resolve the link’s data to a string, store it in a variable and use the latter as an offset reference for the nextSibling designator. Obvious once you figure it out, but a silent fail if you haven’t.

I thought this worth noting as, reviewing my aTbref notes, it does (correctly!) say that designators allow offset references—except using find()—so without knowing better you might reasonably assume a loop variable hiding a title/path/ID would be valid usage. At present it isn’t, though the example above shows a simple workaround. Don’t worry about using an extra line of code - it won’t slow anything down and once written you won’t need to look at it again.

I believe you’d be better off with

eachLink(aLink){
  var:string vNote = aLink["sourceIDString"];
  vTermLinked+=$Name(nextSibling(vNote))+"\n";
};

My rule of thumb is to use an IDString in contexts where I anticipate multiple notes with the same name, and Path otherwise.

Yes. However, the sourceIDString link key is only available to Backstage users (and in the next public release). Thus my code above.

But also, Yes!

1 Like