Create valid html links from list attribute

I searched around and tried ChatGPT, but I couldn’t find a solution. I tried using the $list.each() function, but couldn’t figure it out. I am just looking for a way to create valid links from the ‘Tags’ attribute that I have assigned to a note.

Input

$Tags = [Tag1, Tags, Tag3]

Output

Tag1
Tag2
Tag3

Thanks a lot!

Not 100% clear from the question, but I assume you want to make internal (in-TBX-doc) links in the $Text of a note. Additionally, I assume you want to link to a note whose $Name is the list item, and to use the same text as the anchor text at the source of the link. Apologies for asking, but this isn’t the only reading of the task above.

Late here so no time for a detailed example. But, with a list, you can iterate it using list.each(loopVar){actions}. Then, in-loop, use createLink(sourceItem, destinationItem[, linkTypeStr]) to make the links.

1 Like

I am looking for a way to loop through the list attribute and create a valid HTML link for each element in the list attribute.

Pseudocode

Pseudocode as example using this:
$Tags.format(“<a href=”./s=“+this+”>“+this+”<a>")

Result

The result should produce:
<a href=“./s=tag1>tag1</a>
<a href=”./s=tag1>tag2</a>
<a href="./s=tag1>tag3</a>

Thanks for your reply, Marc. Just to clarify, here is another example of what I am doing right now. As I don’t know how many Tags I might add in future, I would like to make this line of code more generic.

Loop through each tag in Tags Attribute, This works fine!
^if($Tags.count()>0)^^value($Tags[0])^^endif^
^if($Tags.count()>1)^^value($Tags[1])^^endif^

I am doing something similar for another purpose, which works pretty well. I collect all the children and create a loop through the ‘MySet’ attribute of each note, generating a valid HTML link to the note.

^action($MyList(parent)=collect_if(children(/Draft/),$MySet(this).icontains($Name(that)),“— ”+$Name+“”+" #“+$MyNumber).format(”
"))^
^value($MyList(parent).each(scenes){ scenes })^

In the first example, the ‘tags’ attribute is only for one note, but the code from the second example doesn’t work here right now.

Export code tends to assume you are exporting links already existing in the Tinderbox document, which here is obviously not the case. It’s no problem to build the list in action code in the template (inside an ^action( action )^ wrapper) but that alone is not enough.

Yes, you have a list of values in $Tags but your HTML links need, for their target) either a full URL or—more likely in this scenario—relative URL to the exported page of another note in the same TBx. Also, those target locations need to be unique paths within the the document.

So the next pieces of the jigsaw are:

  • where are the ‘tag’ notes stored
  • do they export
  • is the exported filename the same case as the tag.

Tip: if unsure, check and don’t guess. Likely all the answer are simple, but don’t assume.

Lets, assume the answers are:

  • at path `/Tags’
  • yes
  • the case is the same as the tag value. IOW, “pear” for both not “pear” vs. “Pear”.

Next, some code…

On the hop just now, so I’ll sketch one approach, originated by @satikusala.

  1. Do the work in a function which we’ll write in the next step, and export its return value: ^value(tagLinks($Tags))

  2. tagLinks goes through the tags, as in your ^action, and exports something for each tag:

function tagLinks(tags:list) {
   var:string result;
   tags.each(tag) {
       result = result +"<a href='“+x+'  >"+x+"</a>";
    }
   return result;

Untested, but this general approach should do what you want.

Thanks for the clarification, Mark. I was trying to use the action approach, but I couldn’t figure it out.

Thanks @eastgate, tested and for reference, for later use:

function tagLinks(tags:list) {
var:string result;
tags.each(tag) {
result = result +“<a target=‘_new’ href=‘http://localhost/?s=“+tag+”’>”+tag+“” + " ";
}
return result;
}

OK, so we know a note of an expected name path will be output when we export the file. Ee will assume the first tag is the word ‘plum’, and our note exporting the tag list is at path /Projects/X. Meanwhile the note for ‘plum’ is /Topics/plum.

So template looks like this:

^action(
var:string vTagListing =;
var:string vListStart = "<ul>\n";
var:string vListEnd = "</ul>\n";
$Tags.each(aTag){
   vTagListing += '<li><a href="..'+$Path(aTag)+'.html">'+aTag+'</a></li>\n';
};
vTagListing = vListStart+vTagListing+vListEnd;
)^<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>^title^</title>
        <style>
            ^text(/Hints/Preview/style,plain)
        </style>
    </head>
    <body>
        ^text^
        ^value(vTagListing)^
    </body>
</html>

Proof of concept TBX: export-link-list.tbx (169.9 KB)

I can see one might assume there is a function that ‘just’ does this. But consider these factors:

  • you are making an HTML link for a (document) link that doesn’t exist, so we can’t use export code (no link to export!)
  • only we know that values from $Tags have been set up elsewhere as a set of notes (i.e. to where the link points.

So, we have to calculate all this ourselves, which we can do (as above). Note the .. at the beginning of each path in the loop making the links is needed!

Assumption: containers/notes export to HTML using the same case as in their $Name—the info is case-sensitive.

1 Like

@Gernot, I think we’re getting to what you need, but can you be clear on your request?

What is your ultimate goal?

Are you trying to,

  1. Create a bulleted list
  2. create a standalone them document that links from the list to doc,

Option one:

Tags

  • Tag 1
  • Tag 2
  • Tag 3

Option two:

Tags

  • Tag 1 >link to Tag 1
  • Tag 2 >link to Tag 2
  • Tag 3 >link to Tag 3

Tag 1

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Tag 2

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Tag 3

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Actually, it’s a third option, per earlier discussion up thread. The aim is to make items in a note’s $Tags export as a list of links to notes named for that tag. The latter was one of the early unclear points.

As explained above, there is a single ‘operator’ for this as there are a number of assumptions where the app can’t guess the user’s intent (again already explained).

I think the underlying, and understandable, misconception at work here is that Tinderbox will auto-create per-Tags-value notes and ‘just’ link to them. It can’t/doesn’t, and so a little bit of user work is needed. The constituent parts of the solution:

  • list.each, to iterate $Tags.
  • Using $Path data to (help) generate the relative link to the target note.
    • Implicit assumption: there is a note in a consistent location per $Tags value
    • Implicit assumption: those notes are set to export
    • Implicit assumption: those notes are configured to export to a web path case-sensitivly consistent with $Path info.
1 Like

Thank you so much @mwra @satikusala for your feedback. This really helps a lot!

I collect notes representing central concepts/themes for my novel/project in Tinderbox. However, there may be other themes to which I only want/need to refer via a tag pointing to my Zettelkasten. I run the Zettelkasten on WordPress on my localhost. So Tinderbox HTML export and Zettelkasten WordPress work pretty well together.

If the ‘external’ tag notes have a consistent URL (except the actually tag value), then the method above will still work. I used an internal target partly as it makes it easy to show the method works.

If still stuck, the only thing we need (as we can’t) guess it is the local path to the zettelkastern tag. For instance, they could be .txt text files. a local URL would still work.

HTH.

In today’s meet-up, it was discussed if using a function to get the action-code-compiled HTML code is a cleaner approach. It is! I was in a hurry.

This export-link-list-02.tbx (218.7 KB) is the same as above except note ‘Y’ uses a different template set to get the same HTML via a function. As so often, there is more than one way.

1 Like

That’s true :o) there’s always more than one solution in Tinderbox! I really appreciate the discussion and your valuable feedback.

1 Like