Script API to serialize a resource reference in a given syntax

Some context I found while researching this:

  • The Security Developer Guide documents the current manual way: escape the link label twice (escape(escape($label))) and the reference once. It doesn’t mention the resource type prefix, and its translation example even inserts >>$target unescaped. If we agree on a new API, I’ll update that section to recommend it.
  • Loading... (open since 2022) asks for helpers to perform escaping with wiki syntax, including “escape labels of wiki links (double escape)”. This proposal would address part of it.
  • In Syntax support in translations/translation parameters we agreed to pass blocks (e.g. links) as translation parameters (already supported by the localization module) and to “provide a script API that allow creating common blocks (like links, emphasize, bold, italics) easily without writing syntax”. That script API was never implemented.

Regarding my open question 2 (a helper generating the whole link), I see these options. To compare them, here’s the same link written with each. Done properly by hand today, it reads:

* [[$services.rendering.escape($services.rendering.escape($label, 'xwiki/2.1'), 'xwiki/2.1')>>doc:$services.rendering.escape($services.model.serialize($reference, 'default'), 'xwiki/2.1')||queryString="$services.rendering.escape($queryString, 'xwiki/2.1')"]]
  1. escapeLinkLabel(label, syntax), used together with serializeResourceReference(). The template keeps showing the link syntax and Velocity templating stays natural, but it’s verbose and you still have to escape the parameter values yourself:

     * [[$services.rendering.escapeLinkLabel($label, 'xwiki/2.1')>>$services.rendering.serializeResourceReference($reference, 'xwiki/2.1')||queryString="$services.rendering.escape($queryString, 'xwiki/2.1')"]]
    
  2. serializeLink(label, reference, parameters, syntax), returning the link in the given syntax, with the label, the reference and the parameter values all escaped by the renderer. Safer and shorter to write, but you lose sight of the syntax in the template:

     * $services.rendering.serializeLink($label, $reference, {'queryString': $queryString}, 'xwiki/2.1')
    
  3. A #wikiLink($label $reference $parameters) Velocity macro (in macros.vm), built on 2. It’s consistent with the existing #wikiTranslation helper, and a bit closer to the template syntax than a script service call:

     * #wikiLink($label $reference {'queryString': $queryString})
    
  4. A createLink(label, reference, parameters) returning a LinkBlock. Not that useful on its own (it still needs rendering), but it would also serve the translation use case above, where the link is passed as a translation parameter:

     #set ($link = $services.rendering.createLink($label, $reference, {'queryString': $queryString}))
     * $services.rendering.render($link, 'xwiki/2.1')
     ## or, inside a translation such as "myapp.archive=Articles from {0}":
     * $services.localization.render('myapp.archive', 'xwiki/2.1', [$link])
    

WDYT?

Thanks

PS: This post was co-written with Claude Code.