-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
[gatsby-remark-copy-linked-files] Fix regression from #2871 with RegExp cannons #2901
Merged
Conversation
This file contains 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
Very icky, but works for now. I broke copying resources referenced for HTML-in-Markdown links, video sources, and images in gatsbyjs#2871 when trying to fix gatsbyjs#2696 by removing writing back the HTML `node.value` via Cheerio’s .html(). Cheerio closes open HTML tags, which in our case caused the problems described in gatsbyjs#2696—an HTML node a with value `<a href=…>` would be written back as `<a href=…`></a>. Since I couldn’t figure out how to do this with either Cheerio nor Rehype/unified, here’s a beginner fix 😬 that replaces the resource links directly in `node.value`…
Not making real tests obsolete at all, but all I could come up with for now.
Deploy preview ready! Built with commit 78f43d7 |
Deploy preview ready! Built with commit 78f43d7 |
fk
changed the title
[gatsby-remark-copy-linked-files] Fix regression from #2871 with replaceString cannons
[gatsby-remark-copy-linked-files] Fix regression from #2871 with RegExp cannons
Nov 13, 2017
This is great! Cheerio is nice but yeah, didn't turn out to be a great fit for us as they want to create "well-formed" HTML which doesn't always jive with user input or perhaps how Remark parses things. So yeah, Regex seems like the way to go to ensure we're not breaking people's HTML. |
This was referenced Jan 22, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Sooooo…I broke resource references being updated for HTML-in-Markdown links, videos, and images in #2871 when trying to fix #2696 by removing writing back the HTML
node.value
via Cheerio’s .html(). 🙈Cheerio closes open HTML tags, which in our case caused the problems described in #2696—an HTML node a with value
<a href=…>
would be written back as<a href=…></a>
, putting the link children after the link:Not serializing the Cheerio object of our node back to
node.value
of course resulted in the resource attributes altered with it being lost.Since I couldn’t figure out how to use Cheerio nor Rehype/unified to a) comfortably and relatively safely update the
src
andhref
attributes while b) returning just a fragment of the HTML node (<a href=...>
instead of<a href=...></a>
) , here’s an ugly beginner fix 😬 that replaces the resource links directly innode.value
…All in all this is merely trying to follow up on the mess I made in #2871 as good as my available time currently allows – not convinced about this approach at all, so I left the replace stuff as dumb as possible for now and added a new example instead:
examples/using-remark-copy-linked-files
that hopefully makes it a little easier for other people to take a look. ⛑ 🙏Here's the new example site's main Markdown file, exploring the AST might help understanding my blatherings: https://astexplorer.net/#/gist/307f2f3226954fa34304503a8d9c6cde/c52af3223c9e2b3a14f21ec0df00f143cd688037