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(v2): handle multiple asset links in one line properly #3653

Merged
merged 5 commits into from
Oct 30, 2020

Conversation

lex111
Copy link
Contributor

@lex111 lex111 commented Oct 28, 2020

Motivation

Attempt to fix #3631

Note: I didn't quite understand why the parent node content should be changed now, if it's easier and safer to change the child node (which was done).

Previous tests are passed successfully + add new test for new use case.

Have you read the Contributing Guidelines on pull requests?

Yes

Test Plan

Unit tests (I guess).

Related PRs

(If this PR adds or changes functionality, please take some time to update the docs at https://github.com/facebook/docusaurus, and link to your PR here.)

@lex111 lex111 added the pr: bug fix This PR fixes a bug in a past release. label Oct 28, 2020
@lex111 lex111 requested a review from slorber as a code owner October 28, 2020 18:14
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Oct 28, 2020
@docusaurus-bot
Copy link
Contributor

docusaurus-bot commented Oct 28, 2020

Deploy preview for docusaurus-2 ready!

Built with commit 51fa150

https://deploy-preview-3653--docusaurus-2.netlify.app

Copy link
Collaborator

@slorber slorber left a comment

Choose a reason for hiding this comment

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

Thanks, LGTM

@anshulrgoyal if there's a strong reason to modify the parent node it's time to tell us :)

});

parent.children.splice(index + 2, 0, {type: 'jsx', value: '</a>'});
} >${linkText}</a>`;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
} >${linkText}</a>`;
}>${linkText}</a>`;

Wonder if we can sately remove this space here? Seems not useful to keep it and unusual?
This would modify the snapshots but should be fine

@anshulrgoyal
Copy link
Contributor

I think it is to support children in a tag

@slorber
Copy link
Collaborator

slorber commented Oct 29, 2020

I think it is to support children in a tag

@anshulrgoyal can you give an example md snippet? not sure to understand what you mean by that but it may be worth adding an extra test case of this

@anshulrgoyal
Copy link
Contributor

Something like this

[![image](url)](url)

@slorber
Copy link
Collaborator

slorber commented Oct 29, 2020

oh, you mean an image link?
Didn't know it was possible to do that, but worth checking if this still works

@anshulrgoyal
Copy link
Contributor

anshulrgoyal commented Oct 29, 2020

oh, you mean an image link?
Didn't know it was possible to do that, but worth checking if this still works

In MDX the file is converted to MDAST->HAST->MDXAST. So we can't just put link text between <a> and </a>

@lex111
Copy link
Contributor Author

lex111 commented Oct 29, 2020

Indeed, image links do not work in the current implementation (because that use case is not covered by unit test), as well as rendering other (child) HTML tags inside the asset link (because type for all children is hardcoded as text).

In this PR I fixed these issues, see unit tests/snapshots for details.

I also did a small cleanup code (removed extra spaces in the output and unnecessary slug import , escaped title attribute).
So now I guess everything should be fine. @slorber please re-check this.

Note for future refactoring: instead of using string interpolation we need to come up with a clearer and better way to create a elements and dynamically add attributes to them.

Strange (?) thing: It seems that the handling of asset paths between transformLinks and transformImage is different (compare the body of toAssetLinkNode and processImageNode functions). For example, @site alias does not support in transformImage, that how it should be?

@anshulrgoyal
Copy link
Contributor

Hi @lex111 If a child is JSX then how is it handled like:-

[<Button>Open</Button>](url)

@slorber
Copy link
Collaborator

slorber commented Oct 30, 2020

Thanks @lex111 , LGTM!

Tried this in MDX playground, didn't even know it was possible 🤪

[<div style={{color: "red"}}>Open</div>](http://reactnative.dev/)

Does your code support it currently? Looks like it support MD in link but JSX?

Also what about adding a complex example of this dogfooding page?
https://v2.docusaurus.io/examples/markdownPageExample
This is a bit messy, we can refactor it a bit later but at least it ensures that it actually works in production :)

For example, @site alias does not support in transformImage, that how it should be?

Yes, this is a bit different, wanted to enable @site alias for both but didn't have time and had to release an urgent fix a few releases back 😅 if you can support @site alias in the other code as well that would be great :)

@lex111
Copy link
Contributor Author

lex111 commented Oct 30, 2020

JSX inside asset Markdown link is already supported, although I don't think anyone will use it actively, but I updated the test to make sure that it really works.

@lex111
Copy link
Contributor Author

lex111 commented Oct 30, 2020

Also what about adding a complex example of this dogfooding page?

Hmm, this is standard functionality that is assumed to work out of the box. I do not know which new use cases to add in the example demo page. So if you think you need to update this page, please feel free do it yourself.

@slorber
Copy link
Collaborator

slorber commented Oct 30, 2020

thanks, let's merge this :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: bug fix This PR fixes a bug in a past release.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Linking multiple PDFs in one line causes parsing error
5 participants