-
Notifications
You must be signed in to change notification settings - Fork 18
fix(mdxish): support unquoted attributes in component tags #1389
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
Draft
maximilianfalco
wants to merge
10
commits into
next
Choose a base branch
from
falco/fix-components-not-accepting-unquoted-attributes
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e6f8533
fix: support unquoted attrs in PascalCase component tags
maximilianfalco b623854
Merge branch 'next' of https://github.com/readmeio/markdown into falc…
maximilianfalco 642774c
feat: add jsxComponentBlock micromark extension for grabbing html tags
maximilianfalco 7d8ae4e
feat: replace attrs preprocessor with new micromark extension
maximilianfalco 466df18
chore: make more test for multiline image components
maximilianfalco 23ade44
refactor: rename tokenizer to jsx-component
maximilianfalco 147bd8f
chore: extract shared utils from jsx-component tokenizer
maximilianfalco ba54154
fix: skip inline code spans when matching closing tags
maximilianfalco 2afd215
Merge branch 'next' of https://github.com/readmeio/markdown into falc…
maximilianfalco 2c9bb51
bundlewatch
maximilianfalco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
__tests__/transformers/normalize-component-attributes.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import { normalizeComponentAttributes } from '../../processor/transform/mdxish/normalize-component-attributes'; | ||
|
|
||
| describe('normalize-component-attributes', () => { | ||
| describe('self-closing block components', () => { | ||
| it('quotes unquoted URL in Image src', () => { | ||
| expect(normalizeComponentAttributes('<Image src=https://example.com/image.png alt=test />')).toBe( | ||
| '<Image src="https://example.com/image.png" alt="test" />', | ||
| ); | ||
| }); | ||
|
|
||
| it('quotes unquoted URL in Embed', () => { | ||
| expect(normalizeComponentAttributes('<Embed url=https://example.com title=Example />')).toBe( | ||
| '<Embed url="https://example.com" title="Example" />', | ||
| ); | ||
| }); | ||
|
|
||
| it('quotes unquoted attributes in Recipe', () => { | ||
| expect(normalizeComponentAttributes('<Recipe slug=my-recipe title=Recipe link=https://example.com/recipe />')).toBe( | ||
| '<Recipe slug="my-recipe" title="Recipe" link="https://example.com/recipe" />', | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('block components with children', () => { | ||
| it('quotes unquoted emoji and simple values in Callout', () => { | ||
| expect(normalizeComponentAttributes('<Callout icon=📘 theme=info>')).toBe('<Callout icon="📘" theme="info">'); | ||
| }); | ||
|
|
||
| it('quotes unquoted attributes in multi-line Callout', () => { | ||
| const input = `<Callout icon=📘 theme=info> | ||
| content | ||
| </Callout>`; | ||
| const expected = `<Callout icon="📘" theme="info"> | ||
| content | ||
| </Callout>`; | ||
| expect(normalizeComponentAttributes(input)).toBe(expected); | ||
| }); | ||
| }); | ||
|
|
||
| describe('inline components', () => { | ||
| it('quotes unquoted URL in Anchor at line start', () => { | ||
| expect(normalizeComponentAttributes('<Anchor href=https://readme.com>ReadMe</Anchor>')).toBe( | ||
| '<Anchor href="https://readme.com">ReadMe</Anchor>', | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('already-quoted attributes', () => { | ||
| it('leaves double-quoted attributes unchanged', () => { | ||
| const input = '<Image src="https://example.com/image.png" alt="test" />'; | ||
| expect(normalizeComponentAttributes(input)).toBe(input); | ||
| }); | ||
|
|
||
| it('normalizes single-quoted attributes to double quotes', () => { | ||
| const input = "<Callout icon='📘' theme='info'>"; | ||
| expect(normalizeComponentAttributes(input)).toBe('<Callout icon="📘" theme="info">'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('boolean attributes', () => { | ||
| it('preserves boolean attributes without values', () => { | ||
| expect(normalizeComponentAttributes('<Image src=test.png border />')).toBe( | ||
| '<Image src="test.png" border />', | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('mixed quoted and unquoted attributes', () => { | ||
| it('only quotes the unquoted ones', () => { | ||
| expect(normalizeComponentAttributes('<Image src="test.png" alt=test />')).toBe( | ||
| '<Image src="test.png" alt="test" />', | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('does not affect non-component content', () => { | ||
| it('ignores lowercase HTML tags', () => { | ||
| const input = '<div class=container>'; | ||
| expect(normalizeComponentAttributes(input)).toBe(input); | ||
| }); | ||
|
|
||
| it('ignores content inside fenced code blocks', () => { | ||
| const input = `\`\`\`html | ||
| <Image src=https://example.com/image.png alt=test /> | ||
| \`\`\``; | ||
| expect(normalizeComponentAttributes(input)).toBe(input); | ||
| }); | ||
|
|
||
| it('ignores content inside indented code blocks', () => { | ||
| const input = ' <Image src=https://example.com/image.png alt=test />'; | ||
| expect(normalizeComponentAttributes(input)).toBe(input); | ||
| }); | ||
|
|
||
| it('ignores PascalCase tags not at line start (inline)', () => { | ||
| const input = 'Use <Anchor href=https://readme.com>ReadMe</Anchor> for links.'; | ||
| expect(normalizeComponentAttributes(input)).toBe(input); | ||
| }); | ||
| }); | ||
|
|
||
| describe('multi-line documents', () => { | ||
| it('normalizes multiple component tags independently', () => { | ||
| const input = `<Image src=https://example.com/a.png alt=first /> | ||
|
|
||
| <Recipe slug=my-recipe title=Recipe /> | ||
|
|
||
| <Callout icon=📘 theme=info> | ||
| content | ||
| </Callout>`; | ||
| const expected = `<Image src="https://example.com/a.png" alt="first" /> | ||
|
|
||
| <Recipe slug="my-recipe" title="Recipe" /> | ||
|
|
||
| <Callout icon="📘" theme="info"> | ||
| content | ||
| </Callout>`; | ||
| expect(normalizeComponentAttributes(input)).toBe(expected); | ||
| }); | ||
|
|
||
| it('does not affect multi-line JSX expression tags', () => { | ||
| const input = `<AdvancedTable | ||
| data={[ | ||
| { 'code': '<INPUT_CODE_1>' } | ||
| ]} | ||
| />`; | ||
| expect(normalizeComponentAttributes(input)).toBe(input); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
processor/transform/mdxish/normalize-component-attributes.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { protectCodeBlocks, restoreCodeBlocks } from '../../../lib/utils/mdxish/protect-code-blocks'; | ||
|
|
||
| import { parseAttributes } from './mdxish-component-blocks'; | ||
|
|
||
| // Matches single-line PascalCase tags at line start (up to 3 spaces indent per CommonMark). | ||
| // Only block-level HTML is affected, inline HTML already allows unquoted attribute values. | ||
| const SINGLE_LINE_PASCAL_TAG_RE = /^([ ]{0,3})<([A-Z][A-Za-z0-9_]*)([^\n]*?)(\/?)\s*>/gm; | ||
|
|
||
| /** | ||
| * Wraps unquoted attribute values in PascalCase component tags with double quotes. | ||
| * | ||
| * Micromark's HTML block tokenizer rejects tags whose unquoted attribute values | ||
| * contain characters like `:` or `/` (e.g. `src=https://example.com`), and GFM | ||
| * autolinks then fragment the URLs into link nodes. By quoting these values before | ||
| * parsing, the tags are recognized as valid HTML blocks and flow through to the | ||
| * component-block transformer unchanged. | ||
| */ | ||
| export function normalizeComponentAttributes(content: string): string { | ||
| const { protectedContent, protectedCode } = protectCodeBlocks(content); | ||
|
|
||
| const normalized = protectedContent.replace( | ||
| SINGLE_LINE_PASCAL_TAG_RE, | ||
| (_match, indent: string, tagName: string, attrsPart: string, closing: string) => { | ||
| const attrs = parseAttributes(attrsPart); | ||
| if (!attrs.length) return `${indent}<${tagName}${attrsPart}${closing}>`; | ||
|
|
||
| const rebuilt = attrs | ||
| .map(attr => { | ||
| if (attr.value === null) return ` ${attr.name}`; | ||
| return ` ${attr.name}="${attr.value}"`; | ||
| }) | ||
| .join(''); | ||
|
|
||
| return `${indent}<${tagName}${rebuilt}${closing ? ` ${closing}` : ''}>`; | ||
| }, | ||
| ); | ||
|
|
||
| return restoreCodeBlocks(normalized, protectedCode); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.