Skip to content
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

Fix cases for JSX-like expressions in code blocks of headings #3502

Merged

Conversation

nokazn
Copy link
Contributor

@nokazn nokazn commented Jun 2, 2022

Changes

I fixed cases to render incorrectly JSX-like expressions in code blocks of headings.
See also #3491 for details.

Testing

Add test cases including JSX-like expressions in code blocks of headings like below.

# `{frontmatter.title}`

# `{ foo }` is a shorthand for `{ foo: foo }`

###### `{}` is equivalent to `Record<never, never>` <small>(at TypeScript v{frontmatter.version})</small>

Docs

N/A

Related

@changeset-bot
Copy link

changeset-bot bot commented Jun 2, 2022

🦋 Changeset detected

Latest commit: fb69531

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@astrojs/markdown-remark Minor
astro Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the feat: markdown Related to Markdown (scope) label Jun 2, 2022
@nokazn nokazn marked this pull request as draft June 2, 2022 12:16
@nokazn nokazn force-pushed the feature/fix-render-markdown-in-header branch from 83c307a to 45dbd17 Compare June 2, 2022 12:44
@nokazn nokazn force-pushed the feature/fix-render-markdown-in-header branch from 45dbd17 to 1a42da6 Compare June 2, 2022 12:50
@nokazn nokazn marked this pull request as ready for review June 2, 2022 12:57
@matthewp
Copy link
Contributor

matthewp commented Jun 2, 2022

I think that the way you are doing this but skipping any nodes where the parent is a code/pre entirely will cause there to be no id if there is only a codeblock and nothing else.

To test this, create a test for:

await renderMarkdown(`# \`{frontmatter.title}\``, {})

That should still render some id. In my branch that was making the id be "frontmattertitle". See https://github.com/withastro/astro/pull/3510/files

Happy to go with your approach in general, just want to make sure we handle this case.

visit(node, (child) => {
if (child.type === 'element') {
visit(node, (child, _, parent) => {
if (child.type === 'element' || ['code', 'pre'].some((tag) => parent?.tagName === tag)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use an object or Set here to avoid an unnecessary O(2) lookup.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to use Set to scan parent.tagName in fb69531

@nokazn
Copy link
Contributor Author

nokazn commented Jun 2, 2022

@matthewp
Thank you for your feedback.

I think that the way you are doing this but skipping any nodes where the parent is a code/pre entirely will cause there to be no id if there is only a codeblock and nothing else.

This case was not considered, then fixed by fb69531

@nokazn nokazn requested a review from matthewp June 2, 2022 22:12
Copy link
Contributor

@matthewp matthewp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks!

@matthewp matthewp merged commit 939fe15 into withastro:main Jun 3, 2022
@github-actions github-actions bot mentioned this pull request Jun 3, 2022
@nokazn nokazn deleted the feature/fix-render-markdown-in-header branch June 3, 2022 23:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: markdown Related to Markdown (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 BUG: JSX-like expressions in code blocks are rendered incorrectly in headings
2 participants