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

relative baseUrl #1526

Merged
merged 1 commit into from Jul 19, 2019
Merged

relative baseUrl #1526

merged 1 commit into from Jul 19, 2019

Conversation

UziTech
Copy link
Member

@UziTech UziTech commented Jul 18, 2019

Marked version: master

Description

Allow baseUrl option to be a relative url.

Expectation

<p><img src="/favicon.ico" alt="alt text" title="relative"></p>

Result

<p><img src="/derp//favicon.ico" alt="alt text" title="relative"></p>

What was attempted

marked('![alt text](/favicon.ico "relative")', {baseUrl: "/derp/"});

Contributor

  • Test(s) exist to ensure functionality and minimize regression.
  • If submitting new feature, it has been documented in the appropriate places.

Committer

In most cases, this should be a different person than the contributor.

  • Draft GitHub release notes have been updated.
  • CI is green (no forced merge required).
  • Merge PR

@vercel
Copy link

vercel bot commented Jul 18, 2019

This pull request is automatically deployed with Now.
To access deployments, click Details below or on the icon next to each push.

Latest deployment for this branch: https://markedjs-git-fork-uzitech-baseurl-null.markedjs.now.sh

Copy link
Contributor

@davisjam davisjam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks OK from a regex perspective.

@UziTech UziTech merged commit aff0aa4 into markedjs:master Jul 19, 2019
@UziTech UziTech deleted the baseurl-null branch July 19, 2019 15:36
@UziTech UziTech mentioned this pull request Dec 4, 2019
12 tasks
This was referenced Dec 20, 2019
This was referenced Apr 24, 2020
@EdwardEisenhauer
Copy link

EdwardEisenhauer commented Nov 1, 2021

Hello,

is there a way to turn off this behaviour via options? I have a lot of relative links in my markdown files, which come from the external (not the one in which marked is used) website.

@UziTech
Copy link
Member Author

UziTech commented Nov 1, 2021

@EdwardEisenhauer baseUrl is null by default.

@EdwardEisenhauer
Copy link

@EdwardEisenhauer baseUrl is null by default.

after setting baseURL via:

marked.setOptions({
	baseUrl: 'https://external.domain/some/path/',
});

when using:

marked('[some_link](/i/want/to/go/there)');

I receive:

"<p><a href=\"https://external.domain/i/want/to/go/there\">some_link</a></p>"

when I would like to get:

"<p><a href=\"https://external.domain/some/path/i/want/to/go/there\">some_link</a></p>"

as if calling:

marked('[some_link](i/want/to/go/there)');

I would like to be able to turn off relative link generation if there is a leading slash.

@UziTech
Copy link
Member Author

UziTech commented Nov 2, 2021

@EdwardEisenhauer your best bet would be to walk the tokens and update the href

marked.use({
  walkTokens(token) {
    if (token.type === 'link') {
      token.href = 'https://external.domain/some/path' + token.href;
    }
  }
});
marked('[some_link](/i/want/to/go/there)');

https://runkit.com/embed/rmsje9r0y7cy

@EdwardEisenhauer
Copy link

@UziTech I modified it to:

if (token.type === 'link' && token.href[0] === '/') {
    token.href = 'https://external.domain/some/path' + token.href;
}

And now it works as expected (as I have some links to the external webpages too). Thanks for help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

baseUrl with relative prefix not working as expected.
4 participants