[Question] How I setup a base path for img or url that use relative path ? #138

Closed
opened 2026-01-29 14:28:26 +00:00 by claunia · 13 comments
Owner

Originally created by @John0King on GitHub (Sep 15, 2017).

I'm new to this project, and I try to render a markdown file through MVC.
I need to config the relative path to a absolute path such like ![](image/imageName.jpg) => <img src="/doc/?path=imageName.jpg" /> .
Is there a way to do this ? Thanks.

Originally created by @John0King on GitHub (Sep 15, 2017). I'm new to this project, and I try to render a markdown file through MVC. I need to config the relative path to a absolute path such like `![](image/imageName.jpg)` => `<img src="/doc/?path=imageName.jpg" />` . Is there a way to do this ? Thanks.
claunia added the enhancementPR Welcome! labels 2026-01-29 14:28:26 +00:00
Author
Owner

@xoofx commented on GitHub (Sep 15, 2017):

Currently, there is no interface to do this automatically. Either you have to process the Markdown AST and fix the links, and then render this AST to HTML. Or make a PR that can introduce this as part of the HTML rendering part, as an option (base url for relative url for example).

@xoofx commented on GitHub (Sep 15, 2017): Currently, there is no interface to do this automatically. Either you have to process the Markdown AST and fix the links, and then render this AST to HTML. Or make a PR that can introduce this as part of the HTML rendering part, as an option (base url for relative url for example).
Author
Owner

@markheath commented on GitHub (Jan 17, 2018):

