Pasting image for Markdown/reST lexers

Plugins for SynWrite and CudaText...
lookoutside
Posts: 51
Joined: 17.08.2022 13:42

Pasting image for Markdown/reST lexers

Post by lookoutside »

Pasting image in plain text editor is a very continent function for markdown/reStructuredtext users. When the user copy/screen-capture an image in clipboard and paste in text, the following actions are accomplished in the background:

1. the image is saved as a file in the specific folder, and the file name and path are predefined which are set by users,
2. the relative image path (including file name) of the file are pasted as text under the cursor

Typora has this feature. Vscode afford this function as a extension (paste image extension).

Cudatext plug-in insert-pics is very similar to the Paste image extension but work a little differently. In Cudatext we can see the image directly. Will it be possible for Cudatext to support the Paste image function? This feature is helpful but not very urgent.

Thank you.
Last edited by lookoutside on 23.08.2022 07:32, edited 3 times in total.
main Alexey
Posts: 2323
Joined: 25.08.2021 18:15

Post by main Alexey »

nice suggestion. but, I already did it years ago. so plugin 'Insert Pics' has the command here:
Plugins / Insert Pics / ...
you will find the command with word 'clipboard' there. does it work?
lookoutside
Posts: 51
Joined: 17.08.2022 13:42

Post by lookoutside »

I tried insert pic,
20220823_134140.png
20220823_134140.png (3.79 KiB) Viewed 1566 times
After I inserted a pic, then I can see image showing in cudatext, it's amazing. But It works a little different:

1. I can't find the folder containing the copied/pasted file
2. I can't get the new file path in the current editor

The file and filepath are needed as image link for markdown/rst users when they insert images. Of course insert pics is wonderful, but it can't solve the markdowner's problem in pasting image.

I think cudatext can do this and even better than others, because cudatext can show the image directly. Insert pics is a wonderful feature, while if it is combined with Paste image, then more people will be benefited.

Should I give more details about the function of Paste image?
Last edited by lookoutside on 23.08.2022 07:33, edited 1 time in total.
main Alexey
Posts: 2323
Joined: 25.08.2021 18:15

Post by main Alexey »

Image was pasted from memory to memory, then Insert Pics saves this image in a 'helper file' in base64 coding.
To make this feature for Markdown, it needs more coding, maybe I will do it.
do you suggest to insert this code to 'Markdown Editing' plugin? to a new plugin?
Hobbes
Posts: 43
Joined: 17.05.2022 07:53

Post by Hobbes »

Great plugin and nice idea.

From my point of view, I would prefer to have the Markdown option (relative path instead of image) in the same 'Insert Pics' plugin. Mostly to avoid several plugins doing almost the same or hide useful features (insert path option can be useful not only for Markdown). In fact, an option to switch between this two alternatives (image/relative path) would be great.

But if it implies a lot of coding, or redoing most of the previous plugins, the best option could be a new one.
lookoutside
Posts: 51
Joined: 17.08.2022 13:42

Post by lookoutside »

main Alexey wrote:Image was pasted from memory to memory, then Insert Pics saves this image in a 'helper file' in base64 coding.
To make this feature for Markdown, it needs more coding, maybe I will do it.
do you suggest to insert this code to 'Markdown Editing' plugin? to a new plugin?
In my opinion, paste image should not be bound with markdown. For example, I seldom use markdown because I use another markup. I think that paste image can serve all markup editing. You can enhanced the existing insert-pics or make a new plugin, and then it can be called paste image. The name is well known, easy to recall.
main Alexey
Posts: 2323
Joined: 25.08.2021 18:15

Post by main Alexey »

I now added more functions to plugin 'Special URL paste' and renamed it to 'Markdown Special Paste'. from its readme.txt:
1) if clipboard contains text URL: 'http://' or 'https://'.
it changes pasted text according to these templates:

- for Markdown: [Text](url)
- for reStructuredText: `Text <url>`__

2) if clipboard contains a picture. plugin then suggests to save the picture
in the folder of the current file, and after that it inserts the link to this
new file.

- for Markdown: '![alt text](filename.png "Title")
- for reStructuredText: .. image:: filename.png
plugin can support more lexers. tell me what markup do you use. and how to extend this code for your format:

Code: Select all


FORMAT_URL = {
    'Markdown': '[{title}]({url})',
    'reStructuredText': '`{title} <{url}>`__',
}

FORMAT_PIC = {
    'Markdown': '![alt text]({filename} "Title")',
    'reStructuredText': '\n.. image:: {filename}',
}
lookoutside
Posts: 51
Joined: 17.08.2022 13:42

Post by lookoutside »

Most of the time, I don't want to save the image in the folder of the current file but in the folder containing the curatext project-file. Doing so let me move my .md or .rst file freely, not worrying about the image path.

This is just a case for me, not for everyone.

VS Code's paste image is a very good example. Users can set the path format and define where to store the image file. So I can use either relative and obsolete paths at different occasion.
main Alexey
Posts: 2323
Joined: 25.08.2021 18:15

Post by main Alexey »

okay, I improved plugin. (update in AddonsManager.)
it now has 2 options and it can save picture to the folder of project-file!
please test.

===
config file "plugins.ini" can be opened via menu item:
"Options / Settings-plugins / Markdown Special Paste / Config".
options there are:
- url_timeout - Timeout in seconds, which it used on downloading webpage by its URL.
- pic_path - Initial folder for picture file, when pasting picture. Can have macros:
{projdir} - Folder of currently opened CudaText project. If none, it's folder of current editor file.
{filedir} - Folder of current editor file.
main Alexey
Posts: 2323
Joined: 25.08.2021 18:15

Post by main Alexey »

improved plugin 'Markdown Special Paste'.
added 2 more options:
- pic_name - Initial suggested name for picture file. Can have macros:
{now} - Current date/time formatted as yyyy-mm-dd-hh-mm-ss.
- now - Date/time format for {now} macro. See Python docs for strftime().

added support for lexers MediaWiki and AsciiDoc.
Post Reply