Skip to content

Commit

Permalink
feat(mf2): Support u:id option (unicode-org/message-format-wg#846)
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Oct 27, 2024
1 parent 6e941aa commit 5b2254e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
3 changes: 2 additions & 1 deletion mf2/messageformat/src/data-model/format-markup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export function formatMarkup(
if (typeof rv === 'object' && typeof rv?.valueOf === 'function') {
rv = rv.valueOf();
}
part.options[name] = rv;
if (name === 'u:id') part.id = String(rv);
else part.options[name] = rv;
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions mf2/messageformat/src/data-model/function-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export class MessageFunctionContext {
#ctx: Context;
#locales: Intl.Locale[];
readonly dir: 'ltr' | 'rtl' | 'auto' | undefined;
readonly id: string | undefined;
readonly source: string;
constructor(ctx: Context, source: string, options?: Options) {
this.#ctx = ctx;
Expand Down Expand Up @@ -42,6 +43,9 @@ export class MessageFunctionContext {
}
}

const idOpt = options?.get('u:id');
this.id = idOpt ? String(resolveValue(ctx, idOpt)) : undefined;

this.source = source;
}

Expand Down
12 changes: 11 additions & 1 deletion mf2/messageformat/src/data-model/resolve-function-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ export function resolveFunctionRef(
`Function :${name} did not return a MessageValue`
);
}
if (msgCtx.id && typeof res.toParts === 'function') {
return {
...res,
toParts() {
const parts = res.toParts!();
for (const part of parts) part.id = msgCtx.id;
return parts;
}
};
}
return res;
} catch (error) {
ctx.onError(error);
Expand All @@ -51,7 +61,7 @@ function resolveOptions(ctx: Context, options: Options | undefined) {
const opt: Record<string, unknown> = Object.create(null);
if (options) {
for (const [name, value] of options) {
if (name !== 'u:dir' && name !== 'u:locale') {
if (name !== 'u:dir' && name !== 'u:id' && name !== 'u:locale') {
opt[name] = resolveValue(ctx, value);
}
}
Expand Down
2 changes: 2 additions & 0 deletions mf2/messageformat/src/formatted-parts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface MessageExpressionPart {
source: string;
dir?: 'ltr' | 'rtl';
locale?: string;
id?: string;
parts?: Array<{ type: string; source?: string; value?: unknown }>;
value?: unknown;
}
Expand All @@ -32,6 +33,7 @@ export interface MessageMarkupPart {
kind: 'open' | 'standalone' | 'close';
source: string;
name: string;
id?: string;
options?: { [key: string]: unknown };
}

Expand Down

0 comments on commit 5b2254e

Please sign in to comment.