Add an image to markdown file (Github and local computer) and solve the problem that the image can't be displayed in the md file preview
Chinese version: Zhihu
1. Github[1]
1.1. Locate in the same folder
If the image is at the same folder with the md file, for example, "username/example.md" and "username/1.png", then you should just input:

1.2. Locate in different places
If the image locates in "username/docs/assets/1.png" and the md file locates in "username/docs/example.md"then you should input :

1.3. URL
If the url of the image is "https://example/1.jpg", then you should input:

2. Local computer
Markdown editor: VS Code
Extention: Markdown Preview Enhanced by Yiyi Wang
2.1. Locate in the same folder
If the image is at the same folder with the md file, for example, "C:\Users\X\Desktop\example.md" and "C:\Users\X\Desktop\1.png", then you should just input:

2.2. Md file locates in one folder and the image locates in its subfolder
If the image is at a subfolder under the folder where the md file locates, for example, md file locates at "C:\Users\X\Desktop\example.md" and the image locates "C:\Users\X\Desktop\NoExample\1.jpg", then you should input:

Note that there can't be spaces in the name of the folder. If there are spaces, replace them with %20. (You can check this when the other codes in this article don't work.)
2.3. URL
It's same as 1.3.
2.4. Absolute file path
If the file path of the image is "C:\Users\X\Desktop\1.png", then input:

Note to change the first "\" to "/" so that the image can be shown normally.
2.5. Base64 string[2]
Convert the image to base64 string in below websites:
Image to Base64 converter to convert Image to Base64 String
then you should input:
)
or you can input:
![][tmp]
at the place where you want to insert image and input:
[tmp]: data:image/png;base64,... (the base64 string)
at the end of the article to place the base64 string at the end of the article. (png as an example)
Note it is [tmp] rather than (tmp), [] and not (), and you should input "data:image/png;base64," before your plain base64 string. No space between them.
If it you still can't see the image in preview, save your md file to local then have a try again. (You can try this when the other codes in 2. don't work.)
This method will embed the image into the md file, so the file will be bigger.
Comments
Post a Comment