-
Notifications
You must be signed in to change notification settings - Fork 53
fix: render code block props as data-* attributes on native pre
#463
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f180bb8
fix: render code block props as `data-*` attributes on native `pre`
danielroe f74db9d
fix: keep code block props in binding scope of native `pre` children
danielroe 8fedac9
chore: update bundle size snapshot
danielroe 4f5d0c0
fix: do not change html blocks
farnabaz 9e40805
Update markdown-node.component.ts
farnabaz e02890f
Update bundle.test.ts
farnabaz 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
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
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
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,53 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { renderToString } from 'react-dom/server' | ||
| import { parseMarkdown } from 'comark' | ||
| import { MarkdownDocument } from '../src/components/MarkdownDocument' | ||
|
|
||
| const markdown = '```ruby {1} [app.rb] foo=bar\nputs 1\n```' | ||
|
|
||
| describe('code blocks', () => { | ||
| it('should render code block props as data attributes on native pre', async () => { | ||
| const tree = await parseMarkdown(markdown) | ||
| const html = renderToString(<MarkdownDocument value={tree} />) | ||
| expect(html).toContain( | ||
| '<pre data-language="ruby" data-highlights="[1]" data-filename="app.rb" data-meta="foo=bar"><code class="language-ruby">' | ||
| ) | ||
| }) | ||
|
|
||
| it('should pass code block props unchanged to a custom pre component', async () => { | ||
| const tree = await parseMarkdown(markdown) | ||
| function ProsePre({ language, filename, highlights, meta }: any) { | ||
| return <div data-props={JSON.stringify({ language, filename, highlights, meta })} /> | ||
| } | ||
| const html = renderToString( | ||
| <MarkdownDocument | ||
| value={tree} | ||
| components={{ ProsePre }} | ||
| /> | ||
| ) | ||
| expect(html).toContain( | ||
| '{"language":"ruby","filename":"app.rb","highlights":[1],"meta":"foo=bar"}' | ||
| ) | ||
| }) | ||
|
|
||
| it('should keep authored language on a raw HTML pre', () => { | ||
| const tree = { | ||
| nodes: [['pre', { $: { html: 1, block: 1 }, language: 'ruby' }, 'puts 1']], | ||
| frontmatter: {}, | ||
| meta: {}, | ||
| } as any | ||
| const html = renderToString(<MarkdownDocument value={tree} />) | ||
| expect(html).toContain('<pre language="ruby">') | ||
| expect(html).not.toContain('data-language') | ||
| }) | ||
|
|
||
| it('should keep code block props in the binding scope of native pre children', () => { | ||
| const tree = { | ||
| nodes: [['pre', { language: 'ruby' }, ['code', { ':data-lang': 'props.language' }, 'x']]], | ||
| frontmatter: {}, | ||
| meta: {}, | ||
| } as any | ||
| const html = renderToString(<MarkdownDocument value={tree} />) | ||
| expect(html).toContain('<code data-lang="ruby">') | ||
| }) | ||
| }) |
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
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
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
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,67 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { createSSRApp, defineComponent, h } from 'vue' | ||
| import { renderToString } from '@vue/server-renderer' | ||
| import { parseMarkdown } from 'comark' | ||
| import { MarkdownDocument } from '../src/components/MarkdownDocument' | ||
|
|
||
| const markdown = '```ruby {1} [app.rb] foo=bar\nputs 1\n```' | ||
|
|
||
| async function renderTree(components: Record<string, any> = {}) { | ||
| const tree = await parseMarkdown(markdown) | ||
| return renderToString(createSSRApp({ render: () => h(MarkdownDocument, { value: tree, components }) })) | ||
| } | ||
|
|
||
| describe('code blocks', () => { | ||
| it('should render code block props as data attributes on native pre', async () => { | ||
| const html = await renderTree() | ||
| expect(html).toContain( | ||
| '<pre data-language="ruby" data-highlights="[1]" data-filename="app.rb" data-meta="foo=bar"><code class="language-ruby">' | ||
| ) | ||
| }) | ||
|
|
||
| it('should pass code block props unchanged to a custom pre component', async () => { | ||
| const ProsePre = defineComponent({ | ||
| props: ['language', 'filename', 'highlights', 'meta'], | ||
| setup(props, { slots }) { | ||
| return () => | ||
| h( | ||
| 'div', | ||
| { | ||
| 'data-props': JSON.stringify({ | ||
| language: props.language, | ||
| filename: props.filename, | ||
| highlights: props.highlights, | ||
| meta: props.meta, | ||
| }), | ||
| }, | ||
| slots.default?.() | ||
| ) | ||
| }, | ||
| }) | ||
| const html = await renderTree({ ProsePre }) | ||
| expect(html).toContain( | ||
| '{"language":"ruby","filename":"app.rb","highlights":[1],"meta":"foo=bar"}' | ||
| ) | ||
| }) | ||
|
|
||
| it('should keep authored language on a raw HTML pre', async () => { | ||
| const tree = { | ||
| nodes: [['pre', { $: { html: 1, block: 1 }, language: 'ruby' }, 'puts 1']], | ||
| frontmatter: {}, | ||
| meta: {}, | ||
| } as any | ||
| const html = await renderToString(createSSRApp({ render: () => h(MarkdownDocument, { value: tree }) })) | ||
| expect(html).toContain('<pre language="ruby">') | ||
| expect(html).not.toContain('data-language') | ||
| }) | ||
|
|
||
| it('should keep code block props in the binding scope of native pre children', async () => { | ||
| const tree = { | ||
| nodes: [['pre', { language: 'ruby' }, ['code', { ':data-lang': 'props.language' }, 'x']]], | ||
| frontmatter: {}, | ||
| meta: {}, | ||
| } as any | ||
| const html = await renderToString(createSSRApp({ render: () => h(MarkdownDocument, { value: tree }) })) | ||
| expect(html).toContain('<code data-lang="ruby">') | ||
| }) | ||
| }) |
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
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
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
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
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
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
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
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
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
Oops, something went wrong.
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.