docsify icon indicating copy to clipboard operation
docsify copied to clipboard

How to add images from root?

Open rilc0n opened this issue 6 years ago • 23 comments

For example, I have 2 folders in the root directory. There is a picture in the first folder (images).

The second folder (docs) contains the markdown file.

How do I insert an image into this markdown file that is located in the folder (images).

If I put it this way:

![](/images/testimage.jpg)

He tries to add it like this:

/docs/images/testimage.jpg

rilc0n avatar May 15 '19 19:05 rilc0n

Use https://docsify.js.org/#/configuration?id=relativepath

timaschew avatar May 16 '19 08:05 timaschew

This changes the links but not the path to pictures. Am I doing something wrong?

rilc0n avatar May 16 '19 17:05 rilc0n

Hmm, it’s maybe a bug

timaschew avatar Jun 10 '19 20:06 timaschew

I confirm this is a bug. When you use ![](/img/img.png) it doesn't work, but when you use <img src='/img/img.png'></img> it works

marcantoinem avatar Aug 08 '19 00:08 marcantoinem

Hmm .. this bug is fixed?

tianyw0 avatar Aug 18 '19 04:08 tianyw0

Yes, now it works (with a workaround)

marcantoinem avatar Aug 18 '19 15:08 marcantoinem

Hey @LemarkNova , Thanks for reply.

I am confused about the img tag compile result.

my markdown content is :

![my img](/myimg.png)

The docsify compiled result is :

<img src="/foldername/subfoldername/myimg.png" alt.../>

my expected result is :

<img src="/myimg.png" alt.../>

because my all static img file is just in a single folder.

tianyw0 avatar Aug 19 '19 01:08 tianyw0

I found that this 「long path」is not caused by "marked" —— docsify compile markdown file by it,

It seems caused docsify ?

Something could be config ?

tianyw0 avatar Aug 19 '19 02:08 tianyw0

No finally it isn't fixed, I remember that I just use a workaround to use image in markdown: I use ../picturefolder/picture.jpg. So the real problem is that docsify doesn't compile correctly the markdown image. I don't know how you compile the markdown, but the problem is in the compilation of the markdown like in your previous comment.

marcantoinem avatar Aug 19 '19 13:08 marcantoinem

Does Docsify use the latest version of marked? But I just try with marked and it work.

marcantoinem avatar Aug 19 '19 13:08 marcantoinem

Thanks for your help. I note the workaround way.

I do nothing about the compilation of markdown files, just do some steps from the official documentation.

I am not sure the version of marked, but the docsify I installed is the latest version.

tianyw0 avatar Aug 19 '19 13:08 tianyw0

No finally it isn't fixed, I remember that I just use a workaround to use image in markdown: I use ../picturefolder/picture.jpg. So the real problem is that docsify doesn't compile correctly the markdown image. I don't know how you compile the markdown, but the problem is in the compilation of the markdown like in your previous comment.

Hi, could you please help me? #936

ShusenTang avatar Oct 21 '19 14:10 ShusenTang

I've the same problem :(

gigaga avatar Nov 26 '19 10:11 gigaga

It's quite difficult to cover all the cases with a single syntax. I will try to look around this soon.

anikethsaha avatar Nov 26 '19 11:11 anikethsaha

@anikethsaha the behavior should be in function of relativePath? Currently even if this parameter is false, img path generated still is in relative.

gigaga avatar Nov 26 '19 11:11 gigaga

@anikethsaha the behavior should be in function of relativePath?

Yes

Currently even if this parameter is false, img path generated still is in relative

Need an investigation about this.

anikethsaha avatar Nov 26 '19 11:11 anikethsaha

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Feb 04 '20 07:02 stale[bot]

This is still an issue.

cptcanuck avatar Feb 13 '20 04:02 cptcanuck

hook.beforeEach(function(content) {
    return content.replace(new RegExp("/images", 'ig'), "../images");
})

or

hook.afterEach(function(html, next) {
    let reg = new RegExp(vm.config.basePath + ".*/images", 'ig');
    html = html.replace(reg, vm.config.basePath + "images");
    next(html);
})

or

markdown: {
    // https://marked.js.org/#/USING_PRO.md#renderer
    renderer: {
        image: function(href, title) {
            return `<img src="${href}" data-origin="${href}" alt="${title}">`
        }
    }
}

bajins avatar Mar 06 '20 17:03 bajins

In core/render/compiler/image.js

    import { isAbsolutePath, isRelativePath, getPath, getParentPath } from '../../router/util';

    ...

    if (!isAbsolutePath(href)) {
      url = isRelativePath(href) ?
        getPath(contentBase, getParentPath(router.getCurrentPath()), href) :
        getPath(contentBase, href);
    }

In core\router\util.js

export const isRelativePath = cached(path => {
  return /^[^\/]/g.test(path);
});

ngpeijiun avatar Jul 31 '20 16:07 ngpeijiun

If your image is in GitHub repo, Use 'img src' instead of markdown and attach "?raw=true" to end of image's URL.

<img src="https://github.com/your/repo/blob/master/docs/images/img.png?raw=true"</img>

I don't know why, but it works.

computerphilosopher avatar Feb 16 '21 06:02 computerphilosopher

I come up with the same problem, i think the most convinent way is to turn ![]() syntax into <img src="" alt="">.

Maxlinn avatar Apr 16 '22 16:04 Maxlinn

same issue here!

nekename avatar Sep 09 '23 20:09 nekename

Bug acknowledged. Tracking with #1891.

jhildenbiddle avatar Mar 02 '24 04:03 jhildenbiddle