diff --git a/Models/ThemeConfig/Conversion/PrepareLayer.ts b/Models/ThemeConfig/Conversion/PrepareLayer.ts index ab5f1578e1..649bb672c5 100644 --- a/Models/ThemeConfig/Conversion/PrepareLayer.ts +++ b/Models/ThemeConfig/Conversion/PrepareLayer.ts @@ -366,7 +366,7 @@ export class RewriteSpecial extends DesugaringStep { * // should warn for unexpected keys * const errors = [] * RewriteSpecial.convertIfNeeded({"special": {type: "image_carousel"}, "en": "xyz"}, errors, "test") // => {'*': "{image_carousel()}"} - * errors // => ["At test: Unexpected key in a special block: en"] + * errors // => ["The only keys allowed next to a 'special'-block are 'before' and 'after'. Perhaps you meant to put 'en' into the special block?"] * * // should give an error on unknown visualisations * const errors = [] @@ -378,24 +378,25 @@ export class RewriteSpecial extends DesugaringStep { * const errors = [] * RewriteSpecial.convertIfNeeded({"special": {}}, errors, "test") // => undefined * errors // => ["A 'special'-block should define 'type' to indicate which visualisation should be used"] - * - * + * + * * // an actual test - * const special = {"special": { - * "type": "multi", - * "before": { + * const special = { + * "before": { * "en": "

Entrances

This building has {_entrances_count} entrances:" * }, - * "after": { + * "after": { * "en": "{_entrances_count_without_width_count} entrances don't have width information yet" * }, + * "special": { + * "type": "multi", * "key": "_entrance_properties_with_width", * "tagrendering": { * "en": "An entrance of {canonical(width)}" * } * }} * const errors = [] - * RewriteSpecial.convertIfNeeded(special, errors, "test") // => {"en": "

Entrances

This building has {_entrances_count} entrances: {multi(_entrance_properties_with_width,An entrance of &LBRACEcanonical&LPARENSwidth&RPARENS&RBRACE)}An entrance of {canonical(width)}"} + * RewriteSpecial.convertIfNeeded(special, errors, "test") // => {"en": "

Entrances

This building has {_entrances_count} entrances:{multi(_entrance_properties_with_width,An entrance of &LBRACEcanonical&LPARENSwidth&RPARENS&RBRACE)}{_entrances_count_without_width_count} entrances don't have width information yet"} * errors // => [] */ private static convertIfNeeded(input: (object & { special: { type: string } }) | any, errors: string[], context: string): any { @@ -409,12 +410,18 @@ export class RewriteSpecial extends DesugaringStep { errors.push("A 'special'-block should define 'type' to indicate which visualisation should be used") return undefined } + const vis = SpecialVisualizations.specialVisualizations.find(sp => sp.funcName === type) if (vis === undefined) { const options = Utils.sortedByLevenshteinDistance(type, SpecialVisualizations.specialVisualizations, sp => sp.funcName) errors.push(`Special visualisation '${type}' not found. Did you perhaps mean ${options[0].funcName}, ${options[1].funcName} or ${options[2].funcName}?\n\tFor all known special visualisations, please see https://github.com/pietervdvn/MapComplete/blob/develop/Docs/SpecialRenderings.md`) return undefined } + errors.push(... + Array.from(Object.keys(input)).filter(k => k !== "special" && k !== "before" && k !== "after") + .map(k => { + return `The only keys allowed next to a 'special'-block are 'before' and 'after'. Perhaps you meant to put '${k}' into the special block?`; + })) const argNamesList = vis.args.map(a => a.name) const argNames = new Set(argNamesList) @@ -471,20 +478,20 @@ export class RewriteSpecial extends DesugaringStep { for (const ln of languages) { const args = [] for (const argName of argNamesList) { - const v = special[argName] ?? "" + let v = special[argName] ?? "" if (Translations.isProbablyATranslation(v)) { - const txt = new Translation(v).textFor(ln) - .replace(/,/g, "&COMMA") - .replace(/\{/g, "&LBRACE") - .replace(/}/g, "&RBRACE") - ; - args.push(txt) - } else if (typeof v === "string") { + v = new Translation(v).textFor(ln) + + } + + if (typeof v === "string") { const txt = v.replace(/,/g, "&COMMA") .replace(/\{/g, "&LBRACE") .replace(/}/g, "&RBRACE") + .replace(/\(/g, "&LPARENS") + .replace(/\)/g, '&RPARENS') args.push(txt) - } else if(typeof v === "object"){ + } else if (typeof v === "object") { args.push(JSON.stringify(v)) } else { args.push(v) @@ -518,7 +525,7 @@ export class RewriteSpecial extends DesugaringStep { * const result = new RewriteSpecial().convert(tr,"test").result * const expected = {render: {'en': "Some introduction{image_carousel(image)}"}} * result // => expected - * + * * // Should put text after if specified * const tr = { * render: {special: {type: "image_carousel", image_key: "image"}, after: {en: "Some footer"} },