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

Drag and Drop to create Markdown images in MDX files #322

Closed
4 tasks done
karlhorky opened this issue May 4, 2023 · 17 comments · Fixed by #345, #346 or #357
Closed
4 tasks done

Drag and Drop to create Markdown images in MDX files #322

karlhorky opened this issue May 4, 2023 · 17 comments · Fixed by #345, #346 or #357
Labels
🗄 area/interface This affects the public interface 💪 phase/solved Post is done 🧒 semver/minor This is backwards-compatible change 🦋 type/enhancement This is great to have

Comments

@karlhorky
Copy link

karlhorky commented May 4, 2023

Initial checklist

Problem

Quickly authoring MDX documents by dragging and dropping images is a nice DX

There is a drag and drop feature introduced in VS Code 1.70 (thanks @mjbvz) that allowed for handling dropped files (either from the VS Code explorer, editor tabs, the OS, or other applications) (extension sample) - activate this feature by holding shift while dragging a file.

With Markdown files (*.md), VS Code allows for creating a Markdown image:

Changing.how.an.image.is.dropped.using.the.drop.selector.widget.mp4

It would be amazing if creating Markdown images with drag and drop was also supported in an MDX file. Currently, as of VS Code 1.78, it just inserts the path:

vscode-1.78-april-2023-drag-drop-mdx.mp4

Solution

Apparently in VS Code 1.78 (April 2023) - or maybe earlier - there is an option for extensions to contribute their own options to this list:

VS Code includes a few built-in ways to drop common content formats. Extensions can also add their own drop options using the DocumentDropEditProvider API.

Source: https://code.visualstudio.com/updates/v1_78#_drop-selector

Maybe this would be the way to contribute this action of "Insert Markdown Image"

Alternatives

Installing the Drag And Drop Import Relative Path extension by @ElecTreeFrying

@karlhorky karlhorky changed the title Drag and Drop to create Markdown images in MDX files Drag and Drop to create Markdown images and videos in MDX files May 4, 2023
@wooorm
Copy link
Member

wooorm commented May 4, 2023

How far should this be taken? What should we generate?

  • What about other markdown, or MDX, files? Are there more formats? CSS? JS/JSX/TS/TSX?
  • Should we generate markdown images? Or an <img>, <Image>? I think markdown.
  • <video>, <Video>? What attributes?
  • For videos, what about other source formats? User might drop an .mp4, and have an .webm next to it, should we figure that out and inject that too? Or, should someone drop multiple video files to get it turned? Or, can a user first drop one file, which turns into a video, and then drop another somewhere inside it, in which case a <source> is added?
  • What URLs should be used? Presumably, MDX is compiled and runs from somewhere else (some bundle), so we’d need to be aware of where assets will be hosted, perhaps they aren’t, or somewhere else entirely?

@karlhorky
Copy link
Author

karlhorky commented May 4, 2023

I would suggest just copying the relatively simple VS Code implementation for a start - only handle single files, and insert (using relative paths):

  • Markdown for images
  • HTML element for videos

And only allow drag + drop to *.mdx files.

I think that's probably the 80% or even 90% use case.

@remcohaszing
Copy link
Member

I like the idea of starting with inserting images using markdown syntax to get started. The inserted image node should contain a path relative to the MDX file. I think we should consider supporting other file types (Video files? URLs?) afterwards.

We also need to figure out is this is going to be part of the VSCode extension, or if the language server can somehow support this.

@karlhorky
Copy link
Author

Wonder if this Copy external media files into workspace on drop or paste for Markdown from VS Code 1.79 (May 2023) is going to also just work out of the box, when this feature is implemented 😍

Coping.a.file.into.the.workspace.by.drag.and.dropping.it.mp4

remcohaszing added a commit that referenced this issue Oct 27, 2023
When dropping an image, the image is downloaded next to the file, and a
markdown link is generated. When text is dropped, the text is inserted
as-is. This only works in VS Code.

This uses a naive text-based approach. This is very similar to the
implementation for markdown files that’s builtin to VS Code.

Closes #322
remcohaszing added a commit that referenced this issue Oct 27, 2023
When dropping an image, the image is downloaded next to the file, and a
markdown link is generated. When text is dropped, the text is inserted
as-is. This only works in VS Code.

This uses a naive text-based approach. This is very similar to the
implementation for markdown files that’s builtin to VS Code.

Closes #322
remcohaszing added a commit that referenced this issue Oct 27, 2023
When dropping an image, the image is downloaded next to the file, and a
markdown link is generated. When text is dropped, the text is inserted
as-is. This only works in VS Code.

This uses a naive text-based approach. This is very similar to the
implementation for markdown files that’s builtin to VS Code.

Closes #322
remcohaszing added a commit that referenced this issue Oct 30, 2023
When dropping an image, the image is downloaded next to the file, and a
markdown link is generated. When text is dropped, the text is inserted
as-is. This only works in VS Code.

This uses a naive text-based approach. This is very similar to the
implementation for markdown files that’s builtin to VS Code.

Closes #322
@github-actions

This comment has been minimized.

@karlhorky
Copy link
Author

karlhorky commented Oct 30, 2023

Thanks for the PR and review at #345 @remcohaszing @wooorm 🙌

