# Script API to serialize a resource reference in a given syntax

**URL:** <https://forum.xwiki.org/t/script-api-to-serialize-a-resource-reference-in-a-given-syntax/19021>\
**Category:** Development\
**Tags:** proposal\
**Created:** [October 24, 2026, 3:09pm UTC](https://forum.xwiki.org/t/script-api-to-serialize-a-resource-reference-in-a-given-syntax/19021 "2026-10-24T15:09:18Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![vmassol](https://forum.xwiki.org/user_avatar/forum.xwiki.org/vmassol/32/6_2.png) [@vmassol](https://forum.xwiki.org/u/vmassol)\
**Post date:** [October 24, 2026, 3:56pm UTC](https://forum.xwiki.org/t/script-api-to-serialize-a-resource-reference-in-a-given-syntax/19021/2 "2026-10-24T15:56:44Z")

</div>

Some context I found while researching this:

- The [Security Developer Guide](https://www.xwiki.org/xwiki/bin/view/Documentation/DevGuide/Security/#HLinklabel) 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...](https://jira.xwiki.org/browse/XWIKI-20150) (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](https://forum.xwiki.org/t/syntax-support-in-translations-translation-parameters/11646) 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:

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:

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:

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:

WDYT?

Thanks

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

---

_[View the full topic](https://forum.xwiki.org/t/script-api-to-serialize-a-resource-reference-in-a-given-syntax/19021)._