I've had a crack at doing this in the HtmlRenderer class. Should be minimal perf impact if the feature isn't being used

    /// <summary>
    /// Gets a value to use as the base url for all relative links
    /// </summary>
    public Uri BaseUrl { get; set; }

    public HtmlRenderer WriteEscapeUrl(string content)
    {
        if (content == null)
            return this;

        if (BaseUrl != null && !Uri.TryCreate(content, UriKind.Absolute, out Uri _))
        {
            content = new Uri(BaseUrl, content).AbsoluteUri;
        }

        ....

I've made some unit tests too, so if you like this approach I can issue a PR

@markheath commented on GitHub (Jan 17, 2018): I've had a crack at doing this in the `HtmlRenderer` class. Should be minimal perf impact if the feature isn't being used /// <summary> /// Gets a value to use as the base url for all relative links /// </summary> public Uri BaseUrl { get; set; } public HtmlRenderer WriteEscapeUrl(string content) { if (content == null) return this; if (BaseUrl != null && !Uri.TryCreate(content, UriKind.Absolute, out Uri _)) { content = new Uri(BaseUrl, content).AbsoluteUri; } .... I've made some unit tests too, so if you like this approach I can issue a PR
Author
Owner

@xoofx commented on GitHub (Jan 17, 2018):

Yep, looks good for a PR

@xoofx commented on GitHub (Jan 17, 2018): Yep, looks good for a PR
Author
Owner

@John0King commented on GitHub (Jan 22, 2018):

@markheath this is great , that works for me !!
but I think maybe it better be a delegate method instead just a property , for example

public delegate string UrlMarkdownConverter(string rawValue,string extension,bool isRelative)

public UrlMarkdownConverter UrlConverter { get;set; } = (raw,ext,isrelative)=> rawUrl  ; // default value

and then I can do this

reander.UrlConverter = (raw,ext,isRelative)=>
{
    if( isRelative && string.Equals(ext,".md") )
    {
         return raw.Replace(ext,".html"); // better to use Regex class
    }
}

to actually generate markdown to HTML files

@John0King commented on GitHub (Jan 22, 2018): @markheath this is great , that works for me !! but I think maybe it better be a delegate method instead just a property , for example ```c# public delegate string UrlMarkdownConverter(string rawValue,string extension,bool isRelative) public UrlMarkdownConverter UrlConverter { get;set; } = (raw,ext,isrelative)=> rawUrl ; // default value ``` and then I can do this ```C# reander.UrlConverter = (raw,ext,isRelative)=> { if( isRelative && string.Equals(ext,".md") ) { return raw.Replace(ext,".html"); // better to use Regex class } } ``` to actually generate markdown to HTML files
Author
Owner

@markheath commented on GitHub (Jan 23, 2018):

I think if you went that approach it would make more sense to just have a Func<string,string> and give complete control to the caller to decide how they want to interpret and transform the URL

@markheath commented on GitHub (Jan 23, 2018): I think if you went that approach it would make more sense to just have a `Func<string,string>` and give complete control to the caller to decide how they want to interpret and transform the URL
Author
Owner

@John0King commented on GitHub (Jan 24, 2018):

@markheath I think 'get extension' and 'the path is relative or not' may be a very common action, if it not done by render,then it need to be done in your code anyway. and add those two parameter you still have complete control.
Func<string,string> is still good for me too . :P

@John0King commented on GitHub (Jan 24, 2018): @markheath I think 'get extension' and 'the path is relative or not' may be a very common action, if it not done by `render`,then it need to be done in your code anyway. and add those two parameter you still have complete control. `Func<string,string>` is still good for me too . :P
Author
Owner

@markheath commented on GitHub (Jan 24, 2018):

Any opinions @xoofx ? I can issue a PR with a Func<string,string> with the property called LinkRewriter

@markheath commented on GitHub (Jan 24, 2018): Any opinions @xoofx ? I can issue a PR with a `Func<string,string>` with the property called `LinkRewriter`
Author
Owner

@xoofx commented on GitHub (Jan 24, 2018):

@markheath Sure, we can have a LinkRewriter callback as well (but we keep the BaseUrl). It would run just after the BaseUrl rewrite if any.

@xoofx commented on GitHub (Jan 24, 2018): @markheath Sure, we can have a LinkRewriter callback as well (but we keep the BaseUrl). It would run just after the BaseUrl rewrite if any.
Author
Owner

@carlowahlstedt commented on GitHub (Mar 23, 2018):

Curious when you're planning on releasing this? I stumbled on this when looking to get this working. Fundamentally, I just need the BaseUrl concept. The only other improvement I can see is adding a way to set it in Markdown.ToHtml(text, pipeline); but I can see why you wouldn't want to do that.

@carlowahlstedt commented on GitHub (Mar 23, 2018): Curious when you're planning on releasing this? I stumbled on this when looking to get this working. Fundamentally, I just need the BaseUrl concept. The only other improvement I can see is adding a way to set it in `Markdown.ToHtml(text, pipeline);` but I can see why you wouldn't want to do that.
Author
Owner

@MihaZupan commented on GitHub (Apr 5, 2019):

Closing as resolved by #201

@MihaZupan commented on GitHub (Apr 5, 2019): Closing as resolved by #201
Author
Owner

@Matix-Media commented on GitHub (Sep 9, 2020):

How do I use this function now?

@Matix-Media commented on GitHub (Sep 9, 2020): How do I use this function now?
Author
Owner

@xoofx commented on GitHub (Sep 10, 2020):

How do I use this function now?

There are tests in the PR just above that shows how to use it

@xoofx commented on GitHub (Sep 10, 2020): > How do I use this function now? There are tests in the PR just above that shows how to use it
Author
Owner

@Matix-Media commented on GitHub (Sep 10, 2020):

I am really new to this, and I can't figure out how to use it.

I only know how to setup the basic markdown renderer but that's all. Do I set this up in brackets at the initialization of markdown-it like this: new markdownit({BaseUrl: my_url})
Because that didn't worked for me.

i'm sorry if i ask something very simple. Unfortunately, I am not very familiar with this area.

@Matix-Media commented on GitHub (Sep 10, 2020): I am really new to this, and I can't figure out how to use it. I only know how to setup the basic markdown renderer but that's all. Do I set this up in brackets at the initialization of markdown-it like this: `new markdownit({BaseUrl: my_url})` Because that didn't worked for me. i'm sorry if i ask something very simple. Unfortunately, I am not very familiar with this area.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/markdig#138