Files
wekan/models/lib/customFieldStringTemplate.js
T
Lauri Ojansivu c8e89bd582 Add card context and URL encoding to custom field String Templates
Resolve public card, board, list and swimlane titles reactively in detail and minicard views. Preserve existing regex formatting and constrain linked source placement metadata to its authorized board.

Thanks to solunix and xet7 !

Fixes #3815,
2026-09-28 17:33:33 +03:00

38 lines
1.8 KiB
JavaScript

'use strict';
// Only these public display values are addressable: never walk arbitrary
// document properties or evaluate expressions supplied in a format string.
const CONTEXT_KEYS = new Set(['card.title', 'board.title', 'list.title', 'swimlane.title']);
const TOKENS = /\$\{(?:[^"{}]|"(?:\\.|[^"\\])*")*\}|%\{[^{}]*\}|\{%[^{}]*\}/g;
function formatStringTemplate(rawValue, format, separator = '', context = {}) {
if (!Array.isArray(rawValue) || typeof format !== 'string') return '';
return rawValue.filter(value => typeof value === 'string' && value.trim())
.map(value => format.replace(TOKENS, token => {
if (token.startsWith('${')) {
// Retain the existing JSON regex/flags/replace syntax. A malformed
// format remains visible rather than becoming the word "undefined".
try {
const spec = JSON.parse(token.slice(1));
if (typeof spec.regex !== 'string' || typeof spec.replace !== 'string' ||
(spec.flags !== undefined && typeof spec.flags !== 'string')) return token;
return value.replace(new RegExp(spec.regex, spec.flags), spec.replace);
} catch (error) { return token; }
}
const expression = token.slice(2, -1);
const [name, modifier, ...extra] = expression.toLowerCase().split('|');
if (extra.length || (modifier !== undefined && modifier !== 'urlencode')) return token;
let result;
if (name === 'value') result = value;
else if (CONTEXT_KEYS.has(name) && Object.hasOwn(context, name)) result = context[name];
if (typeof result !== 'string') return token;
if (modifier === 'urlencode') {
try { return encodeURIComponent(result); }
catch (error) { return token; }
}
return result;
})).join(separator ?? '');
}
module.exports = { formatStringTemplate };