Adding a parameter to a recursive ancestor-tagged function breaks it

Background

For a wiki I’ve been developing for a work project, I have a bunch of snippets I want to be able to copy and paste from a button in the TOC, using my text-free variant of copy-to-clipboard macro.

I wrote a recursive function to check whether a tiddler has the tag Snippet or whether one if its tags has the tag Snippet or if one of those tags has the tag Snippet, or…

It looks like this:

\function .step(tid) [<tid>tags[]] :map[is.snippet<currentTiddler>]

\function is.snippet(tid) [<tid>tag[Snippet]then[yes]] :else[<tid>!has[tags]then[no]] :else[.step<tid>]

and I use in in an override to toc-caption . (And yes, I know that this could have an infinite recursion problem; for a shared tool, I’d need to avoid loops; for the current usage, this is fine.)

I call it like this:

  <% if [is.snippet<currentTiddler>match[yes]] %> 
    <$transclude $variable="clipboard-button" src={{{ [{!!text}] }}} />
  <% endif %>

That’s all working fine.

(If you want to try it, this version uses Welcome instead of Snippet, suitable for dragging onto the main site: Copy snippet.json (1.0 KB). It should show a copy button on everything in the Welcome hierarchy.)

My Question

It clearly would be useful to make that function more generic. The tag name should be a parameter rather than hard-coded. When I tried to do this, it failed (the buttons didn’t show), and I can’t see why. The code is below. Any suggestions?

function .step(tagname, tid) [<tid>tags[]] :map[in.hierarchy<tagname>,<currentTiddler>]

\function in.hierarchy(tagname, tid) [<tid>tag<tagname>then[yes]] :else[<tid>!has[tags]then[no]] :else[.step<tagname>,<tid>]

called liike

  <% if [in.hierarchy<Snippet>,<currentTiddler>match[yes]] %> 
    <$transclude $variable="clipboard-button" src={{{ [{!!text}] }}} />
  <% endif %>

Any suggestions for better ways to do this are more than welcome, too!




BTW, I’ve been out-of-touch here for a few weeks. Work and other side projects have taken all free moments I have (and some not-so-free ones too!) If you’re waiting for anything from me, I apologize; feel free to DM me.

1 Like

This may not be your issue at all, but I wanted to eliminate some low-hanging fruit first…

<% if [in.hierarchy<Snippet>,<currentTiddler>match[yes]] %>

<<Snippet>> is defined in this context, right? I ask mostly because you were using tag[Snippet] as a hard-coded value in your working version, and you don’t seem to be using capitalized variables otherwise.

1 Like

“Low-hanging fruit” to some. “Annoyingly silly blind spot” to others.

Thank you very much. That’s exactly what it was. I should have used [Snippet], and couldn’t see it for the life of me. I’d say that this must be what happens when I spend a few weeks away from serious TW work… but similar things happened plenty often when I was steeped in it. I’d be learning new stuff, sailing along, improving my skills happily, when some silly syntax issue derailed me for days.

Thanks for putting me back on track!

2 Likes

This is my one real complaint about TiddlyWiki, and we should do what we can to reduce these. I was collecting info about such cases, but usually when I solve it I just stay on topic and forget to document it properly. Perhaps we could crowd source this.