One question regarding this note from #345:

When dropping an image, the image is downloaded next to the file, and a markdown link is generated

When dragging and dropping an existing image from the VS Code tree (eg. an image which is either a sibling or in a different directory), I am guessing that it doesn't download the image again, right?

@remcohaszing
Copy link
Member

It doesn’t handle images dragged from the file tree apparently, only from the web. But it definitely makes sense to do so. I’ll fix that right away.

@remcohaszing remcohaszing reopened this Oct 30, 2023
remcohaszing added a commit that referenced this issue Oct 30, 2023
A mime type of `uri-list` is for example files dragged from the VSCode
tree view.

Images are turnes into Markdown images. These are detected based on a
hardcoded list of file extensions. Other URIs are inserted as-is.

If the dragged URI uses the same scheme as the document it was dragged
in, a relative path is inserted. Otherwise the full URI is used.

Closes #322
remcohaszing added a commit that referenced this issue Nov 7, 2023
A mime type of `uri-list` is for example files dragged from the VSCode
tree view.

Images are turnes into Markdown images. These are detected based on a
hardcoded list of file extensions. Other URIs are inserted as-is.

If the dragged URI uses the same scheme as the document it was dragged
in, a relative path is inserted. Otherwise the full URI is used.

Closes #322
@remcohaszing remcohaszing added 🦋 type/enhancement This is great to have 🗄 area/interface This affects the public interface 🧒 semver/minor This is backwards-compatible change 💪 phase/solved Post is done labels Nov 7, 2023
@karlhorky
Copy link
Author

karlhorky commented Nov 7, 2023

Thanks @remcohaszing , awesome 🚀

I judge from the semver/minor label and the current version of 1.4.0 on the Releases page and the extension page that this will be released in a future 1.5.0 version?

I will be one of your first testers and will be happy to also do my bit to push the feature on social media too 🙌

@remcohaszing
Copy link
Member

Yes. I really want to implement #349 in this release. That’s currently the only blocker (but not a hard one). Expect a release very soon.

I love the enthousiasm and would love your feedback and any promotion on social media ❤️

@remcohaszing
Copy link
Member

Released in [email protected], @mdx-js/[email protected], @mdx-js/[email protected] 🎉

Re promotion: feel free to retweet https://twitter.com/remcohaszing/status/1722621607620059512 😄

@karlhorky
Copy link
Author

Finally got some time to look at this.

Just trying to make a video of this feature for X/Twitter and I'm having trouble activating the feature. Am I doing something wrong?

I would expect that dragging a .png file into an .mdx file while holding Shift would cause an image tag to be created in that MDX (either Markdown image syntax or <img> tag)

Kapture.2023-11-26.at.15.53.17.mp4

@remcohaszing remcohaszing reopened this Nov 27, 2023
@remcohaszing
Copy link
Member

I’ve confirmed and found the problem.

You can drop images from external sources, but not reference local files. I’m working on it right now.

@remcohaszing remcohaszing added the 🤞 phase/open Post is being triaged manually label Nov 27, 2023
@github-actions github-actions bot removed the 💪 phase/solved Post is done label Nov 27, 2023
remcohaszing added a commit that referenced this issue Nov 27, 2023
This was using `URL.canParse`, which isn’t available in the Node.js
version used by VSCode. This failed silently.  This is fixed by using
strict parse mode from `vscode-uri`.

Closes #322
remcohaszing added a commit that referenced this issue Nov 27, 2023
This was using `URL.canParse`, which isn’t available in the Node.js
version used by VSCode. This failed silently.  This is fixed by using
strict parse mode from `vscode-uri`.

Closes #322
remcohaszing added a commit that referenced this issue Dec 5, 2023
This was using `URL.canParse`, which isn’t available in the Node.js
version used by VSCode. This failed silently.  This is fixed by using
strict parse mode from `vscode-uri`.

Closes #322

This comment has been minimized.

@remcohaszing remcohaszing added the 💪 phase/solved Post is done label Dec 5, 2023
@github-actions github-actions bot removed the 🤞 phase/open Post is being triaged manually label Dec 5, 2023
@karlhorky
Copy link
Author

karlhorky commented Dec 17, 2023

@remcohaszing I was making a video to test this just now, and it almost works!

The path seems to be 1 level incorrect though (the ../ prefix to the inserted path ../ci-cd-pipeline.png should be ./ or just an empty string)

Kapture.2023-12-17.at.13.54.20.mp4

Paths:

  • slide-decks/partials/ciCd/index.mdx
  • slide-decks/partials/ciCd/ci-cd-pipeline.png

@remcohaszing
Copy link
Member

Fixed in 31f1912.

@karlhorky
Copy link
Author

Great, just tested this in 1.6.1 of the VS Code and it works, thanks! 🚀 did some posts about it:

@karlhorky
Copy link
Author

I edited this issue to be image-only and moved the video parts to a new issue:

@karlhorky karlhorky changed the title Drag and Drop to create Markdown images and videos in MDX files Drag and Drop to create Markdown images in MDX files Dec 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🗄 area/interface This affects the public interface 💪 phase/solved Post is done 🧒 semver/minor This is backwards-compatible change 🦋 type/enhancement This is great to have
3 participants