-
Notifications
You must be signed in to change notification settings - Fork 8
Add markup support #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
dminor
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me.
My only suggestion is to incorporate some of the explanation from the pull request into the explainer itself.
|
@dminor Thank you for the review! I've added a few phrases in response to your inline comments. In the MFWG, unicode-org/message-format-wg#470 may end up refining that spec layer's understanding of markup, so I'll hold off on merging this until at least next week, given that MFWG members will be meeting in Seville during W3C TPAC and some progress is expected on this topic. |
|
@catamorphism Thank you for the review! The terminology was indeed a bit wrong, but I ended up updating it towards the practice introduced in unicode-org/message-format-wg#470, which treats markup as a different thing from expressions. |
|
This PR will need to be updated once the current MFWG discussions on markup are concluded. |
|
The changes here have now been updated to match the accepted spec design. I've also included |
|
I ended up dropping the |
dminor
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few nits, but overall, this looks really good!
Co-authored-by: Daniel Minor <dminor@mozilla.com>
ryzokuken
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The actual contents of this PR LGTM and the idea to just emit markup in formatToParts to let people post-process it themselves sounds like a fair plan but did I misunderstand at some point or was this always how markup was intended to work? I know that formatToParts is a JS-only thing at the moment but I assumed that handling markup would involve exposing special functions (which would work in both format and formatToParts) but this should work too. If this way of dealing with markup is already implemented in the polyfill, I'd like to try it out with some sort of React app that injects components inside a message to see if it works as intended.
|
Yeah, requiring post-processing for markup has been the idea for a while, and it's supported by the polyfill. So with import { MessageFormat } from 'messageformat';
const mf = new MessageFormat('Have a {#b}great{/b} day!', 'en');
mf.formatToParts()and get: [
{ type: 'literal', value: 'Have a ' },
{ type: 'markup', kind: 'open', source: '#b', name: 'b' },
{ type: 'literal', value: 'great' },
{ type: 'markup', kind: 'close', source: '/b', name: 'b' },
{ type: 'literal', value: ' day!' }
] |
A core value proposition of MF2 is being able to represent messages that have some internal structure, such as:
In the MF2 spec, the
+and-annotation prefixes are intentionally loosely defined to correspond to "start/open" and "end/close" parts of a message, to allow for implementation flexibility. The examples above demonstrate some of that: They may have inputs and options; they do not need to nest cleanly, and their names may include characters like.that aid in subclassing.To support these in
Intl.MessageFormat, the approach proposed here is both rather restrictive and permissive: The resolution of the syntax parts is not opened for customization (as is the case for standalone placeholders like:number), but the corresponding formatted parts are emitted in a form that makes their post-processing as easy as possible. For example:Given that, a post-processor could quite easily construct from it a DOM fragment, a sequence of React elements, or an HTML string representation, applying any validation rules specific to the target structure.
Formatting an open/close markup expression to a string always results in the empty string, so continuing with the above:
This post-processing approach is required to support the wide range of use cases and environments that JavaScript has, and it's to some extent forced by the MF2 spec, which does not support internally defining something like a formatter that is capable of consuming a sequence of message parts, rather than just one part.