-
-
Notifications
You must be signed in to change notification settings - Fork 494
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
Url filter that outputs relative paths (e.g. ../ or ../../) #648
Comments
Try href="{{ "/" | url ))", the url filter should take care of this. Imo always add the url filter to links to avoid issues later. |
I'm trying solve same problem, and I'm actually using root variable.
Maybe it's good enough for you too :) |
My solution was to add a relative path filter.
|
Thanks @mpalpha ! I tinkered a bit with the filter, here's my version of it: eleventyConfig.addFilter('relativeUrl', (url, page) => {
if (!url.startsWith('/')) {
throw new Error('URL is already relative')
}
const relativeUrl = path.relative(page.filePathStem, url)
return path.relative('..', relativeUrl)
}) <link rel="stylesheet" href="{{ '/css/vendor.css' | relativeUrl(page) }}"> |
I don't know if this is related, but I am running into issues using Typora with 11ty. My markdown links in Typora are like this: For example if I am on Is there a way to make it so all links are at |
I have no idea, I don't use Typora. Maybe you could create a markdown function to replace a string with the current relative path.
|
Isn't that the correct behavior [since the markdown link is relative and not absolute)? If you want it to be |
If I change my markdown links, then I am not able to navigate in my markdown editor (Typora). Doing For every page I define a permalink: |
I'm not sure then, sorry. |
Sounds like this was resolved, thanks folks! @edgarasben please file your issue separately! |
I ended up going with a global edited: switched to .reduce and added a filter module.exports = {
rootPath: (data) =>
data.page.url
.split('/')
.reduce((a, b) => a+(b && '../'),)
}
or const Path = require('path')
eleventyConfig.addFilter("relative", function (url) {
return Path.join(
'./',
this.ctx.page.url.split('/').reduce((a, b) => a + (b && '../')),
url
)
})
|
Thanks @mpalpha
And then use it like this: |
I am having the same issue since I am working with a Website hosted in a subfolder. I managed to change the urls for css stylesheets and thumbnails images, however I did not succeed in changing them for posts urls. |
I put my solution for this problem, which is a mishmash of the other suggestions here, and fitting my own site's requirements at: https://www.npmjs.com/package/eleventy-filter-relative-url |
I came up with another solution which tweaks some of the solutions from here. It doesn't seem like filters get the eleventyConfig.addShortcode("url2", function (url) {
if (!url.startsWith("/")) {
throw new Error("URL is already relative");
}
// See https://www.11ty.dev/docs/languages/liquid/ for more info on page
// variables.
const relativeUrl = path.relative(this.page.url, url); // Use page.url instead of page.filePathStem
return relativeUrl;
}); Usage: {% url2 "/your/absolute/url" %} |
I put your eleventyConfig.addGlobalData('eleventyComputed.rootPath', ()=>{
return data=>data.page.url
.split('/')
.filter(x=>x)
.map(()=>'../')
.join('');
}
); usage : Note that the function passed to |
I am currently working on a 11ty project and really like it. But I have a problem with the links when I deploy the output. I'd like to deploy it to 2 different locations on separate servers. One of the locations is in the root directory, the other one in a sub-folder. It works like a charm for the one that is located in the root directory, but the other doesn't since all the paths are incorrect.
Is it possible to have relative links in the output?
I already tried pathPrefix, but either I don't use it properly or it just doesn't give me the result I am looking for.
.eleventy.js:
When I run
eleventy --config=eleventy.config.js --serve
, an additional folder gets generated with the name_eleventy_redirect
, including anindex.html
file with:When I run
eleventy --config=eleventy.config.js
(without the--serve
), that folder isn't there.However, either way all the links are absolute (e.g. Home is
href="/"
), and the site doesn't work on the server.Is there a way to have either relative links (e.g. Home is
href="./"
on the root index.html andhref="../"
on sub pages) or at least include the subfolder in the urls (e.g. Home ishref="./subfolder/"
)?The text was updated successfully, but these errors were encountered: