Skip to content

Commit 1f4b633

Browse files
feat(mf2): Support u:id option (unicode-org/message-format-wg#846)
1 parent 2670f4c commit 1f4b633

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

mf2/messageformat/src/data-model/format-markup.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export function formatMarkup(
2323
if (typeof rv === 'object' && typeof rv?.valueOf === 'function') {
2424
rv = rv.valueOf();
2525
}
26-
part.options[name] = rv;
26+
if (name === 'u:id') part.id = String(rv);
27+
else part.options[name] = rv;
2728
}
2829
}
2930
}

mf2/messageformat/src/data-model/function-context.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export class MessageFunctionContext {
77
#ctx: Context;
88
#locales: Intl.Locale[];
99
readonly dir: 'ltr' | 'rtl' | 'auto' | undefined;
10+
readonly id: string | undefined;
1011
readonly source: string;
1112
constructor(ctx: Context, source: string, options?: Options) {
1213
this.#ctx = ctx;
@@ -42,6 +43,9 @@ export class MessageFunctionContext {
4243
}
4344
}
4445

46+
const idOpt = options?.get('u:id');
47+
this.id = idOpt ? String(resolveValue(ctx, idOpt)) : undefined;
48+
4549
this.source = source;
4650
}
4751

mf2/messageformat/src/data-model/resolve-function-ref.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ export function resolveFunctionRef(
3939
`Function :${name} did not return a MessageValue`
4040
);
4141
}
42+
if (msgCtx.id && typeof res.toParts === 'function') {
43+
return {
44+
...res,
45+
toParts() {
46+
const parts = res.toParts!();
47+
for (const part of parts) part.id = msgCtx.id;
48+
return parts;
49+
}
50+
};
51+
}
4252
return res;
4353
} catch (error) {
4454
ctx.onError(error);
@@ -51,7 +61,7 @@ function resolveOptions(ctx: Context, options: Options | undefined) {
5161
const opt: Record<string, unknown> = Object.create(null);
5262
if (options) {
5363
for (const [name, value] of options) {
54-
if (name !== 'u:dir' && name !== 'u:locale') {
64+
if (name !== 'u:dir' && name !== 'u:id' && name !== 'u:locale') {
5565
opt[name] = resolveValue(ctx, value);
5666
}
5767
}

mf2/messageformat/src/formatted-parts.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface MessageExpressionPart {
1616
source: string;
1717
dir?: 'ltr' | 'rtl';
1818
locale?: string;
19+
id?: string;
1920
parts?: Array<{ type: string; source?: string; value?: unknown }>;
2021
value?: unknown;
2122
}
@@ -32,6 +33,7 @@ export interface MessageMarkupPart {
3233
kind: 'open' | 'standalone' | 'close';
3334
source: string;
3435
name: string;
36+
id?: string;
3537
options?: { [key: string]: unknown };
3638
}
3739

0 commit comments

Comments
 (0)