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

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

Closed
John0King opened this issue Sep 15, 2017 · 13 comments
Closed

Comments

@John0King
Copy link

John0King commented 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.

@xoofx
Copy link
Owner

xoofx commented 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).

@markheath
Copy link
Contributor

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

@xoofx
Copy link
Owner

xoofx commented Jan 17, 2018

Yep, looks good for a PR

@John0King
Copy link
Author

@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

@markheath
Copy link
Contributor

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

@John0King
Copy link
Author

@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

@markheath
Copy link
Contributor

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

@xoofx
Copy link
Owner

xoofx commented 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.

markheath added a commit to markheath/markdig that referenced this issue Jan 24, 2018
@carlowahlstedt
Copy link

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.

@MihaZupan
Copy link
Collaborator

Closing as resolved by #201

@Matix-Media
Copy link

How do I use this function now?

@xoofx
Copy link
Owner

xoofx commented Sep 10, 2020

How do I use this function now?

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

@Matix-Media
Copy link

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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants