feat: add option to perserve variable syntax in plain text compiler#1345
feat: add option to perserve variable syntax in plain text compiler#1345
Conversation
| it('falls back to key name for unresolved variables when others are provided', () => { | ||
| const md = 'Hello {user.name} from {user.company}'; | ||
|
|
||
| expect(plain(hast(md), { variables: { name: 'Owlbert' } })).toBe('Hello Owlbert from company'); |
There was a problem hiding this comment.
is this fallback technically new or existing behavior in our system?
i'm a lil cautious and worried about having this be our default behavior. from a user and privacy perspective, i would expect user variables to never become publicized, since user var names are technically "private" or internal information. this currently reveals unresolved user var names and lets them show up in search results.
so if a user var is undefined, i'd expect the plain text compiler to simply omit it
const md = 'Hello {user.name} from {user.company} and good bye';
expect(plain(hast(md), { variables: { name: 'Owlbert' } })).toBe('Hello Owlbert from and good bye');
There was a problem hiding this comment.
Hey! This is actually existing behaviour that predates this PR. The view mode already falls back to the capitalised variable name when rendering docs, and search indexing / table of contents do the same with the key name. This PR doesn't change any of that, it just adds the preserveVariableSyntax option on top for the search indexing path.
I think it's less of a privacy concern since the markdown source is always visible in the frontend anyway, and variable names tend to be pretty generic stuff. Also we can update the behaviour when interpolating in the frontend (readmeio/readme/pull/17331), like omitting it if unresolved at rendering time
There was a problem hiding this comment.
thank you for adding tests
__tests__/lib/plain.test.ts
Outdated
| const txt = '{user.name}'; | ||
|
|
||
| expect(plain(hast(txt), { preserveVariableSyntax: true })).toBe('{user.name}'); |
There was a problem hiding this comment.
can we give txt some before and after text to check that it correctly interpolates?
| const txt = '{user.name}'; | |
| expect(plain(hast(txt), { preserveVariableSyntax: true })).toBe('{user.name}'); | |
| const txt = 'Hello {user.name} and good bye'; | |
| expect(plain(hast(txt), { preserveVariableSyntax: true })).toBe('Hello {user.name} and good bye'); |
__tests__/lib/plain.test.ts
Outdated
| const txt = '<Variable name="company">company</Variable>'; | ||
| const tree = hast(txt); | ||
|
|
||
| expect(plain(tree, { preserveVariableSyntax: true })).toBe('{user.company}'); |
There was a problem hiding this comment.
same here, to add before + after text
__tests__/lib/plain.test.ts
Outdated
| it('preserves multiple variables with preserveVariableSyntax option', () => { | ||
| const md = 'Hello {user.name}, welcome to {user.company}!'; | ||
|
|
||
| expect(plain(hast(md), { preserveVariableSyntax: true })).toBe('Hello {user.name} , welcome to {user.company} !'); | ||
| }); |
There was a problem hiding this comment.
this is a duplicate test of L119, is probably unnecessary and can be removed
lib/plain.ts
Outdated
| /** When true, outputs variables using `{user.key}` syntax instead of resolving | ||
| * to values or bare key names. Used by search indexing so the frontend can | ||
| * interpolate variables at display time. */ |
There was a problem hiding this comment.
| /** When true, outputs variables using `{user.key}` syntax instead of resolving | |
| * to values or bare key names. Used by search indexing so the frontend can | |
| * interpolate variables at display time. */ | |
| /** | |
| * When true, outputs variables using `{user.key}` syntax instead of resolving | |
| * to values or bare key names. Used by search indexing so the frontend can | |
| * interpolate variables at display time. | |
| */ |
## Version 13.3.0 ### ✨ New & Improved * **mdxish:** add legacy variable tokenizer ([#1339](#1339)) ([8e8b11b](8e8b11b)) * add option to perserve variable syntax in plain text compiler ([#1345](#1345)) ([5ab350e](5ab350e)) * **mdxish:** resolve variables in code blocks ([#1350](#1350)) ([a6460f8](a6460f8)) * **mdxish:** use variable name for heading slug generation ([#1340](#1340)) ([61a97d3](61a97d3)) <!--SKIP CI-->
This PR was released!🚀 Changes included in v13.3.0 |

🧰 Changes
Adds a
preserveVariableSyntaxoption to theplain()text compiler so it can output variables in{user.key}format instead of resolving them to bare key names.When we index docs for search, we want to keep the variable syntax intact so the frontend can interpolate them with actual user values at display time (e.g. showing "Welcome Owlbert" instead of "Welcome name" in search results).
Both MDX expressions (
{user.name}) and legacy<Variable>tags get unified into the same{user.key}output format. Also added a bunch of edge case tests.Related:
🧬 QA & Testing
Expected output: